Arduino Programming

 Arduino Documentation Blog Entry


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

  1. Input devices:

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

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

  1. Output devices:

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

  2. 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:

  1. 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 the code

void setup()

{

  pinMode(A0, INPUT);

  Serial.begin(9600);

}

 

void loop()

{

  Serial.println(analogRead(A0));

  delay(1000);

}



Analog pin A0 will be set as the input.

A serial communication will be established between the arduino and the computer.


The below action will loop infinitely.


The value from the analog pin A0 will be displayed.

There will be a pause of 1000 milliseconds.



Overall:

The value from analog pin A0 will be displayed on the laptop every 1 second for an infinite number of times.



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

https://youtu.be/yyG0koj9nNY



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

A problem that I faced was that the values shown on the serial monitor would start to fluctuate rapidly during start up. When the delay was 10 milliseconds, the values shown would always change. To fix this problem, I extended the delay to 1 second and waited for the value to stabilise and fluctuate around a value so that the rate of change in value is not so obvious.



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

 

void setup()

{

Serial.begin(9600);

pinMode(LED_BUILTIN,OUTPUT);

}

void loop()

{

LDRvalue = analogRead(A0);

Serial.println(LDRvalue) ;

if (LDRvalue > 600)

digitalWrite(LED_BUILTIN, HIGH);

else

digitalWrite(LED_BUILTIN, LOW);

}

 


A variable named “LDRvalue” of starting value 0 is created.



A serial communication will be established between the arduino and the computer.

Pin 13 (LED_BUILTIN)  will be set as the input.

The below action will loop infinitely


The analog signal from pin A0 and written into the integer variable “LDRvalue”. The value from the analog pin A0 will be displayed.

If the LDRvalue is more than 600, signal to pin 13 is high and it will on.

Else (LDRvalue less than 600), signal to pin 13 is low and it will off.


Overall:

The value from the LDR connected to pin A0 will be displayed. If the value is more than 600, pin 13 will light up. If the value is less than 600, pin 13 will be off. This loops infinitely.



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



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

I did not know how to connect the LDR to the arduino and make it display the value. I referred to the Maker Uno Edu Kit Module document to guide me through and I modified the code.


  1. 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;

 

void setup()

{

  pinMode(10, OUTPUT);

 

  digitalWrite(10, HIGH);

  delay(1000);

  digitalWrite(10, LOW);

  delay(1000);

}

 

void loop()

{

  for (Brightness = 0; Brightness <= 255; Brightness += 25.5) {

    analogWrite(10, Brightness);

    delay(500);

  }

  for (Brightness = 255; Brightness >= 0; Brightness -= 25.5) {

    analogWrite(10, Brightness);

    delay(500);

  }

}

A variable named “Brightness” of starting value 0 is created.



Pin 10 is set as an output.


Signal to pin 10 is high and it will on.

Wait 1000 milliseconds.

Signal to pin 10 is low and it will off.

Wait 1000 milliseconds.



The below action will loop infinitely.


When the value of brightness ≤ 255, increase by 25.5 each time.

Signal to pin 10 will be the value of the brightness variable. Wait 500 milliseconds.

When the value of brightness ≥ 0, decrease by 25.5 each time.

Signal to pin 10 will be the value of the brightness variable. Wait 500 milliseconds.


Overall:

At the start, the LED will light up for 1 seconds then switched off for another 1 seconds.


The LED will light up brighter by 10% every 0.5 seconds until it is at 100% brightness.

Then, The LED will fade by 10% every 0.5 seconds until it is at 0% brightness.

This happens an infinite number of times.



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

https://youtu.be/yyG0koj9nNY

https://youtu.be/X8dHbdhnGKY


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

The problem I have encountered is with the green LED. It did not light up as bright as the red and yellow when the 3 LEDs are connected in series. To fix this problem, I first checked if the green LED was working. I replaced it with another but got the same result so I tested the green LED alone to see if it works. It lights up brightly when it is alone. To make it light up brighter, I connected the red and green LED in parallel to decrease the overall resistance of the circuit. Hence, current flowing through the LEDs will increase and the LEDs will light up brighter.



  1. 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 writeable format

