Topic 3 Arduino

 In this blog I will be talking about Arduino uno 💻

There will be 4 tasks that I will be explaining:
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.

Lastly, 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.

1. Below are the code/program I have used and the explanation of the code. 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); // Wait for sensorValue millisecond(s)

  // turn the LED off

  digitalWrite(LED_BUILTIN, LOW);

  // pause the program for <sensorValue> milliseconds

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

} Explanation of code:

Code

Explanation of the code

int sensorValue = 0;

Writes input of 0 into a integer variable 

void setup()

{

Setup will run once board is powered or reset button is pressed

pinMode(A0, INPUT);

Sets pin A0 as input

pinMode(13, OUTPUT);

Sets pin 13 as output

Serial.begin(9600);

}

Starts serial connection

void loop()

{

Loops code

sensorValue = analogRead(A0);

Reads the voltage passing through the pin A0

Serial.println(sensorValue);

Displays value of sensorval

digitalWrite(13, HIGH);

Voltage in pin 13 is increased to high
(Turns LED on)

delay(sensorValue);

Delay or wait for sensorval (seconds)

digitalWrite(LED_BUILTIN, LOW);

Voltage in pin 13 is decreased to low
(Turns LED off)

delay(sensorValue);

Delay or wait for sensorval (seconds)

2. Below are the hyperlink to the sources/references that I used to write the code/program https://youtu.be/yyG0koj9nNY 3. Below are the problems I have encountered and how I fixed them. After following the steps of the YouTube video, we realized that the LED was still not lighting up despite the arrangements being correct. We then decided to flip the LED around and it worked.
4. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/1rixlih94Ox4z9aiWpQhIcpnjQniWjobS/view?usp=drivesdk 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. 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);

}

Explanation of code:

Code/program in writeable format

Explanation of the code

int light;

Setting an integer as “light”

void setup() {

Setup will run once board is powered or reset button is pressed

Serial.begin(9600);

}

Starts serial connection

void loop() {

Loops code

light= analogRead(A0);

Reads light that is shone on A0

if(light<50){

If light that is read from pin A0 is less than 50, LED that is connected would not turn on. (no light)



If light that is read from pin A0 is more than 50, LED that is connected will turn on. (light)

digitalWrite(13, LOW);

}else{

digitalWrite(13, HIGH);

 }

Serial.println(light);

Displays value of sensorval

delay(0);

}

Ensures that is no delay 

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. At first we were told that the LDR replaced the potentiometer in the set up of task 1a, thus we spent a lot of time trying to figure out how to replace it, but we kept failing. Thus we decided to search for YouTube videos and reset the idea of replacing the potentiometer in set up of task1a. We then arrived at a solution after much discussion and searching. We also realized that the light in ones surroundings will affect the readings of the LDR thus it is better to do it in a dark room.
4. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/1XRkggxD-Flyf6JKyFVp68LhCnOIDt9Lg/view?usp=drivesdk 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 The code:

void setup() {

 

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

 

}

 

 

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);  

  delay(2000);                      

  digitalWrite(LED_BUILTIN, LOW);    

  delay(1000);                      

  digitalWrite(12, HIGH);  

  delay(2000);                      

  digitalWrite(12, LOW);    

  delay(1000)

  digitalWrite(11, HIGH);  

  delay(2000);                      

  digitalWrite(11, LOW);  

  delay(1000);                     

}

Explanation of code:

Code/Program in writable format

Explanation of code

void setup() {

Setup will run once board is powered or reset button is pressed

pinMode(LED_BUILTIN, OUTPUT);

Sets pin 13 as output

pinMode(12, OUTPUT);

Sets pin 12 as output

  pinMode(11, OUTPUT);

}

Sets pin 11 as output

void loop() {

Loops the code

digitalWrite(LED_BUILTIN, HIGH);

Voltage in pin 13 is increased to high (turns LED on)

delay(2000);  

Delays/waits for 2 seconds

digitalWrite(LED_BUILTIN, LOW);

Voltage in pin 13 is decreased to low (turns LED off)

delay(1000);

Delays/waits for 1 second

digitalWrite(12, HIGH);

Voltage in pin 12 is increased to high (turns LED on)

delay(2000)

Delays/waits for 2 seconds

digitalWrite(12, LOW)

Voltage in pin 12 is decreased to low (turns LED off)

delay(1000)

Delays/waits for 1 second

digitalWrite(11, HIGH);

Voltage in pin 11 is increased to high (turns LED on)

delay(2000)

Delays/waits for 2 seconds

digitalWrite(11, LOW);

Voltage in pin 11 is decreased to low (turns LED off)

  delay(1000);                     

}

Delays/waits for 1 second

2. Below are the hyperlink to the sources/references that I used to write the code/program. https://youtube.com/shorts/stExOB1NMfA?feature=share 3. Below are the problems I have encountered and how I fixed them. There were no problems we finished the task smoothly.

4. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/1ZdGeB3H7xFHp9BePRwn5CYS9mg41SgQk/view?usp=drivesdk 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. 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);

  }

} Explanation of code:

Code/Program in writable format

Explanation of code

void setup() {

Setup will run once board is powered or reset button is pressed

Serial.begin(9600);

Starts serial connection

pinMode(2, INPUT_PULLUP);

Sets pin 2 as input

pinMode(13, OUTPUT);

Sets pin 13 as output 

pinMode(12, OUTPUT);

Sets pin 12 output

pinMode(11, OUTPUT);

}

