Arduino Programming
Arduino Documentation Blog Entry
There are 4 tasks that will be explained in this page:
Input devices:
Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.
Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE
Output devices:
Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
Include the pushbutton on the MakerUno board to start/stop part 2.a. above
For each of the tasks, I will describe:
The program/code that I have used and explanation of the code. The code is in writable format (not an image).
The sources/references that I used to write the code/program.
The problems I encountered and how I fixed them.
The evidence that the code/program worked in the form of video of the executed program/code.
Finally, I will describe:
My Learning reflection on the overall Arduino programming activities.
Input devices: Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.
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() { pinMode(A0, INPUT); pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { // read the value from the sensor sensorValue = analogRead(A0); Serial.println(sensorValue); // turn the LED on digitalWrite(13, HIGH); // pause the program for <sensorValue> milliseconds delay(sensorValue); // turn the LED off digitalWrite(LED_BUILTIN, LOW); // pause the program for <sensorValue> milliseconds delay(sensorValue); } | int sensorValue- integer for sensor value set to 0 void setup()- Setting up pin identity; A0 is set as input and 13 is set as output (the setup function runs once when you press reset or power the board) Serial.begin(9600)-establishes serial communication between your Arduino board and another device. void loop()- run the code infinitely refer to notes in code for the meaning of each line |
Below are the hyperlink to the sources/references that I used to write the code/program.
-
Below are the problems I have encountered and how I fixed them.
I did not know how to build the potential meter on the board and hence I watch a YouTube video to learn it
( you might think why then did I not include it in part 2 hyperlink, that is because part 2 is for code not putting the potential meter onto the board)
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:
Below are the code/program I have used and the explanation of the code.
Code/program in writeable format | Explanation of the code |
int light;
void setup() { Serial.begin(9600); }
void loop() { light= analogRead(A0);
if(light<50){ digitalWrite(13, LOW);
}else{ digitalWrite(13, HIGH); }
Serial.println(light); delay(0); } | int light- integer of light void setup()- the setup function runs once when you press reset or power the board Serial.begin(9600)-establishes serial communication between your Arduino board and another device. void loop()- run the code infinitely first line of code says that the light value will be the input value of A0, second line says that if the input is less than 50, the LED will turn off, or else, the LED will be on third line of code print out the integer value of light last line off code is redundant |
Below are the hyperlink to the sources/references that I used to write the code/program.
-
Below are the problems I have encountered and how I fixed them.
I did not know how to build the LDR on the board and hence I watch a YouTube video to learn it
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(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT);
}
void loop() { digitalWrite(13, HIGH); digitalWrite(12, LOW); digitalWrite(11, LOW); delay(1000); digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW); delay(1000); digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH); delay(1000); } | void setup() - Setting up pin identity; 13, 12 and 11 is set as output void loop()- run the code infinitely turn on 13 while turning off 12 and 11 wait a second turn on 12 while turning off 13 and 11 wait a second turn on 11 while turning off 13 and 12 wait a second |
Below are the hyperlink to the sources/references that I used to write the code/program.
-
Below are the problems I have encountered and how I fixed them.
There was no issue, this part was really easy
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 |
void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT);
}
void loop() { int sensorVal = digitalRead(2); Serial.println(sensorVal);
if (sensorVal == HIGH) { digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
} else { digitalWrite(13, HIGH); digitalWrite(12, LOW); digitalWrite(11, LOW); delay(1000); digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW); delay(1000); digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH); delay(1000); } } | void setup() - Setting up pin identity; 13, 12 and 11 is set as output and set 2 as input and enable internal pullup Serial.begin(9600)- establish connection void loop() - run the code infinitely int sensorVal = digitalRead(2)- It reads the input of PIN2 and writes into a integer variable, sensorVal A press will be change the status as low and releasing it will change the status as high. If sensor value is high, turn off all LED If sensor value is low, turn on 13 while turning off 12 and 11 wait a second turn on 12 while turning off 13 and 11 wait a second turn on 11 while turning off 13 and 12 wait a second |
Below are the hyperlink to the sources/references that I used to write the code/program.
-
Below are the problems I have encountered and how I fixed them.
We could not get the LED to light up in order. This was due to me accidentally connecting the led on the board causing it to be in series which cause all the LED to light up at once, hence I space out the LED to solve the issue
Below is the short video as the evidence that the code/program work.
Overall, programming is quite tiring and frustrating if you could not get the code to work, but very satisfying and fulfilling once you got it to work. One example I can think on the spot is during the practical for arduino , I wanted to simultaneously get my code to move a servo, turn on and off LED and play sound, but I was not able to play the sound simultaneously, even after spending slightly more than an hour altering the code. Therefore, I decided to just scratch off the music and only kept the flickering LED and servo, which only took 45 minutes to code. Hence, as we all can see, I could have make better use of the time and instead of trying to beat the final boss, I should have just farm the peons to gain experience point. Basically, I should have spent the remainder of the time making the pegasus look nice. However, I did not do that because this practical is on arduino programming, not arts and craft, hence I really wanted to get the code to work, even if it means not making the pegasus look aesthetically pleasing.
Comments
Post a Comment