Explanation of the code

int Brightness = 0;

 

void setup()

{

  pinMode(2, INPUT_PULLUP);

  pinMode(10, OUTPUT);

}

 

void loop()

{

  int SensorVal = digitalRead(2);

  if (SensorVal == HIGH) {

   digitalWrite(10, LOW);

    }

  else {

    digitalWrite(10, HIGH);

    delay(1000);

    digitalWrite(10, LOW);

    delay(1000);

     for (Brightness = 0; Brightness <= 255; Brightness += 25.5) {

    analogWrite(10, Brightness);

    delay(500);

  }

  for (Brightness = 255; Brightness >= 0; Brightness -= 25.5) {

    analogWrite(10, Brightness);

    delay(500);

  }

  }

}


A variable named “Brightness” of starting value 0 is created.


Pin 2 is set as an input and the internal pull up resistor is enabled.

Pin 10 is set as an output.




It reads the input of pin 2 and writes into an integer variable, sensorVal.

If the sensorVal is high (button not pressed), signal to pin 10 is low and it will be off.

Or else,

Signal to pin 10 is high and it will on.

Wait 1000 milliseconds.

Signal to pin 10 is low and it will off.

Wait 1000 milliseconds.



When the value of brightness ≤ 255, increase by 25.5 each time.

Signal to pin 10 will be the value of the brightness variable. Wait 500 milliseconds.

When the value of brightness ≥ 0, decrease by 25.5 each time.

Signal to pin 10 will be the value of the brightness variable. Wait 500 milliseconds.


Overall:

At the start, nothing will happen.


When the button is pressed, the LED will light up for 1 seconds then switched off for another 1 seconds. Next, the LED will light up brighter by 10% every 0.5 seconds until it is at 100% brightness.

Then, The LED will fade by 10% every 0.5 seconds until it is at 0% brightness and stops here until the button is pressed again.




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

https://youtu.be/yyG0koj9nNY

https://youtu.be/X8dHbdhnGKY


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

The problem I have encountered is that the LEDs flashed and faded continuously without stopping once the button was pressed. To fix this problem, I compared and contrasted the code written for this with the one for the previous task. The difference lies between a break in the code between the one for flashing and fading. Without the break in between the 2 sets of codes, the LEDs will loop infinitely between flashing and fading.


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



Below is my Learning Reflection on the overall Arduino Programming activities.

Arduino Programming is challenging. It is something completely new to me and I had a hard time trying to understand what was going on at the start. The pre-practical allowed me to have a better understanding of what the different line of codes mean as they guide us through and taught us what each function do. For the practical, I made use of what I learnt from the pre-practical and implemented it to our Pegasus. The two functions of our Pegasus was to play a tune🎵 and flap its wing through the use of a servo. The difficulty we faced was trying to make the two functions run simultaneously. I asked Dr Noel if it was possible and he said it is but instead of using delay(), we need to use millis(). That was not taught to us so I tried googling but was not able to understand anything😧. After learning about the existence of this code and what it can do, I will look up on it and learn how to use it during the holidays as I believe that it will be very useful for the tea 🍵maker my group is making.



The activity on input and output devices was also challenging. Luckily, we have TinkerCad which I used for writing some of the programs. TinkerCad has a very useful feature which allows us to do block programming. In Secondary school, I have learnt to program the Micro:bit using blocks. Hence, it was very reassuring😌 when I saw that we can just drag and drop the blocks and convert it to text. However, I believe there are some limitations. For example, I find it challenging to use the blocks when working on the output device due to the inclusion of variables. I also can’t make use of the button that is available on the actual Arduino board in TinkerCad.

There were also some challenges that I faced while programming the Arduino. For example, the Arduino did not do as intended when I use the pushbutton to start the action by the LED. I compared and contrasted to find the difference between this code and the previous code to fix this problem. Another challenge was also the connection of the parts. To tackle this problem, I used the different resources provided such as the Youtube videos and the document on Maker Uno. The entire process was very tiring but I persevered and got the job done and felt proud when the Arduino performed as intended.