top of page
Search
jjinghui21

Arduino Programming

Hello lads welcome to my blog *+.^!


There are 4 tasks that will be explained in this page:

1. Input devices:

a. Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

b. Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE


2. Output devices:

a. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)​

b. Include the pushbutton on the MakerUno board to start/stop part 2.a. above


For each of the tasks, I will describe:

1. The program/code that I have used and explanation of the code. The code is in writable format (not an image).

2. The sources/references that I used to write the code/program.

3. The problems I encountered and how I fixed them.

4. The evidence that the code/program worked in the form of video of the executed program/code.

Finally, I will describe:

5. 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

Code/program in writeable format

Explanation of the code

​int sensorPin = A0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin,OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delay(sensorValue); digitalWrite(ledPin, LOW); delay(sensorValue); }

  • ​int tells us where the sensorPin, ledPin and sensorValue is initialized to. for example, sensorPin is initiallized to A0.

  • under void setup() the identities of each pin is stated

  • pinMode(ledPin, OUTPUT) is used to declare that led on Pin13 will be an output.

  • void loop() runs the code infinitely

  • sensorValue = analogRead(sensorPin) reads the input of A0 and writes into an integer variable, sensorValue.

  • To write a digital signal to pin 13, we will have to use digitalWrite()

  • HIGH/LOW after ledPin refers to the voltage level.

  • If it is HIGH, the LED will turn on vice versa

  • delay(sensorValue) changes the led frequency as the knob of the potentiometer turns.



Below are the problems I have encountered and how I fixed them.

By right, the LED should light up when it is dark. But for me, my LED turned off when it is dark despite me copying the code from the maker uno guide. I didn't fix this issue as it still function like how an LDR would.

Another problem I had was when I covered the LDR with my bare fingers, it did not turn off. So, I had to use the sleeves of my sweater as it was opaquer.


Below is the short video as the evidence that the code/program work.


Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE

Code/program in writeable format

Explanation of the code

​int LDR = A0; int ledPin = 13; int LDRvalue = 0; void setup() { pinMode(ledPin,OUTPUT); } void loop() { LDRvalue = analogRead(LDR); if(LDRvalue > 600) digitalWrite(ledPin, HIGH); else digitalWrite(ledPin, LOW); }

  • ​int tells us where the LDR, ledPin and LDRValue is initialized to. For example, ledPin is initialized to pin 13.

  • void setup() the identities of each pin is stated.

  • pinMode(ledPin, OUTPUT) is used to declare that led on Pin13 will be an output.

  • void loop() runs the code infinitely

  • LDRvalue = analogRead(LDR) reads the input of A0 and writes into an integer variable, LDRValue.

  • under else{} the action that is desired to be performed when the condition under if() is not fulfilled.

  • For the code, if LDRvalue is more than 600, pin13 will be high, else, it will be low.

  • This also means if there is light, the led will be turned on, and if it is dark, the led will turn on.

Below are the problems I have encountered and how I fixed them.

I did not know why my LED did not light up despite having assembled it according to the picture in the maker uno guide. Then I compared it to the picture, and I realised I used the other side of the bread board. I thought that the red line was on the outside for both sides therefore the orientation did not matter.



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)

Code/program in writeable format

Explanation of the code

​const int greenLedVehicle = 5;

const int yellowLedVehicle = 6;

const int redLedVehicle = 7;



void setup()

{

pinMode(greenLedVehicle, OUTPUT);

pinMode(yellowLedVehicle, OUTPUT);

pinMode(redLedVehicle, OUTPUT);



}

void loop()

