ABOUT ME!

Hi my name is Glenn from class DCHE/FT/2B/04 and I love football and playing mahjong. Sometimes I would sacrifice precious sleep just to wat...

Arduino Programming

Arduino Documentation Blog Entry

 

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

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:

  • int sensorValue = 0; Writes input of 0 into a integer variable 
  • void setup(): Setup will run once the 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); :Shows value of sensorval
  • digitalWrite(13, HIGH); :Voltage in pin 13 is increased to high (LED on)
  • delay(sensorValue); :Delay or wait for sensorval (seconds)
  • digitalWrite(LED_BUILTIN, LOW); :Voltage in pin 13 is decreased to low (LED low)


H
yperlink to the sources/references that I used to write the code/program:

Problems I have encountered and how I fixed them:

Even after following the given video step by step, somehow we could not get the LED to light up untill Jasmine suggested flipping the LED and boom! It worked.

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:

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:

  • 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){

       digitalWrite(13, LOW); :If light reading from A0<50, LED would not be lit

  • }else{

       digitalWrite(13, HIGH); :Or else, LED would be lit (A0>50)

  • Serial.println(light); :Displays value of sensorval

  • delay(0); :Zero delay

Problems I have encountered and how I fixed them:

When my group and I first attempted adding a LDR into the set up by replacing the potentiometer from the previous task, we witness insignificant change to the LED and we found it strange. Again, Jasmine came up with a brilliant hypothesis that our surrounding light would affect the result and true enough it does.

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)

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:
  • 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 code
  • digitalWrite(LED_BUILTIN, HIGH); :Pin 13's voltage high (LED on) 
  • digitalWrite(LED_BUILTIN, LOW); :Pin 13's voltage low (LED off)
  • digitalWrite(12, HIGH); :Pin 12's voltage high (LED on) 
  • digitalWrite(12, LOW); :Pin 12's voltage low (LED off)
  • digitalWrite(11, HIGH); :Pin 11's voltage high (LED on)
  • digitalWrite(11, LOW); :Pin 11's voltage low (LED off)
  • delay(2000); :Delay for 2000ms (2 seconds)
  • delay(1000); :Delay for 1000ms (1 second)

Hyperlink to the sources/references that I used to write the code/program:

Problems I have encountered and how I fixed them:

My group and I managed to complete this activity smoothly.

Short video as the evidence that the code/program work:




----------------------------------------------------------------------------------------------------


Output devices: Include pushbutton to start/stop the previous task 

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:

  • 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
  • digitalWrite(13, LOW); :Sets pin 13 as output
  • digitalWrite(12, LOW); :Sets pin 12 as output
  • digitalWrite(11, LOW); :Sets pin 11 as output
  • void loop() :Loops code
  • int sensorVal = digitalRead(2); :Read the sensor value at pin 2
  • Serial.println(sensorVal); :Show value of Sensorval 
  • if (sensorVal == HIGH) {

        digitalWrite(13, LOW);

        digitalWrite(12, LOW);

        digitalWrite(11, LOW); :If sensor value high, pins 13,12 & 11 voltage decrease, LED off

  • } else { :Or else

  • digitalWrite(13, HIGH); :Pin 13's voltage high (LED on) 

  • digitalWrite(12, LOW); :Pin 12's voltage low (LED off)

  • digitalWrite(11, LOW); :Pin 11's voltage low (LED off)

  • digitalWrite(13, LOW); :Pin 13's voltage low (LED off)

  • digitalWrite(12, HIGH); :Pin 12's voltage high (LED off)

  • digitalWrite(11, LOW); :Pin 11's voltage low (LED off)

  • digitalWrite(13, LOW); :Pin 13's voltage low (LED off)

  • digitalWrite(12, LOW); :Pin 12's voltage low (LED off)

  • digitalWrite(11, HIGH); :Pin 11's voltage high (LED on)

  • delay(1000); :Delay for 1000ms (1 second)


Problems I have encountered and how I fixed them:

I did not encounter any problem.

Short video as the evidence that the code/program work:





----------------------------------------------------------------------------------------------------


Learning Reflection on the overall Arduino Programming activities.

Grasping the concept and learning how to code using the Arduino Programming at the start was very tedious. Since everything was new to me, I did not understand what each code means or represent so I had a hard time inputting the correct codes in however as I slowly did more, I soon realise many of the codes are repititions with just some minor changes depending on the input and output numbers. I felt that the most engaging activity during the whole Arduino Programming Pre-Practical would be "Make some noise". Since I do have some music background, playing around with the songs I could produce was very fun. My group started off with making "Mary had a little lamb" to making christmas songs🎄. What made us different from other groups was that we keyed in every single notes and codes individually instead of copying and pasting it from websites online. We had to actually used our music background to figure our way around and listen to each notes on the song. Overall I had so much fun in this pre practical activity. (Too much fun I might have neglected the other activites.) 👀

However the Arduino Practical was my favourite activity of the entire Arduino Programming activities, my group and I had way too much fun. We first planned out to what kind of modifications we wanted to add to the pegasus and to make it as realistic as possible, we tried adding horse noises which was very funny. We spent 10 mins hearing horses neigh😂. After realising there was no notes that the arduino programme could hit, we scraped that idea and decided to add the iconic song "Butterfly" by smiles to mimick a toy that we can buy from Pasar Malam but with a more disco vibe. With music background, Jasmine and I went ahead while Samantha tried figuring out how to add LED lights to the eyes of the Pegasus. On the other hand, Hariz and Tristan tried figuring out how to make the wings move. After completing our given tasks, we decorated our Pegasus to look more like a singer/dancer💃. And here is our final product!


As previously mentioned, our Pegasus is a singer/ dancer💃 so let it demostrate its talents!!👏


As amazing our Pegasus is, it did not come without any issues. We initially wanted it to sing and dance at the same time however, no matter how hard we tried, we could not figure out but that did not dampan our spirit. We were still very proud of our Pegasus and we even gave it a lil medal at the end. Look at my group and I with our very special guest appearance, Dr. Noel. We hope Dr. Noel loved our Pegasus performance as much as we did!😊




No comments:

Post a Comment