Arduino Programming

There are 4 tasks that will be explained in this page:

  1. Input devices:

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

  2. Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE

  1. Output devices:

  1. Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)​

  2. 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.

Finally, I will describe:

  1. 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.

Code/program in writeable format

Explanation of 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)

  digitalWrite(13, LOW);

  // pause the program for <sensorValue> milliseconds

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

}


Int sensorValue = 0; : Writes in a integer variable,sensorVal if the input is 0


Void setup(): Code type here will be establish as input or output


pinMode(A0,INPUT); : Set pin A0 as an input


pinMode(13,OUTPUT); : Set pin 13 as an output


Serial. begin (9600); : Establish serial communication between the Arduino board and another device


void loop() : Code type here will run an infinite loop


sensorValue = analogRead(A0); : Reads the voltage passing through A0


Serial.println(sensorValue); : It displays the value of sensorVal if it is pressed.


digitalWrite(13, HIGH); : Change the voltage to high (Turn on)


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


digitalWrite(13, LOW); : Change the voltage to low (Turn off)


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

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

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

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

At first when I open the serial monitor, it was not displaying anything and I did not know what was wrong. However after asking my friend, I realised that I have to add the Serial. begin(9600) and Serial.printIn in order for the serial monitor to pick up the value.

  1. Below is the 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/program in writeable format

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

int fade = map(sensorValue, 54, 974, 255, 0);

  Serial.println(fade);


analogWrite(13,fade);

delay(100);

}

Int sensorValue = 0; : Writes in a integer variable,sensorVal if the input is 0


Void setup(): Code type here will be establish as input or output


pinMode(A0,INPUT); : Set pin A0 as an input


pinMode(13,OUTPUT); : Set pin 13 as an output


Serial. begin (9600); : Establish serial communication between the Arduino board and another device


void loop() : Code type here will run an infinite loop


sensorValue = analogRead(A0); : Reads the voltage passing through A0


Serial.println(sensorValue); : It displays the value of sensorVal if it is pressed.


int fade = map(sensorValue, 54, 974, 255, 0); :Map the sensor reading to a range for the LED (In this case, when it is dark, the resistance is 54, when it is bright the resistance is 974. When the resistance of the LDR reaches 54, the LED will light up and when the resistance reaches 974, the LED will not light up)


Serial.println(fade); :  It displays the value of fade if it is pressed.


analogWrite(13,fade); :Writes an analog value to pin 13 as fade


delay(100); :Delay for 100 milliseconds 


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

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

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

At first I did not know why the LED lights up when it is bright and turns off when is dark. However after looking through the code, I realised that the sensor reading is opposite which means when the resistor is low, the LED will light up vice versa. By changing the code int fade = map(sensorValue, 0, 1023, 0 , 255) to int fade = map(sensorValue, 54, 974, 255 , 0), the LED will light up when it is dark.


  1. Below is the 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/program in writeable format

Explanation of the code

void setup() {

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

}

void loop() {

  digitalWrite(13, HIGH);   

  delay(100);                       

  digitalWrite(13, LOW);   

  delay(100);                       

  digitalWrite(12, HIGH);  

  delay(100);                       

  digitalWrite(12, LOW);   

  delay(100);      

  digitalWrite(11, HIGH);  

  delay(100);     

  digitalWrite(11, LOW);

  delay(100);      

}

Void setup(): Code type here will be establish as input or output


pinMode(13, OUTPUT); : Set pin 13 as an output


pinMode(12, OUTPUT); :Set pin 13 as an output


pinMode(11, OUTPUT); :Set pin 13 as an output


void loop() :Code type here will run an infinite loop


digitalWrite(13, HIGH); :Change the voltage to high (Turn on)


delay(100); :Delay for 100 milliseconds 


digitalWrite(13, LOW); :Change the voltage to low (Turn off)


delay(100); :Delay for 100 milliseconds 

  

digitalWrite(12, HIGH); :Change the voltage to high (Turn on)


delay(100); :Delay for 100 milliseconds 


digitalWrite(12, LOW); :Change the voltage to low (Turn off)


delay(100); :Delay for 100 milliseconds 


digitalWrite(11, HIGH); :Change the voltage to high (Turn on)


delay(100); :Delay for 100 milliseconds 


digitalWrite(11, LOW); :Change the voltage to low (Turn off)

  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.

