ARDUINO PROGRAMMING

 

Hey guys! Weclome back to my third blog entry😎In this blog, I will be documenting my Arduino programming activities and what I have learnt from them😼Sit back, relax, and read on!

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 writable format

Code 

Explanation of code 

// C++ code 

//

int sensorValue = 0;


void setup ( )

{

     pinMode (A0, INPUT);

     pinMode (LED_BUILTIN, OUTPUT);

}


void loop ( ) 

{

  // read value from sensor

  sensorValue = analogRead (A0) ;

  // turn the LED on

  digitialWrite (LED_BUILTIN, HIGH) ;

  // pause the program for <sensorValue> milliseconds

  delay <sensorValue> ;  // Wait for sensorValue millisecond(s)

  // turn the LED off

  digitalWrite (LED_BUILTIN, LOW) ;

  delay <sensorValue> ; // Wait for sensorValue millisecond(s)

// C++ code 

This is a comment to explain what the program is doing. In this case, it is saying that the program is using C++ programming language

int sensorValue = 0;

The program will read the sensorValue,  which in this case is the values of the potentiometer

void setup ( )

We are setting up a connection between the laptop and program

pinMode (A0, INPUT);


This means that analog pin A0 is the input (which is the values of the potentiometer)

pinMode (LED_BUILTIN, OUTPUT);

}

The LED builtin is the output (the red LED). We can also use pin13 as output since the LED is connected to pin13. 


void loop ( ) 

This means that we want the code to run on infinite loop

sensorValue = analogRead (A0) 

This means that the value of the potentiometer is being input to A0

digitialWrite (LED_BUILTIN, HIGH) ;

The LED will turn ON when the value of the potentiometer is HIGH. 

delay <sensorValue> ;  // Wait for sensorValue millisecond(s)


This tells the program to wait for <sensorValue> milliseconds 

digitalWrite (LED_BUILTIN, LOW) ;

The LED will turn OFF when the value of the potentiometer is LOW. 

delay <sensorValue> ;

This tells the program to wait for <sensorValue> milliseconds 

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

          https://www.youtube.com/watch?v=-EDYMQ9lczA

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

I had trouble understanding  what the codes meant and why they were placed in a certain order😓I went back to the Arduino learning package to recap on what the different commands meant. 

To help me further understand what the different commands meant, I practiced changing the 'delay' values to see if I could play around with it. 

Initially, when I ran the program, I realised that my LED was not blinking, and instead only pin13 was blinking. I realised later on that I had inserted my LED the wrong way. The long end of the LED (anode, positive side) should be placed beside the wire connected to pin13, and the short end (cathode, negative side) should be placed beside the resistor. 

shorter leg of LED=cathode 

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

This is with a delay of 200ms with LED_BUILTIN, HIGH, and a delay of 500ms when LED_BUILTIN, LOW: 


When turning the potentiometer with (sensorValue): 


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 writable format

Code

Explanation of code

// C++ code

//

int LDR = 0;


void setup()

{

  pinMode(A0, INPUT);

  Serial.begin(9600);

  pinMode(9, OUTPUT);

}


void loop()

{

  LDR = analogRead(A0);

  Serial.println(LDR);

  analogWrite(9, map(LDR, 0, 1023, 255));

  delay(100); // Wait for 100 millisecond(s)

}


int LDR = 0;

here, a variable LDR is being set


void setup()

we are setting up a connection between the laptop and program 

pinMode(A0, INPUT);

we are setting pin A0 to be the input 

Serial.begin(9600);

we are establishing a connection between the laptop and program 

pinMode(9, OUTPUT);

we are setting pin 9 to be the output

void loop()

This instructs the program to run on infinite loop 

LDR = analogRead(A0);

analogRead reads the specified value from the analog pin (A0) 

Serial.println(LDR);

The value of the LDR is being displayed 

analogWrite(9, map(LDR, 0, 1023, 255));


Writes an analog wave to a pin. In this case, it is pin 9. 

The map function is used to change one range of values to another range of values. 

Here, the range is 0 to 255. 

delay(100); // Wait for 100 millisecond(s)

}


This tells the program to wait for 100 milliseconds. 

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

https://www.youtube.com/watch?v=99dUAe9uqSI

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

When I first uploaded the code, the LED did not light up. I was confused and tried checking the code to see if there was anything wrong with it. However, it seemed fine. I thought that it was due to how much light was exposed to the LDR. I tried to change my environment to a much more dim background, but nothing changed. 