{


{

digitalWrite(greenLedVehicle, LOW);

delay(2000);

digitalWrite(yellowLedVehicle, HIGH);

delay(2000);

digitalWrite(yellowLedVehicle, LOW);

digitalWrite(redLedVehicle, HIGH);

delay(1000);


digitalWrite(redLedVehicle, LOW);

digitalWrite(greenLedVehicle, HIGH);

delay(2000);

}

}

  • const int tells us where the LED is initialized to. for example, greenLedVehicle = 5 means that the green led is connected to pin 5.

  • under void setup() the identities of each pin is stated.

  • pinMode(greenLedVehicle, OUTPUT) is used to declare that greenLedVehicle will be an output. (same for the other 2 LEDs)

  • void loop() runs the code infinitely

  • digitalWrite() is used to write a digital signal

  • HIGH/LOW after the LEDs, refers to the voltage level.

  • If it is HIGH, the LED will turn on vice versa

  • delay() will allow us to set the duration of the LED to remain on/off

  • The numbers in the delay brackets will be in milliseconds

Below are the problems I have encountered and how I fixed them.

Unlike the previous 2 activities where there was an example I could copy off of in the maker uno guide. For this activity's code, I had to tweak one of the examples in the maker uno guide. To the left is the original code provided. it had 5 LEDs instead of 3 and had a push button included already. Therefore, I had to simplify the code in order to meet the criteria of the activity.


Changing the code was slightly challenging as I had gotten a few error messages while trying to delete some codes.




Below is the short video as the evidence that the code/program work.



Include pushbutton to start/stop the previous task

Code/program in writeable format

Explanation of the code

​const int greenLedVehicle = 5;

const int yellowLedVehicle = 6;

const int redLedVehicle = 7;


const int pushButton = 2;


void setup()

{

pinMode(pushButton, INPUT_PULLUP);

pinMode(greenLedVehicle, OUTPUT);

pinMode(yellowLedVehicle, OUTPUT);

pinMode(redLedVehicle, OUTPUT);



}

void loop()

{

if(digitalRead(pushButton) == LOW)

{

digitalWrite(greenLedVehicle, LOW);

delay(2000);

digitalWrite(yellowLedVehicle, HIGH);

delay(2000);

digitalWrite(yellowLedVehicle, LOW);

digitalWrite(redLedVehicle, HIGH);

delay(1000);


digitalWrite(redLedVehicle, LOW);

digitalWrite(greenLedVehicle, HIGH);

delay(2000);

}

}

The blue highlighted parts are added the the previous code in order to include a push button. Therefore i shall only explain the blue parts.

  • const in tells us that the pushButton will be initialized to pin 2.

  • under void setup() the identities of each pin is stated.

  • pinMode(pushButton, INPUT_PULLUP) declares than the push button will be an input.

  • (digitalRead(pushButton) == LOW) Low volatage means the button is pushed

  • when the push button is pressed, the yellow led will turn on for 2 seconds then turn off

  • followed by the red led turning on for 1sec then turning off

  • finally, the green led will turn on and will not turn off.

Below are the problems I have encountered and how I fixed them

I didnt really encounter much problems for this activity since it was the last one.


Below is the short video as the evidence that the code/program work.


 

Below are the hyperlink to the sources/references that I used to write the code/program for all the activities:


 

Learning Reflection on the overall Arduino Programming activities.

Arduino is not that difficult as I had expected. When faced with a long string of code, it is indeed overwhelming however, after breaking it down and understanding what the words mean, it is not so bad. This doesn't mean that the often occurrence of error messages popping up after verification still don't irks me. It has become one of my biggest pet peeves now.


The practical activity was fun. I got to team up with Gayathri to make a pegasus. I felt that we did a great job as we got to add other functions and colored the pegasus. We added music and 2 red LEDs, one on each wings. Both LEDs light up at the same time together to the beat of the music. I felt that we could have made the lights alternating instead so that it could be actual turn signals. But that is too ambitious for a time limit of 2 hours.






However, we did run into a crisis towards the end of the practical, our servo motor was slightly out of place which made our wings flap quite weakly. We were panicking and trying our best to hot glue it a back. But this time we had all the other wirings in the way >:((



Video of our pegasus :))))







9 views0 comments

Recent Posts

See All

Comments


bottom of page