Putting the LED on wrongly which cause the LED to not light up. By changing the positive end to the wire and the negative end to the resistor.

  1. Below is the 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/program in writeable format

Explanation of the code

void setup() {

  Serial.begin(9600);

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

  pinMode(2, INPUT_PULLUP);


}


void loop() {

  int sensorVal = digitalRead(2);

  Serial.println(sensorVal);



  if (sensorVal == HIGH) {

      

  digitalWrite(13, LOW); 

  delay(100);                    

  digitalWrite(12, LOW);    

  delay(100);      

  digitalWrite(11, LOW);    

  delay(100);   

                     

  } else {

              

  digitalWrite(13, HIGH);    

  delay(100);                     

  digitalWrite(12, HIGH);    

  delay(100);      

  digitalWrite(11, HIGH);   

  delay(100); 

  digitalWrite(13, LOW); 

  delay(100);                    

  digitalWrite(12, LOW);    

  delay(100);      

  digitalWrite(11, LOW);    

  delay(100);        

}

}

Void setup(): Code type here will be establish as input or output


Serial. begin (9600); : Establish serial communication between the Arduino board and another device


pinMode(13, OUTPUT); : Set pin 13 as an output


pinMode(12, OUTPUT); :Set pin 13 as an output


pinMode(11, OUTPUT); :Set pin 13 as an output


pinMode(2, INPUT_PULLUP); : Declare that PIN 2 is an INPUT and enable the internal pull up resistor. So, by default, PIN2 status will always be HIGH


void loop() :Code type here will run an infinite loop


int sensorVal = digitalRead(2); :It reads the input of PIN2 and writes into a INTEGER variable, sensorVal.


Serial.println(sensorVal); :It displays the value of sensorVal if it is pressed.


if (sensorVal == HIGH) : If the button is not pressed, the voltage send to pin 13,12 and 11 will be low which means ‘OFF;


else: If the button is pressed, the voltage send to pin 13,12 and 11 will loop from high to low which means it all 3 LEDs will light up with delays of 100 milliseconds between each other and turn off with delays of 100 milliseconds between each other.

  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.

At first when I pressed the button, the LEDs lights up and remains on but I wanted it to keep turning on and off after I pressed the button. By changing the else part to make the LED turn on and off, the LEDs will keep turning on and off if I hold the button.

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



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


At first, when I was told that we were doing Arduino programming I absolutely hated it as it was so much work and programming codes was very frustrating when the code is wrong and I didn't know what was wrong with it. However, after doing the code slowly and understanding what each lines mean, I slowly started to understand how some codes work and manage to create my code which worked. It was a slow start for me but eventually it worked out for me before the Arduino practical. 

For the practical, our end product which is a pegasus which must be able to perform its main function of flapping, compact and easily moved, reliable and durable, aesthetically pleasing and can perform other function. 

For the eyes and wings, we decided to use red LEDs as 'sharingans' and colour the akatsuki pattern on the wings which we took inspiration from an anime called 'Naruto'.
For the body, we decided to colour it rainbow which we took inspiration from a character from my little pony called rainbow dash.

For the code, we use a melody code, a LED blink code and a servo code. First, the melody will play which sounds like the activation of the sharingan. Second, the LED will light up and lastly the servo will move which will flap the wings.

Sharingan

The Akatsuki
A picture of our finals product:

Here is the code that we use:

#define LED 9

#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

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_A7
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4
};

void setup() {
pinMode(LED, OUTPUT);
pinMode(3, OUTPUT);

  myservo.attach(11);  // attaches the servo on pin 9 to the servo object

// iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; 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, HIGH);
  delay (100);
  digitalWrite(LED, LOW);
  delay (100); 
  digitalWrite(3, HIGH);
  delay (100);
  digitalWrite(3, LOW);
  delay (100);   

    for (pos = 0; pos <= 180; pos += 15) { // goes from 0 degrees to 20 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 0ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 15) { // goes from 150 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(40);                       // waits 15ms for the servo to reach the position

}}

pitches.h:
#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

A video of our final product in action:

Overall, this practical was a very fun one as I get to use my Arduino skills and be able to code what I want which puts what I learn from the previous lessons to the test. However for the practical, a few things that we can improve on are to use cardboard joineries that we learned in the previous module to attached the pegasus to the cardboard board and we can hide the wires below the box to make it more aesthetically more pleasing.

Comments