Arduino Programming

 Arduino Documentation Blog Entry

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 an 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 a 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.

1. Below are the code/program I have used and the explanation of the code.


Code/program in writeable format

Explanation of code

int TempValue=0;

void setup() {

 // put yoursetup code here,to run once:

 Serial.begin(9600);

}

 

void loop()  {

 // put your main code here, to run repeatedly:

 TempValue = analogRead (A0);

 Serial.println(TempValue);

 delay(1000);

}


int TempValue=0;

it is a variable that is created before the setup(). The variable ‘TempValue’ is an integer which initially set at number 0. 


Serial.begin(9600);

helps establish serial communication between Arduino Board and other devices. 


TempValue = analogRead (A0);

means that it reads the TempValue from analog pin A0


Serial.println(TempValue);

sends ‘TempValue’ from the arduino to the connected computer. The ‘TempValue’ read will be in the serial monitor and print out line by line.


delay(1000);

there is a pause of 1000 milliseconds = 1sec




2. Below are the hyperlink to the sources/references that I used to write the code/program.

 

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

When I was trying to show whether the potentiometer works, i realised when I turned the potentiometer, the TempValue that was measured shown in the serial monitor did not change. Initially, I thought that something was wrong with the code that's why there are no changes observed in TempValue when the potentiometer was turned. 

But after a long time of navigating the codes around, the potentiometer still did not work. So I went to check the physical components and realised that the potentiometer was quite loose. I tried to straighten the connectors of the potentiometer and push it in further. Finally, there were changes in readings when I turn the potentiometer. So all in all, it was just the connectors of the potentiometer weren’t connected to the breadboard causing no differences in readings when turned.


4. 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 = 0;

void setup() {

 // put your setup code here, to run once:

 Serial.begin(9600);

 pinMode(13,OUTPUT);

 pinMode(A0,INPUT);

 

}

 

void loop() {

 // put your main code here, to run repeatedly:

 LDR = analogRead(A0); //0-1023

  if(LDR < 25)

 {

   digitalWrite(13,HIGH);

 }

 else

 {

   digitalWrite(13,LOW);

 }

 Serial.println(LDR);

 delay(0);

 }



int LDR=0; 

it is a variable that is created before the setup(). The variable ‘LDR’ is an integer which initially set at number 0. 


Serial.begin(9600);  

helps establish serial communication between Arduino Board and other devices. 


pinMode(13, OUTPUT); 

means that we are going to set PIN 13 as the output 


pinMode(A0, INPUT); 

means that we are going to set analog pin A0 as the input 


LDR = analogRead(A0);

means that it reads the LDR value from analog pin A0

 

if(LDR < 25)

{digitalWrite(13,HIGH;} 

means that if the LDR value is less than 25, pin 13 will light up.

 

else {digitalWrite(13,LOW);}

means that pin 13 will not light up when LDR>25

 

Serial.println(LDR); 

sends ‘LDR’ measured value from the arduino to the connected computer. The ‘LDR’ measured value read will be in the serial monitor and print out line by line.

 

delay(0); 

means there is no pause in between actions

 


2. Below are the hyperlink to the sources/references that I used to write the code/program.

 

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

One of the biggest problem that I faced was that my LED did not react to the different intensities of light. For example, when I cover LDR with my finger, the LED should light up because the purpose of the LDR is to measure the light intensity and change the resistance when the light level changes allowing the LED to light up when the surrounding light level is low. However, when finished setting up the LED, LDR, and the code, the LED remains lit up when I cover the LDR. Hence, I thought that my arrangement of the components on the breadboard are wrong. So I went on to youtube to watch the different arrangements of the components and changed the code many times, but I still didn’t get the result that I want. 

After navigating the code and my arrangement of the components for a long time, I realised that the value of analogRead for the LED to light up when the surrounding light intensity is low was wrongly keyed in. eg. {if (LDR<600);  digitalWrite (13, HIGH); } this means that when the light intensity measured is less than 600, the LED will light up. 

So one thing I realised is that I copied the code blindly from youtube without realising that my room light intensity was at 30+. Even if I shine extra light onto the LDR, the MAXIMUM light intensity will only reach 160. So no matter how much light I shine or off my room light, the light intensity will still be less than 600. This means that my LED will remain lit up even if I shine light onto it and that is why there are no changes to the LED even when there is a change in the light level. 

So after realising that, I look at the serial monitor and adjust the analogRead value according to my room light intensity (which is at 30+). Therefore the new code will be [if (LDR<30);  {digitalWrite (13,HIGH);} else {digitalWrite(13,LOW)} ]. 

Overall, it was just the analogRead value was written wrongly.

 

4. 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)

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 brightness = 0;