I later found out that it was because of  the +ve and -ve sign on the breadboard. When the wire was connected to the -ve side, the LED did not light up. This was because the -ve side is connected to ground, and there is no voltage. The resistance is higher and hence the LED did not light up. 

On the other hand, the LED lit up when connected to the +ve side. The +ve side is connected to 5V, meaning that its resistance is lower. With a lower resistance, the LED was able to light up. 

In a background with brighter lighting, the LDR's resistance decreases. Hence, the LED will be brighter. Whereas with a dimmer lighting, the LDR's resistance increases. Hence, the LED will be dimmer.

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

This is the layout of my arduino and breadboard for the video above:

 

 


 When the amount of light is high, the LED will turn on as its resistance decreases. 



 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.

          For fading with 1 LED:

Code/program in writeable format 

Code 

Explanation of the code 

int brightness = 0;

void setup()

{

 pinMode(9,OUTPUT)

}

void loop()

{

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

    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

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

    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

}


int brightness = 0;

A variable, brightness, is being set. “int” is used to store a value between -32768 and 32767

void setup()

{

 pinMode(9,OUTPUT)

}

We are setting a connection between the program


Pin 9 is set as the output. 

void loop()

This is to instruct the program to run on infinite loop 

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

 

brightness =0; brightness <=255; means that the LED is turning ON (from 0 to 255)


The program takes 2 instructions, one whereby the brightness increases from 0 to 255 (fading up) , and the other is pin number 9. 

The program can only take functions up to 255, hence 255 is the maximum (when the LED is ON), and the minimum is 0 (when the LED is OFF)

  analogWrite(9, brightness);

analogWrite() is used to light a LED at varying brightnesses

delay(30); // Wait for 30 millisecond(s)


To instruct the program to wait for 30 milliseconds before the LED fades UP again (from 0 to 255)

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

This time, we are making the LED fade OUT, from 255 to 0. 


This instructs the program to turn OFF the LED 

analogWrite(9, brightness);

analogWrite() is used to light a LED at varying brightnesses

delay(30); // Wait for 30 millisecond(s)

To instruct the program to wait for 30 milliseconds before the LED fades OUT  again (from 255 to 0)


 

 For fading with 3 LEDs: 

Code/program in writeable format

Code

Explanation of the code 

int brightness = 0;


void setup()


{

pinMode(6, OUTPUT); 

pinMode(9, OUTPUT);

pinMode(11, OUTPUT);


}

void loop()

