Arduino Programming

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

Breakdown of 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;

Reads 0 as an integer variable

void setup()

{

  pinMode(A0, INPUT);

Set pin A0 as the input 

pinMode(13, OUTPUT);

Set pin 13 as the output 

Serial.begin(9600);

}

Establishes connection between the arduino board and the laptop

void loop()

{

To set the code to run in a loop

sensorValue = analogRead(A0);

Reads the voltage of light passing through A0

Serial.println(sensorValue);

Displays value of sensor value 

digitalWrite(13, HIGH);

Turns on pin 13 (voltage of pin 13 will be changed to high)

delay(sensorValue);

Wait for sensorValue millisecond(s)

digitalWrite(LED_BUILTIN, LOW);

Turns off LED light (voltage will be changed to low)

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

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

Initially, the LED light was connected wrongly. The cathode and anode was in the opposite direction. We figured it out after playing around with the setup.

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



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

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

}

int light;

Sets “light” as in integer 

void setup() {

Serial.begin(9600);

}

Establishes connection between the arduino board and the laptop

void loop() {

To set the code to run in a loop

light= analogRead(A0);

Reads the voltage of light passing through A0

if(light<30){

   digitalWrite(13, LOW);

 

 }else{

   digitalWrite(13, HIGH);

 }

Pin 13 will not turn on when value is less than 30 (low)


Or else, pin 13 will turn on (high)

Serial.println(light);

Displays value of sensor value 

delay(0);

}

No delay

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

            -

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

I could not figure out how to connect the LDR and wires, so I went to watch YouTube to see how people on the internet assembled their Arduino board. I improvised on the code so that the input and output matches.

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


                                         

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

Breakdown of code 

Explanation

int brightness = 0; 

void setup() {

 

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}


void loop() {

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

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

digitalWrite(9, brightness); 

delay(150);

digitalWrite(10, brightness); 

delay(150);

digitalWrite(11, brightness); 

delay(150);

{

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

digitalWrite(9, brightness); 

delay(150);

digitalWrite(10, brightness); 

delay(150);

digitalWrite(11, brightness); 

delay(150);

}


}

pinMode(9, OUTPUT);

Set pin 9 as the output 

pinMode(10, OUTPUT);

Set pin 10 as the output 

pinMode(11, OUTPUT);

Set pin 11 as the output 

void loop() {

To set the code to run in a loop

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

LED will turn on, maximum brightness value is 255

digitalWrite(9, brightness); 

Turns on pin 9

delay(150);

Wait for 150 milliseconds 

digitalWrite(10, brightness);

Turns on pin 10

delay(150);

Wait for 150 milliseconds 

digitalWrite(11, brightness); 

Turns on pin 11

delay(150);

Wait for 150 milliseconds 

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

LED will turn off, minimum brightness value is 0 

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

            -

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


Initially, I used too many wires and it caused the setup to be very complex. I simplified the setup by removing the extra wires.

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



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

Breakdown of code 

Explanation

int brightness = 0; 

void setup() {

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

Serial.begin(9600);

pinMode(2,INPUT_PULLUP);

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}


void loop() {

int sensorVal = digitalRead(2);

  //print out the value of the pushbutton

  Serial.println(sensorVal);

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

   if (sensorVal == HIGH) {

    digitalWrite(9,LOW);   digitalWrite(10,LOW);    digitalWrite(11,LOW);

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

analogWrite(9,brightness); 

delay(150);

analogWrite(10,brightness); 

delay(150);

analogWrite(11,brightness); 

delay(150);

 } else {

    for (int i=0; i < 5; i++)

    {

      digitalWrite(9,HIGH);

      delay(500);

      digitalWrite(9,LOW);

      delay(500);

      digitalWrite(10,HIGH);

     delay(500);

      digitalWrite(10,LOW);

     delay(500);

      digitalWrite(11,HIGH);

     delay(500);

      digitalWrite(11,LOW);

     delay(500);

  

{

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

analogWrite(9,brightness); 

delay(150);

analogWrite(10,brightness); 

delay(150);

analogWrite(11,brightness); 

delay(150);

}

    }

}}

int brightness = 0;

Reads brightness value as an integer variable

Serial.begin(9600);

Establishes connection between the arduino board and the laptop

pinMode(2,INPUT_PULLUP);

to declare that Pin 2 is the input and enable the internal pull up resistor. Hence, status of Pin 2 will always be high 

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

Set pin 9, 10, 11 as output 

int sensorVal = digitalRead(2);

read the input of Pin 2 and writes into an integer variable, sensorVal.

Serial.println(sensorVal)

Displays value of sensor value

digitalWrite(9,LOW);   digitalWrite(10,LOW);    digitalWrite(11,LOW);

Turns off pin 9, 10, 11

analogWrite(9,brightness); 

Turns on pin 9

delay(150);

Wait for 150 milliseconds 

digitalWrite(9,HIGH);


Turns on pin 9

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

             -

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

Initially, the LED lighted up without pressing the button and they were flashing in the wrong sequence. We changed the arrangement of the light bulbs to a parallel arrangement instead of in series so that the LEDs will light and fade in the desired sequence. We also added in the code for push button that we used for our pre practical.


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



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

This was my first time doing something related to coding and programming and it made me realize how much I hated it. I said I want to be confident in programming but I guess it's not happening 😔. Even though it is cool when our codes work and it can run smoothly, there are a lot of times when the code doesn't work and we have to break our heads and squint our eyes to find out what's wrong with our code. 


During our practical, we were tasked to decorate a pegasus 🎠while making use of programming to make its wings flap, and also perform another function (we made it play some music🎵in the background). 


Firstly, we assembled the horse. It was made from cardboard and we used the slot and tab method to join its body parts together. However, some slots and tabs were not that secure so we used a glue gun to stick them together. 



To make the wings flap, we programmed a servo and attached the servo inside the body of the pegasus so as to conceal it. We used a metal wire to tie the wings to the servo, so when the servo is spinning, the wing would flap as well. 



Trying to make the servo spin was a chore. Alvin and I thought our code was wrong initially, and we changed our codes and re-checked it multiple times. But in the end, we found out that it wasn’t working because we connected our output wrongly on the Arduino board 👹👹


Next, for the music, we decided on using the theme song for My Little Pony. Alvin did most of the work for figuring out the music notes.



Lastly, we decorated our pegasus.
still looks very plain, but whatever -_-








Comments