Sets pin 11 output

void loop() {

Loops the code

int sensorVal = digitalRead(2);

Read the sensor value at pin 2

Serial.println(sensorVal);

Display value of Sensorval

if (sensorVal == HIGH) {

If sensor Value is high, voltages passing through pins 13, 12 and 11 are decreased. Thus the LEDs will be turned off.

digitalWrite(13, LOW);

digitalWrite(12, LOW);

digitalWrite(11, LOW);

} else {

If sensor value is not High

digitalWrite(13, HIGH);

Voltage passing through pin 13 is increased to high. (turns LED on)

digitalWrite(12, LOW);

Voltage passing through pin 12 is decreased to low. (turns LED off)

digitalWrite(11, LOW);

Voltage passing through pin 11 is decreased to low. (turns LED off)

delay(1000);

Delays/waits for 1 second

digitalWrite(13, LOW);

Voltage passing through pin 13 is decreased to low. (turns LED off)

digitalWrite(12, HIGH);

Voltage passing through pin 12 is increased to high. (turns LED on)

digitalWrite(11, LOW);

Voltage passing through pin 11 is decreased to low. (turns LED off)

delay(1000);

Delays/waits for 1 second

digitalWrite(13, LOW);

Voltage passing through pin 13 is decreased to low. (turns LED off)

digitalWrite(12, LOW);

Voltage passing through pin 12 is decreased to low. (turns LED off)

digitalWrite(11, HIGH);

Voltage passing through pin 11 is increased to high. (turns LED on)

delay(1000);

  }

}

Delays/waits for 1 second

2.Below are the hyperlink to the sources/references that I used to write the code/program. https://youtube.com/shorts/4zTQMNlfczY 3. Below are the problems I have encountered and how I fixed them. At first, the first LED was lighted up the whole time without even pressing the button. The other two LEDs were also alternating without pressing the button as well. To solve it, we had to ensure the LED was in a parallel circuit. Thus making sure that the LEDs will light up in the correct sequence. 4. Below is the short video as the evidence that the code/program work. https://drive.google.com/file/d/10M90_TwlRMfFk_SDy0r9TL1BxEQlIvG3/view?usp=drivesdk Below is my Learning Reflection on the overall Arduino Programming activities. In general these programming activities reminded me why I did not choose computing in the first place. Even though it is interesting and cool that something would work when we write our own codes, however it is frustrating when the codes we write ends up being wrong. We will then have to go and source and sieve out for the error in the huge chunk of codes we wrote. In our practical, we had to put our Arduino programming skills to the test and do our best to decorate a Pegasus made out of cardboard that we folded. For the task, we decided to allow our little Pegasus to flap its wings and also gave it GREEN LED eyes. We also ended up adding the smile-butterfly song to it. Because I am not a very musically inclined individual, I ended up assembling the breadboard together while Jasmine and Glenn found the right chords for the song. Some problems we encountered were that initially we wanted to flap the wings, make the LED lights light up, and play the song at the same time. This was not possible however as after we clarified with Dr Noel, we realized that we cannot use "delay". We then hence comprised and place the song first before the blinking + flapping of the eyes. We first assembled the Pegasus and modeled it to look like the "MAMA Pegasus". At first we left the tabs out and we realized it looked very weird, then we learnt that we needed to fold the tabs in and this will make the Pegasus more compact and look more presentable. Here is a picture of our undecorated Pegasus with its mum. Look at how small our Pegasus is.

We also ended up decorating out Pegasus to make it look more appealing.
Here is my teammates & Dr Noel with our amazing little Pegasus
Below is a video of our Pegasus working
Here is the code we used to make it work:
#include "pitches.h" // notes in the melody: #include <Servo.h> Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(LED_BUILTIN, OUTPUT); pinMode(12, OUTPUT); int melody[] = { NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_E5, NOTE_CS5, NOTE_B4, NOTE_GS4, NOTE_FS4, NOTE_FS4, NOTE_GS4, NOTE_B4, NOTE_GS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_FS4, NOTE_E4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS4, NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_E5, NOTE_CS5, NOTE_B4, NOTE_GS4, NOTE_FS4, NOTE_FS4, NOTE_GS4, NOTE_B4, NOTE_GS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_FS4, NOTE_E4, NOTE_CS4,NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_GS4, NOTE_B4, NOTE_CS5, NOTE_E5, NOTE_CS5, NOTE_B4, NOTE_GS4, NOTE_FS4, NOTE_FS4, NOTE_GS4, NOTE_B4, NOTE_GS4, NOTE_E4, NOTE_FS4, NOTE_GS4, NOTE_FS4, NOTE_E4, NOTE_CS4 }; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 2, 4, 4, 2, 4, 4, 4, 4, 4, 3, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 4, 6, 6, 6, 6, 6, 4, 3, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 3, 6, 6, 4, 6, 6, 6, 6, 6, 4, 3, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4 }; // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 77; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(8); } } void loop() { digitalWrite(LED_BUILTIN, HIGH); digitalWrite(12, HIGH); for (pos = 0; pos <= 180; pos += 20) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 20) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position digitalWrite(LED_BUILTIN, LOW); digitalWrite(12, LOW); delay(100); } } PITCHES: /************************************************* Public Constants *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978


Comments

Popular posts from this blog