{

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

    analogWrite(6, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

  {

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

    analogWrite(6, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

  }

{

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

    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

}

}

{

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

    analogWrite(9, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

}

 {

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

    analogWrite(11, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

}

{

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

    analogWrite(11, brightness);

    delay(30); // Wait for 30 millisecond(s)

  }

}



int brightness = 0;

A variable, brightness, is being set. “int” is used to store a value between -32768 and 32767

void setup()


{

pinMode(6, OUTPUT); 

pinMode(9, OUTPUT);

pinMode(11, OUTPUT);


}


We are setting a connectection between the program


Here, we are setting pins 6, 9 and 11 to be the output. The LEDs will be connected to these pins. 

void loop()


This is to instruct the program to run on infinite loop 

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


The program takes 2 instructions, one whereby the brightness increases from 0 to 255 (fading up) , and the other is pin number 6. 

The program can only take functions up to 255, hence 255 is the maximum (when the LED is ON), and the minimum is 0 (when the LED is OFF)


(this is repeated for the other 2 pins, pin 9 and 11)

analogWrite(6, brightness);

analogWrite() is used to light a LED at varying brightnesses.

In this case, we are using the LED connected to pin 6 (yellow LED)

To interface the other 2 LEDs, we just have to replace ‘6’ with either ‘9’ or ‘11’ when duplicating this code. 

delay(30); // Wait for 30 millisecond(s)

To instruct the program to wait for 30 milliseconds before the LED fades UP again (from 0 to 255)

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

This time, we are making the LED fade OUT, from 255 to 0. 


This instructs the program to turn OFF the LED 

analogWrite(6, brightness);

analogWrite() is used to light a LED at varying brightnesses

delay(30); // Wait for 30 millisecond(s)

To instruct the program to wait for 30 milliseconds before the LED fades OUT  again (from 255 to 0)

(the rest of the code is repeated for pins 9 and 11)


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

https://www.youtube.com/watch?v=X8dHbdhnGKY

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

I did not face much problems when trying to interface the 3 LEDs. But it was quite fun to challenge myself to modify the code compared to what was provided in the video. 

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

For fading:

First, the video with 1 LED: 



This is the set-up for 1 LED:


Second, I modified the set-up and code from the video to interface 3 LEDs: 


This is the set-up for 3 LEDs: 



 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 writable format

Code 

Explanation of code 

void setup() {

  //start serial connection

  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);

  //configure pin 6, 9, 11 as an output and enable the internal pull-up resistor

  pinMode(6, OUTPUT);

  pinMode(9, OUTPUT);

  pinMode(11, 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(6, LOW);

   digitalWrite(9, LOW);

   digitalWrite(11, LOW);

  } else {

   digitalWrite(6, HIGH);

   digitalWrite(9, LOW);

   digitalWrite(11, LOW);

   delay(700);

   digitalWrite(6, LOW);

   digitalWrite(9, HIGH);

   digitalWrite(11, LOW);

   delay(700);

   digitalWrite(6, LOW);

   digitalWrite(9, LOW);

   digitalWrite(11, HIGH);

   delay(700);



   

  }

}



void setup() {

We are setting a connection with the program

Serial.begin(9600);


We are establishing a connection with the program

pinMode(2, INPUT_PULLUP);

  //configure pin 6, 9, 11 as an output and enable the internal pull-up resistor

  pinMode(6, OUTPUT);

  pinMode(9, OUTPUT);

  pinMode(11, OUTPUT);


}


We are setting the 3 pins connected to the LEDs, pin 6, 9, and 11 to be the OUTPUT. 


Pin 2 is selected as an INPUT for the pushbutton. 


void loop () {

This tells the program to run on infinite loop when the pushbutton is pushed 

digitalRead(2);

This tells the program to read the logic state at pin 2. The input signal of pin 2 (the pushbutton), is being read and the signal is sent to sensorVal

Serial.println(sensorVal);

The value of sensorVal is being displayed

if (sensorVal == HIGH) {

   digitalWrite(6, LOW);

   digitalWrite(9, LOW);

   digitalWrite(11, LOW);



If sensorVal is HIGH, then the output of pins 6, 9, and 13 will be LOW, which means that these pins will NOT light up

} else {


Else, it will be HIGH. When the pushbutton is pressed, the LED will turn ON.


digitalWrite(6, HIGH);

   digitalWrite(9, LOW);

   digitalWrite(11, LOW);

   delay(700);


Setting pin 6 to HIGH means that the LED connected to pin 6 (yellow colour LED) will be turned on, while the rest of the LED is turned off, since pins 9 and 11 are set to LOW.


It will be turned ON for 700 milliseconds before turning OFF.

digitalWrite(6, LOW);

   digitalWrite(9, HIGH);

   digitalWrite(11, LOW);

   delay(700);


Setting pin 9 to HIGH means that the LED connected to pin 9 (red colour LED) will be turned on, while the rest of the LEDs are turned off, since pins 6 and 11 are set to LOW.


It will be turned ON for 700 milliseconds before turning OFF.

digitalWrite(6, LOW);

   digitalWrite(9, LOW);

   digitalWrite(11, HIGH);

   delay(700);



Setting pin 11 to HIGH means that the LED connected to pin 11 (green colour LED) will be turned ON, while the rest of the LEDs are turned off, since pins 6 and 9 are set to LOW.


It will be turned ON for 700 milliseconds before turning OFF.

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 were not much problems I faced when writing the code. I was able to  use the code for the pushbutton (from the pre-practical exercises) and modify it so that the LEDs will light up one by one.

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

Video: 


And this is the set up I used:


 

 

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

Despite doing coding for the past 5(?) weeks, I still do not like it😒 It is hard for me to understand how the code works, and most of the time I will tend to forget what the different commands mean. I will have to frequently go back to my notes and refer to their meaning. 

Although coding has been extremely frustrating so far, I have to admit that I feel a sense of accomplishment and satisfaction whenever the code I have programmed works. Especially when I had to modify the code from the different youtube videos, I had to do some trial and error since I would miss out some symbols when I checked the code before uploading it. 

Overall, most of the Arduino Programming activites were okay, but the one activity that made me really frustrated was the practical activity. During the practical, we had to program the wings of the pegasus to flap up and down, as well as play music from the Arduino board at the same time. 

En Ting and I spent some time assembling the pegasus and connecting the servo to the pegasus. It was tricky when trying to imagine the orientation of the wings and how the servo should be placed. Nonetheless, we managed to figure it out, though there was room for improvement. 

This is the video of our pegasus' wings flapping💨:


The pre-practical preparation came in handy as we were able to use the same code for the servo as the pre-practical. We only changed the angle and the delay of the servo to make the wings flap faster. 

Next, we wanted to include music with the flapping of the wings. We could do so by using the code provided in the pre-practical package. However, I think En Ting and I can agree that we were just not cut out for coding😖We spent half the practical trying to combine the code with servo + together with the code with the music. Although I managed to write a code with all the notes in the song we wanted to play, we could not find a way to combine the 2😭

This led to us only being able to make the pegasus' wings flap  using the servo. I think we could have tried better to make it work, but by then our brains were fried and we were unable to think anymore😪

I think with enough practice, I will get better at coding. I may even develop a liking towards it, but I do not see that happening soon..lol. Anyways, it was fun working with my group, we were able to share the song that Kieth managed to program as well, haha. I hope we will be able to learn more from each other in time to come 😌

Picture with my group! (and our pegasus) 🎠





And this is the code of the song we wanted to include: (its very long) 

#include "pitches.h"

int const TEMPO = 1200;

 

int melody[] = {

  N_D3, N_D3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_C3, N_C3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_B2, N_B2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_AS2, N_AS2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_D3, N_D3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_C3, N_C3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_B2, N_B2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_AS2, N_AS2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_D4, N_D4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_C4, N_C4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_B3, N_B3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_AS3, N_AS3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_D4, N_D4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_C4, N_C4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_B3, N_B3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_AS3, N_AS3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_F4, N_F4, N_F4, N_F4, N_F4, N_D4, N_D4, N_D4, N_F4, N_F4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, 0, N_F4, N_F4, N_F4, N_G4, N_GS4, N_A4, N_C5, N_A4, N_D5, N_D5, N_D5, N_A4, N_D5, N_C5, N_F4, N_F4, N_F4, N_F4, N_F4, N_D4, N_D4, N_D4, N_F4, N_F4, N_F4, N_F4, N_D4, N_F4, N_E4, N_D4, N_C4, 0, N_G4, N_E4, N_D4, N_D4, N_D4, N_D4, N_F3, N_G3, N_AS3, N_C4, N_D4, N_F4, N_C5, 0, N_F4, N_D4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_F4, N_F4, N_GS4, N_A4, N_C5, N_A4, N_GS4, N_G4, N_F4, N_D4, N_E4, N_F4, N_G4, N_A4, N_C5, N_CS5, N_GS4, N_GS4, N_G4, N_F4, N_G4, N_F3, N_G3, N_A3, N_F4, N_E4, N_D4, N_E4, N_F4, N_G4, N_E4, N_A4, N_A4, N_G4, N_F4, N_DS4, N_CS4, N_DS4, 0, N_F4, N_D4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_F4, N_F4, N_GS4, N_A4, N_C5, N_A4, N_GS4, N_G4, N_F4, N_D4, N_E4, N_F4, N_G4, N_A4, N_C5, N_CS5, N_GS4, N_GS4, N_G4, N_F4, N_G4, N_F3, N_G3, N_A3, N_F4, N_E4, N_D4, N_E4, N_F4, N_G4, N_E4, N_A4, N_A4, N_G4, N_F4, N_DS4, N_CS4, N_DS4,

};

 

int noteDurations[] = {

  16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 8, 16, 8, 8, 8, 8, 4, 16, 8, 16, 8, 8, 8, 16, 16, 16, 16, 16, 8, 8, 16, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16, 16, 2, 8, 16, 8, 8, 8, 8, 4, 16, 8, 16, 8, 8, 8, 8, 8, 16, 8, 16, 8, 8, 8, 8, 8, 8, 8, 16, 8, 15, 8, 8, 2, 3, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 2, 16, 8, 16, 8, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8,  8, 8, 16, 16, 16, 2, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 2, 8, 8, 8, 8, 2, 2, 3, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 2, 16, 8, 16, 8, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8,  8, 8, 16, 16, 16, 2, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 2, 8, 8, 8, 8, 2, 1

};

 

//melody = 266 notes

 

void setup() {

  int melody_len = sizeof(melody)/sizeof(melody[0]);

 

  for (int thisNote = 0; thisNote < melody_len; thisNote++) {

    int noteDuration = TEMPO / noteDurations[thisNote];

    tone(8, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.45;

    delay(pauseBetweenNotes);

    noTone(8);

  }

}

 

void loop() {}


And thats all for now! Hope you enjoyed reading how my experience was doing coding for the first time😌



 

 

Comments

Popular Posts