Arduino Programming

Input devices: Interface a potentiometer analog input to make 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

Code

Explanation

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)

}


int sensorValue = 0

It reads the input of 0 and writes into a integer variable, sensorVal

void setup()

{

  pinMode(A0, INPUT);

To set A0 as the input

pinMode(13, OUTPUT);

To set pin 13 as the output

Serial.begin(9600);

To establishes serial communication between your Arduino board and another device

void loop()

To instruct the code to run on an infinite loop

sensorValue = analogRead(A0);

It reads the voltage passing through A0

Serial.println(sensorValue);

It displays the value of sensorVal if the analog is turned

digitalWrite(13, HIGH);

To make pin 13 change the voltage to high (turn on)

delay(sensorValue);

To wait for sensorValue milliseconds

digitalWrite(LED_BUILTIN, LOW);

To make pin 13 change the voltage to low (turn off)

delay(sensorValue);

To wait for sensorValue milliseconds

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.

Initially we couldn't get the LED to light up even with arranging the circuit correctly. We decided to switch the LED cathode and anode by just flipping the LED around. We used trial and error and wow it worked.


4. Below is a 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

Code 

Explanation

int light;

 

void setup() {

Serial.begin(9600);

}

 

void loop() {

  light= analogRead(A0);

 

 if(light<30){

   digitalWrite(13, LOW);

 

 }else{

   digitalWrite(13, HIGH);

 }

 

  Serial.println(light);

  delay(0);

}

 

void setup()

{

  Serial.begin(9600);

To establishes serial communication between your Arduino board and another device

void loop()

To instruct the code to run on an infinite loop

light = analogRead(A0);

It reads the light shun on A0

if(light<30){

   digitalWrite(13, LOW);

 

 }else{

   digitalWrite(13, HIGH);

 }

 

  Serial.println(light);

  delay(0);

}

 

If light shun on the LDR is at low value, LED connected to pin 13 will not be turned on. 

Else, if there are light shun on LDR, the LED will be turned on.

digitalWrite(13, LOW);

To make pin 13 change the voltage to low (turn off)

digitalWrite(13, HIGH);

To make pin 13 change the voltage to high (turn on)

delay(0);

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.

My team and I couldn't get the LDR to work and after watching several videos and asking around, we manage to find the code as well as the set up. When I had to set it up by myself, my LED lighted up but it wasn't affected by the LDR and I thought it was a connected problem. But following the setup my team has done before, it was exactly the same. I decided to play with the code and changed light<50 to light<30. It worked, hence I think that due to the darker environment I was at, it made the LED not light up.


4. Below is a 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

Code 

Explanation

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);                     

}

 

 

void setup()

{

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

To set pin 11,12,13 as the output

void loop()

To instruct the code to run on an infinite loop

digitalWrite(LED_BUILTIN, HIGH);

To make pin 13 change the voltage to high (turn on)

delay(2000);

To wait for 2000 milliseconds

digitalWrite(LED_BUILTIN, LOW);

To make pin 13 change the voltage to low (turn off)

delay(1000);

To wait for 1000 milliseconds

digitalWrite(12, HIGH);

To make pin 12 change the voltage to high (turn on)

delay(2000);

To wait for 2000 milliseconds

digitalWrite(12, LOW);

To make pin 12 change the voltage to low (turn off)

delay(1000);

To wait for 1000 milliseconds

digitalWrite(11, HIGH);

To make pin 11 change the voltage to high (turn on)

delay(2000);

To wait for 2000 milliseconds

digitalWrite(11, LOW);

To make pin 11 change the voltage to low (turn off)

delay(1000);

To wait for 1000 milliseconds


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

https://youtu.be/X8dHbdhnGKY


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

This task went pretty smoothly.


4. Below is a 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

Code 

Explanation

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);

  }

}

 

void setup()

To start serial connection

Serial.begin(9600);

To establishes serial communication between your Arduino board and another device

pinMode(2, INPUT_PULLUP);

To set pin 2 as the input

pinMode(13, OUTPUT);

pinMode(12, OUTPUT);

pinMode(11, OUTPUT);

To set pin 11,12,13 as the output

void loop() {

Loop the code

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);

If sensoVal value is high, LED connected to pin 11,12,13 will not light up. If sensorVal value is low, LED connected to pin 13 will light up for 1000 milliseconds then LED connected to pin 12 will light up for 1000 milliseconds and lastly LED connected to pin 11 will light up for 1000 milliseconds 


digitalWrite(13, LOW);

To make pin 13 change the voltage to low (turn off)

digitalWrite(12, LOW);

To make pin 12 change the voltage to low (turn off)

digitalWrite(11, LOW);

To make pin 11 change the voltage to low (turn off)

digitalWrite(13, HIGH);

To make pin 13 change the voltage to high (turn on)

digitalWrite(12, LOW);

To make pin 12 change the voltage to low (turn off)

digitalWrite(11, LOW);

To make pin 11 change the voltage to low (turn off)

delay(1000);

To wait for 1000 milliseconds

digitalWrite(13, LOW);

To make pin 13 change the voltage to low (turn off)

digitalWrite(12, HIGH);

To make pin 12 change the voltage to high (turn on)

digitalWrite(11, LOW);

To make pin 11 change the voltage to low (turn off)

delay(1000);

To wait for 1000 milliseconds

digitalWrite(13, LOW);

To make pin 13 change the voltage to low (turn off)

digitalWrite(12, LOW);

To make pin 12 change the voltage to low (turn off)

digitalWrite(11, HIGH);

To make pin 11 change the voltage to high (turn on)

 delay(1000);

To wait for 1000 milliseconds

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, the first LED lighted up the entire time without pressing the button and the other two LED alternated, also without pressing the button. The problem was fixed by making the LED into a parallel circuit so when the button is pressed, the LEDS will then light up.


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




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

Through this practical, I realised how much I didn't like coding. But on the flippity side, it was kind of fun too besides the part where I keep getting frustrated when trying figure out how to put the two code together and get it to work.

For the practical, we decided to use the LED as the eyes of little pegasus 🎠 shine at the same speed with the flapping of it's wings. The codes used to do task 1 and 2 in classes were really helpful. Such as part 2 A where we made the lights blink/flash💡, this adds to the aesthetic criteria of the practical.

Besides that, we used servo that we learnt through the pre-practical package to make the wings flap. It wasn't the nicest flap as it looks more like a spasm but I think this can be better by changing up the degree of the servo.

As musician(how dare I claim that), me and Glenn wanted to make the pegasus neigh but with such elegant piano sounds, how can we make a neigh using a piano. Then we decided to make pegasus become a singing pegasus and made him sing "smile-butterfly" from the 90s. It was kinda hilarious though.

Towards the end, we encountered some problem where we couldn't make the multiple code perform simultaneously. We then learnt that it is not possible as we cannot use delay. Hence the nearest way to put the codes together was to make the song play first before the flapping + blinking of the eyes. At first, I just copied and pasted the two codes together, however it couldn't work. I then realised, we could not have two "void setup" or "void loop" hence I retried by combining both "void setup" together as well as "void loop". I tried and voila, it worked.

To assemble the pegasus:
We followed the cuttings on the cardboard and tried folding it to make little pegasus out by following how the queen of pegasus looked like. At first, we didn't realised we had to fold the tabs and it looked quite funny as there are tabs protruding out.
Look at how cute our pegasus turned out.



Videos an photos of our product:





ps: this video was taken before pegasus went to the saloon and had a makeover💄


Codes are attached below:

#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