int buttonState = 0;

void setup()

{

pinMode(11, OUTPUT);

pinMode(10, OUTPUT);

pinMode(9, OUTPUT);

}

void loop()

{

  for (brightness = 0; brightness <= 255; brightness += 5){

  analogWrite(11, brightness);

  delay(30);

  }

  for (brightness = 255; brightness >= 0; brightness -= 5){

  analogWrite(11, brightness);

  delay(30);

  }

  for (brightness = 0; brightness <= 255; brightness += 5){

  analogWrite(10, brightness);

  delay(30);

  }

  for (brightness = 255; brightness >= 0; brightness -= 5){

  analogWrite(10, brightness);

  delay(30);

  }

  for (brightness = 0; brightness <= 255; brightness += 5){

  analogWrite(9, brightness);

  delay(30);

  }

  for (brightness = 255; brightness >= 0; brightness -= 5){

  analogWrite(9, brightness);

  delay(30);

  }

}

 

int brightness=0; 

it is a variable that is created before the setup(). The variable ‘brightness’ is an integer which initially set at number 0. 


int buttonState=0; 

it is a variable that is created before the setup(). The variable ‘buttonState’ is an integer which initially set at number 0. 


for (brightness = 0; brightness <= 255; brightness += 5) {analogWrite(11, brightness); delay(30); } : 

means that when the (brightness=0) brightness was initially at 0, [analogWrite(11,brightness)] pin 11 will gradually (brightness += 5) increase the brightness 5 by 5 (brightness<=255) from 0 to 255 and [delay(30)] pause for 30milliseconds. 

 

for (brightness = 255; brightness >= 0; brightness -= 5) {analogWrite(11, brightness);

delay(30);}

means that when (brightness=255) the brightness was initially at 255, [analogWrite(11,brightness)] pin 11 will (brightness -= 5) gradually decrease the brightness 5 by 5 (brightness>=0) from the initial state 255 to 0 and [delay(30)] pause for 30milliseconds.

 

*copy and paste the same 2 codes above and change the pin number to 9 & 10

 




 

 

2. Below are the hyperlink to the sources/references that I used to write the code/program.

 

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

One problem that I’ve faced was when I was trying to make the LED fade individually,, the first LED did light up slowly, but it didn't fade away. Instead, it blinks off and straight up lights up the next LED. 

After that, I realised that I have included the wrong code. Instead of this,  [for (brightness = 255; brightness >= 0; brightness -= 5)] i wrote  [digitalWrite(11,LOW);]. By doing so, it actually only allows the LED to blink off instead of fading away gradually.

So after knowing my mistake, I've added this code  [for (brightness = 255; brightness >= 0; brightness -= 5)] to every LED pin instead of [digitalWrite(11,LOW);].

 

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




Output devices: Include pushbutton to start/stop the previous task

1. Below are the code/program I have used and the explanation of the code.

Code/program in a writeable format

Explanation of the code

int brightness = 0;

 

int buttonState = 0;

 

void setup()

{

 Serial.begin(9600);

 pinMode(2, INPUT_PULLUP);

 pinMode(11, OUTPUT);

 pinMode(10, OUTPUT);

 pinMode(9, OUTPUT);

}

 

void loop()

{

 //read the pushbutton value into a variable

 int sensorVal = digitalRead(2);

 //print out the value of the pushbutton

 Serial.println(sensorVal);

 

 

   if (sensorVal == HIGH)

   {

    digitalWrite(11, LOW);

    digitalWrite(10, LOW);

    digitalWrite(9, LOW);

}

   else

   {

     for (brightness = 0; brightness <= 255; brightness += 5){

     analogWrite(11, brightness);

     delay(30);

     }

     for (brightness = 255; brightness >= 0; brightness -= 5){

     analogWrite(11, brightness);

     delay(30);

     }

     for (brightness = 0; brightness <= 255; brightness += 5){

     analogWrite(10, brightness);

     delay(30);

     }

     for (brightness = 255; brightness >= 0; brightness -= 5){

     analogWrite(10, brightness);

     delay(30);

     }

     for (brightness = 0; brightness <= 255; brightness += 5){

     analogWrite(9, brightness);

     delay(30);

     }

     for (brightness = 255; brightness >= 0; brightness -= 5){

     analogWrite(9, brightness);

     delay(30);

     }

   }

}

 

 

 

 

 

 


int brightness=0; 

it is a variable that is created before the setup(). The variable ‘brightness’ is an integer which initially set at number 0. 


int buttonState=0; 

it is a variable that is created before the setup(). The variable ‘buttonState’ is an integer which initially set at number 0. 


 Serial.begin(9600);  

