Arduino
Input devices: Interface a potentiometer analog input to maker UNO board
and measure/show its signal in serial monitor Arduino IDE.
1.
Below are the code/program I have used and
the explanation of the code.
|
Code/program in writeable format |
Explanation of the code |
|
int SensorValue = 0; void setup() { Serial.begin(9600); pinMode(A0, INPUT); pinMode(LED_BUILTIN, OUTPUT); } void loop() { // read the value from sensor SensorValue = analogRead(A0); Serial.println(SensorValue); // turn the LED on digitalWrite(LED_BUILTIN, HIGH); // pause the program for <sensorValue> milliseconds delay(SensorValue); // Wait for SensorValue millisecond(s) // turn the LED off digitalWrite(LED_BUILTIN, LOW); // pause the program for <sensorValue> milliseconds delay(SensorValue); // Wait for SensorValue millisecond(s) } |
int sensorValue = 0; declares another integer variable to store the value of the potentiometer Serial.begin(9600); means to start serial communication between Arduino and micro controller through usb cable with a maximum communicate rate of 9600 bits per seconds pinMode(A0, INPUT); set analog pin 0 as the input pinMode(LED_BUILTIN, OUTPUT); set pin 13 as the output sensorValue = analogRead(A0); is to read the voltage levels Serial.println(sensorVal); prints data to the serial port Digitalwrite() is used to set the output of pin 13 to HIGH, turning ON the LED Delay() is used to create a delay of 0 milliseconds Digitalwrite() is used to set the output of pin 13 to LOW, turning OFF the LED Delay() is used to create a delay of 0 milliseconds |
2.
1. Below are the hyperlink to the
sources/references that I used to write the code/program.
https://youtu.be/-EDYMQ9lczA
2.
Below are the problems I have encountered and
how I fixed them.
Initially, I was not sure how to show the data from the Serial Monitor. I fixed them by asking my friends if I was missing anything out. They told me I did not put in serialbegin(9600) in void setup and serial.println(sensorVal)!!
3. Below is the short video as the evidence that the code/program work.
Input devices: Interface a LDR to maker UNO board and measure/show its
signal in serial monitor Arduino IDE:
1.
Below are the code/program I have used and
the explanation of the code.
|
Code/program in writeable format |
Explanation of the code |
|
int LDR = A0; int ledPin = 13; int LDRvalue = 0; void setup() { Serial.begin(9600); pinMode(ledPin,OUTPUT); } void loop() { Serial.println(LDRvalue); LDRvalue = analogRead(LDR); if(LDRvalue > 600) digitalWrite(ledPin, HIGH); else digitalWrite(ledPin, LOW); } |
int LDR = A0; is to define LDR pin as analog pin, A0 int ledPin = 13; is to define pin 13 Serial.begin(9600); means to start serial communication between Arduino and micro controller through usb cable with a maximum communicate rate of 9600 bits per seconds pinMode(ledPin,OUTPUT); is to set pin 13 as the output Serial.println(sensorVal); prints data to the serial port LDRvalue = analogRead(LDR); reads analog values from the LDR If the LDRvalue > 600, digitalWrite() is then used to set pin13 to HIGH, turning LED on If the LDR < 600, digitalWrite() is then used to set pin13 to LOW, turning LED OFF |
Below are the hyperlink to the
sources/references that I used to write the code/program.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5400368%3FfilterOnDatesAndDepth%3D1
Below are the problems I have encountered and
how I fixed them.
There were no problems encountered.
Below is the short video as the evidence that
the code/program work.
Output devices: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board
and program it to perform something (fade or flash etc)
Below are the code/program I have used and
the explanation of the code.
|
Code/program in writeable format |
Explanation of the code |
|
void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); } void loop() { animationspeed = 0; digitalWrite(LED_BUILTIN, HIGH); delay(700); // Wait for animationspeed millisecond(s) digitalWrite(LED_BUILTIN, LOW); delay(1000); // Wait for animationspeed millisecond(s) digitalWrite(12, HIGH); delay(800); // Wait for animationspeed millisecond(s) digitalWrite(12, LOW); delay(300); // Wait for animationspeed millisecond(s) digitalWrite(11, HIGH); delay(1000); // Wait for animationspeed millisecond(s) digitalWrite(11, LOW); delay(300); } |
pinMode(11,OUTPUT); sets pin 11 as an output pinMode(12,OUTPUT); sets pin 11 as an output pinMode(13,OUTPUT); sets pin 11 as an output digitalWrite() is used to set pin13 to HIGH, turning LED on delay(700) creates a 0.7 second delay digitalWrite() is used to set pin13 to LOW, turning LED off delay(1000) creates a 1 second delay digitalWrite() is used to set pin12 to HIGH, turning LED on delay(800) creates a 0.8 second delay digitalWrite() is used to set pin12 to LOW, turning LED off delay(300) creates a 0.3 second delay digitalWrite() is used to set pin11 to HIGH, turning LED on delay(1000) creates a 1 second delay digitalWrite() is used to set pin11 to LOW, turning LED off delay(300) creates a 0.3 second delay |
Below are the hyperlink to the sources/references that I used to write the code/program.
https://www.youtube.com/watch?v=MojSo7OtF9w
Below are the problems I have encountered and
how I fixed them.
There were no problems.
Below is the short video as the evidence that
the code/program work.
Output devices: Include pushbutton to start/stop the previous task
Below are the code/program I have used and
the explanation of the code.
|
Code/program in writeable format |
Explanation of the code |
|
const int greenLed = 13; const int yellowLed = 12; const int redLed = 11; const int pushButton = 2; void setup() { pinMode(greenLed, OUTPUT); pinMode(yellowLed, OUTPUT); pinMode(redLed, OUTPUT); pinMode(pushButton, INPUT_PULLUP); } void loop() { if(digitalRead(pushButton) == LOW) { digitalWrite(greenLed, HIGH); delay(700); digitalWrite(yellowLed, HIGH); digitalWrite(greenLed, LOW); delay(1700); digitalWrite(redLed, HIGH); digitalWrite(yellowLed, LOW); delay(1700); digitalWrite(redLed, LOW); digitalWrite(greenLed, HIGH); } } |
Pin 13 is constant Pin 12 is constant Pin 11 is constant Pin 2 is constant pinMode(greenLed, OUTPUT); sets pin 13 as an output pinMode(yellowLed, OUTPUT); sets pin 12 as an output pinMode(redLed, OUTPUT); sets pin 11 as an output pinMode(pushButton, INPUT_PULLUP) assigned pin 2 as an input port If push button is pressed, the green LED light light up first. After 0.7s, thr green light switched off and the yellow light switched on. After 1.7s, the yellow light switched off, the red light is then swithced on. After 1.7s, the red light switched off, the green light switched on. |
Below are the hyperlink to the
sources/references that I used to write the code/program.
https://splms.polite.edu.sg/d2l/le/enhancedSequenceViewer/172726?url=https%3A%2F%2Fb988e89b-eb2c-401a-999a-94b943da0008.sequences.api.brightspace.com%2F172726%2Factivity%2F5400368%3FfilterOnDatesAndDepth%3D1
3.
Below are the problems I have encountered and
how I fixed them.
There were no problems
4.
Below is the short video as the evidence that
the code/program work.
Below is my Learning Reflection on the overall Arduino Programming
activities.
Overall, I think it is a great activity for me to learn how to code with Arduino Programming, though it is a bit frustrating (sometimes when we compile the files there will always be errors). I also enjoyed the practical activity as well. We had a fun time assembling, designing the cardboard pegasus and to use the coding skills we learnt pre-practical like make some noise, blink, hello world and servo. Of course there are some improvements made, my group could have designed a cardboard stand for the pegasus to stand on top of it in order to hide the servo and the wires to make it more aesthetically pleasing.
Here is the code for our pegasus!!!
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_A7
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4
};
void setup() {
pinMode(LED, OUTPUT);
pinMode(3, OUTPUT);`
myservo.attach(11); // attaches the servo on pin 9 to the servo object
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(3, HIGH);
delay (100);
digitalWrite(3, LOW);
delay (100);
for (pos = 0; pos <= 180; pos += 15) { // goes from 0 degrees to 20 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(40); // waits 0ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 15) { // goes from 150 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(40); // waits 15ms for the servo to reach the position
}}
Videos:



Comments
Post a Comment