helps establish serial communication between Arduino Board and other devices. 


pinMode(2, INPUT_PULLUP); 

This is where we declare that PIN 2 is an INPUT and enable the internal pull up resistor. So, by default, PIN2 status will always be HIGH

 

pinMode(11, OUTPUT);

pinMode(10, OUTPUT);

pinMode(9, OUTPUT);

These indicate that the pin numbers 11,10 and 9 are set as the output

 

int sensorVal = digitalRead(2);

It reads the input of PIN2 and writes it into an INTEGER variable, sensorVal. A press will change the status to LOW and releasing it will change the status to HIGH.

 

Serial.println(sensorVal);

sends ‘sensorVal’ status from the arduino to the connected computer. The ‘sensorVal’ read will be in the serial monitor and print out line by line.

 

if (sensorVal == HIGH)

{digitalWrite(11, LOW);

digitalWrite(10, LOW);

digitalWrite(9, LOW);}

IF sensorVal status is HIGH, PIN11, 10, and 9 will be LOW. In simple English, if the button is not pressed, the light will be turned OFF 

 

else 

{for (brightness = 0; brightness <= 255; brightness += 5) {analogWrite(11, brightness); delay(30);}

for (brightness = 255; brightness >= 0; brightness -= 5){analogWrite(11, brightness);delay(30);  }

else if the button is pressed, the LED will gradually start to increase the brightness, pause for 30 milliseconds and gradually decrease the brightness of the LED, pause for another 30 milliseconds 

 

*The second LED PIN 10 will light up and fade away, and followed by PIN 9.

 

 

 

 

2. Below are the hyperlink to the sources/references that I used to write the code/program.

https://www.youtube.com/watch?v=PC15jBx2UxI&t=160s 

 

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

One problem that I’ve faced is that I forgot to keyed in this sentence [int sensorVal = digitalRead(2);] even though i wrote [pinMode(2, INPUT_PULLUP);] in my setup. If this sentence [int sensorVal = digitalRead(2);] wasn’t written even though this  [pinMode(2, INPUT_PULLUP);] was written, the button would not work as it comes as a set. It took me quite a while to realise that I missed out on that step. How I realised was I went back to the brightspace and relook back at the resources on the ‘programmable button’ challenge that was given to us and realised that that step was missing. So I went to make the adjustments and the button worked.

 

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, the Arduino programming activities that I went through were quite fun after getting the hang of how to code something to make the Arduino react according. 

Initially, I didn't really like coding as I have no idea how the whole arduino listens to the code and reacts to it. Honestly saying, coding seems like a foreign language to me and I have to learn and understand it from scratch. Which is something I really hate as I don't really have the patience and time to learn a new language. I agree that coding really needs more practice and as you practice, you will learn from the mistakes and try to find ways to navigate the code to make it right. This is actually how I learned and get a hang of it. 


So I actually took a very long time to complete the arduino pre-practical activity that we have to do before our practical. I didn’t enjoy it. 


However, the practical was surprisingly fun and engaging. I didn’t hate it but instead, I enjoyed making the pegasus’ wings flap, adding music to the Arduino, and decorating the pegasus to make it aesthetically pleasing. Isabella and I got an idea to add ‘my little pony’ theme song 👻 to it. We have also designed our pegasus according to one of the characters, twilight sparkle in ‘my little pony’. We didn't design exactly to how it looks, but instead, the color we used was inspired from there.

A Picture of our pegasus.

I realised one of the biggest problem that Isabella and I faced was that we couldn't make our wings flap vigorously. The flapping was not very obvious. We tried different ways to position our servo but still, the wings weren't flapping very vigorously 🙄. 


Another problem that we faced was that when we include LED on 2 sides as the eyes of the pegasus, only one side of the LED blinks. The other side wasn't blinking at all. We check the codes that we wrote, and it was all correct, and nothing was missing. We also thought that the LED blow up so we went to change it to a different LED and yet it still didn't blink. we have also checked the connection between the wire to make sure that they are connected. Everything was connected properly and yet our LED still didn't light up. So at one point in time, we just say that we are making the pegusus wink 😉. Suddenly, when we were assembling our pegasus together, the LED that we gave up on started blinking. Till today, I still have no answers to how it suddenly blinked 😕.


So here is a video of how our pegasus work 🤡:


After presenting our pegasus to Dr Noel, he taught us how to present our model in an appropriate manner which is to hide all the cables that are visible. To hide the cable, we can make a box underneath the pegasus and hide our Arduino, breadboard, and wires into that box and cut some holes to allow the connected wires to pass through.


Overall, I enjoyed this practical a lot and it was indeed an enjoyable and enriching session that I had.








Comments