Arduino

 Arduino documentation blog entry

Hello, this will be the final blog for the term and I will be documenting on arduino.

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

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 2a 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 that 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:

5. 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 a serial monitor Arduino IDE

1. Below are the codes/ program that I have used and the explanation of the code.

Code in writable format

Explanation of code

int sensorValue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(13, OUTPUT);

}

 

void loop()

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

  // 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(13, LOW);

  // pause the program for <sensorvalue> milliseconds

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

}


I used the pinMode() function to establish analog pin A0 as an input and digital pin 13 as an output.


The code inside the loop uses a function called analogRead() to listen to the pin’s state.


Instead of pausing for a fixed amount of time, the value passed to the delay() function, the sensor value, will change as I turn the knob of the potentiometer because each time through the loop it is going to read the position again


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

3. Below are the problems that I encountered and how I fixed them
The 1 problem that I faced while doing the code is connecting the wires wrongly to the arduino. For example, connecting the wires at the correct GND and 5v points. As I am still getting used to the arduino board, I will eventually know automatically which slot the wire should belong to.

I simply looked back at the other videos from the first arduino lesson and I was able to see where I was supposed to insert the wires. I also realised afterwards that each slot was labelled....😅

4. 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 codes/ program that I have used and the explanation of the code

Code in writable format

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

}


This code works by setting up the LDR to be conncected to Pin A0. It reads the amount of light and if the light is below 50, it will be read as low while if the light is above 50, it will counted as high, causing the light to either light up or become dim.


Serial.printIn allows the value of the light to be displayed and there is a delay of 0 milliseconds, hence the results are shown

mmediately.


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

3. Below are the problems that I encountered and how I fixed them
I faced more problems with this arduino program compared to the first input device one as I did not refer to a video for this. As such, my first problem was starting the code. I did not know how to start the code and had to ask around. After referencing other people's codes, I was then able to continue as I understood the meanings of the subsequent lines of codes

4. 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 codes/ program that I have used and the explanation of the code

Code in writable format

Explanation of code

int brightness = 0;

void setup() {


pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}

 

void loop() {


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

analogWrite(9, brightness);

delay(150);

analogWrite(10, brightness);

delay(150);

analogWrite(11, brightness);

delay(150);

{

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

analogWrite(9, brightness);

delay(150);

analogWrite(10, brightness);

delay(150);

analogWrite(11, brightness);

delay(150);

}

 

}


This code works by setting PIN 9,10 and 11 as outputs and a loop is created. The loop causes the dim bulbs to increase in brightness to 255 with a delay of 150ms and then they will decrease in brightness from 255 to 0 subsequently with another delay of 150ms, allowing them to light up and dim down continuously.

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

3. Below are the problems that I encountered and how I fixed them
This time the video was able to help me by giving me an idea of how to use TinkerCAD to program this code. However, I was still struggling to get accustomed to using TinkerCAD as I have only used it a few times in the past few weeks. Specifically, using the blocks in TinkerCAD can be quite confusing as well as I did not understand them. However, with trial and error and checking with classmates, I managed to work the code out.

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



Output devices: Include pushbutton to start/ stop previous task

1. Below are the codes/ program that I have used and the explanation of the code

Code in writable format

Explanation of code

int brightness = 0;

void setup() {


Serial.begin(9600);

pinMode(2, INPUT_PULLUP);

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}

 

void loop() {

int sensorVal = digitalRead(2);


  Serial.println(sensorVal);


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

}

    }

}}

 


I used PIN2 as an input to receive signals from the button pushed and make PIN9, 10  and 11 the output such that light flashes from those pins when they receive the signal.

Subsequently, when I use the void loop to ensure that when there is no light being produced by the bulbs, they will increase in brightness with a delay of 150ms.

Next, the bulbs will decrease in brightness with a delay of 150ms.

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

3. Below are the problems that I encountered and how I fixed them
This code made me question life as I was completely loss while doing it and I decided to seek help from anyone in my class that was able to do the code.

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

Below is my learning reflection on the overall Arduino programming activities
This few weeks have definitely not been easy. Although we were given the arduino uno since almost the start of the term, I have not grown accustomed to coding. I feel that coding requires a lot of practice to be able to actually remember what each code does, however, I simply do not have enough practice. I will see myself going back to the learning packages very often, trying to search online on how to figure out the codes or even ask around very often to be able to solve any codes.

Overall, arduino may not be my favourite topic while learning chemical product design & development, however, I am very glad that I got the chance to experience coding using the arduino uno board. I believe that having this skill or just being exposed to this skill will help me in the future too!

During the arduino practical, we were supposed to make our pegasus design:
1. Perfrom its main function (flapping)
2. Be compact and can easily be moved (no loose pieces)
3. Reliable and durable (doesn't fail and no parts failing)
4. Aesthetically pleasing
5. Can perform other functions

First, we had to figure out how to make the wings flap. Everyone in the group provided a different idea of how to make the wings flap and we decided to use a combination of everyone's ideas at the end. The way our pegasus works is by twirling one end of the metal wire around the white part of the servo while the other end is pierced through the two flaps of the wings. We will then attach the servo to the torso area of the pegasus (under the pegasus). We also had to fulfil the requirements of it being aesthetically pleasing and it being able to perform other functions, hence, we decided to add LED lights to the pegasus and add music to it as well.

Coding was relatively simple as we all did the pre-practical activity which included the servo challenge, however, the real challenge came from making it aesthetically pleasing and making it perform other functions. Back then, our knowledge was only limited to the 4 activities in the pre-practical, hence we wanted to add music while the pegasus was flapping its wings. The problem was we could not find a way to add 2 void setups in one program. This means that the 2 codes (one for the servo and the other for the music) had to played seperately. We kept trying to find a way to include the two codes together, searching online and asking other groups. Unfortunately, no one had an answer and we decided to play the music first, then allow the pegasus to flap its wings.🎵 For the music, we decided to play megalovania as it was easily recognisable by many people.

Next, to make our pegasus aesthetically pleasing, we decided to add an array of green, red lights and a bit of yellow LED bulbs to give it a Christmas theme as Christmas was coming.🎅 However, I feel that we could have done a better job in this area as there were small details like hiding the tape inside of the pegasus, or hiding the excess wire inside the pegaus which we could have done easily.




This is our code and video of our pegasus:


#include <Servo.h>

 

Servo myservo;  // create servo object to control a servo

// twelve servo objects can be created on most boards

 

int pos = 65;    // variable to store the servo position

 

#include "pitches.h"

int const TEMPO = 1200;

 

int melody[] = {

  N_D3, N_D3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_C3, N_C3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_B2, N_B2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_AS2, N_AS2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_D3, N_D3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_C3, N_C3, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_B2, N_B2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_AS2, N_AS2, N_D4, N_A3, 0, N_GS3, N_G3, N_F3, N_D3, N_F3, N_G3, N_D4, N_D4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_C4, N_C4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_B3, N_B3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_AS3, N_AS3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_D4, N_D4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_C4, N_C4, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_B3, N_B3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_AS3, N_AS3, N_D5, N_A4, 0, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, N_F4, N_F4, N_F4, N_F4, N_F4, N_D4, N_D4, N_D4, N_F4, N_F4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_G4, 0, N_F4, N_F4, N_F4, N_G4, N_GS4, N_A4, N_C5, N_A4, N_D5, N_D5, N_D5, N_A4, N_D5, N_C5, N_F4, N_F4, N_F4, N_F4, N_F4, N_D4, N_D4, N_D4, N_F4, N_F4, N_F4, N_F4, N_D4, N_F4, N_E4, N_D4, N_C4, 0, N_G4, N_E4, N_D4, N_D4, N_D4, N_D4, N_F3, N_G3, N_AS3, N_C4, N_D4, N_F4, N_C5, 0, N_F4, N_D4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_F4, N_F4, N_GS4, N_A4, N_C5, N_A4, N_GS4, N_G4, N_F4, N_D4, N_E4, N_F4, N_G4, N_A4, N_C5, N_CS5, N_GS4, N_GS4, N_G4, N_F4, N_G4, N_F3, N_G3, N_A3, N_F4, N_E4, N_D4, N_E4, N_F4, N_G4, N_E4, N_A4, N_A4, N_G4, N_F4, N_DS4, N_CS4, N_DS4, 0, N_F4, N_D4, N_F4, N_G4, N_GS4, N_G4, N_F4, N_D4, N_GS4, N_G4, N_F4, N_D4, N_F4, N_F4, N_F4, N_GS4, N_A4, N_C5, N_A4, N_GS4, N_G4, N_F4, N_D4, N_E4, N_F4, N_G4, N_A4, N_C5, N_CS5, N_GS4, N_GS4, N_G4, N_F4, N_G4, N_F3, N_G3, N_A3, N_F4, N_E4, N_D4, N_E4, N_F4, N_G4, N_E4, N_A4, N_A4, N_G4, N_F4, N_DS4, N_CS4, N_DS4,

};

 

int noteDurations[] = {

  16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 16, 16, 8, 6, 32, 8, 8, 8, 16, 16, 16, 8, 16, 8, 8, 8, 8, 4, 16, 8, 16, 8, 8, 8, 16, 16, 16, 16, 16, 8, 8, 16, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16, 16, 2, 8, 16, 8, 8, 8, 8, 4, 16, 8, 16, 8, 8, 8, 8, 8, 16, 8, 16, 8, 8, 8, 8, 8, 8, 8, 16, 8, 15, 8, 8, 2, 3, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 2, 16, 8, 16, 8, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8,  8, 8, 16, 16, 16, 2, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 2, 8, 8, 8, 8, 2, 2, 3, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 2, 16, 8, 16, 8, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8,  8, 8, 16, 16, 16, 2, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 2, 8, 8, 8, 8, 2, 1

};

 

//melody = 266 notes

 

void setup() {

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

 

  int melody_len = sizeof(melody)/sizeof(melody[0]);

 

  for (int thisNote = 0; thisNote < melody_len; thisNote++) {

    int noteDuration = TEMPO / noteDurations[thisNote];

    tone(8, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.45;

    delay(pauseBetweenNotes);

    noTone(8);

  }

}

 

void loop() {

  for (pos = 65; pos >=0 ; pos -= 65) { // 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(50);                       // waits 0ms for the servo to reach the position

  }

  for (pos = 0; pos <=65 ; pos += 65) { // 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(50);                       // waits 0ms for the servo to reach the position

  }

  {}

}

 

 


#define N_B0  31

#define N_C1  33

#define N_CS1 35

#define N_D1  37

#define N_DS1 39

#define N_E1  41

#define N_F1  44

#define N_FS1 46

#define N_G1  49

#define N_GS1 52

#define N_A1  55

#define N_AS1 58

#define N_B1  62

#define N_C2  65

#define N_CS2 69

#define N_D2  73

#define N_DS2 78

#define N_E2  82

#define N_F2  87

#define N_FS2 93

#define N_G2  98

#define N_GS2 104

#define N_A2  110

#define N_AS2 117

#define N_B2  123

#define N_C3  131

#define N_CS3 139

#define N_D3  147

#define N_DS3 156

#define N_E3  165

#define N_F3  175

#define N_FS3 185

#define N_G3  196

#define N_GS3 208

#define N_A3  220

#define N_AS3 233

#define N_B3  247

#define N_C4  262

#define N_CS4 277

#define N_D4  294

#define N_DS4 311

#define N_E4  330

#define N_F4  349

#define N_FS4 370

#define N_G4  392

#define N_GS4 415

#define N_A4  440

#define N_AS4 466

#define N_B4  494

#define N_C5  523

#define N_CS5 554

#define N_D5  587

#define N_DS5 622

#define N_E5  659

#define N_F5  698

#define N_FS5 740

#define N_G5  784

#define N_GS5 831

#define N_A5  880

#define N_AS5 932

#define N_B5  988

#define N_C6  1047

#define N_CS6 1109

#define N_D6  1175

#define N_DS6 1245

#define N_E6  1319

#define N_F6  1397

#define N_FS6 1480

#define N_G6  1568

#define N_GS6 1661

#define N_A6  1760

#define N_AS6 1865

#define N_B6  1976

#define N_C7  2093

#define N_CS7 2217

#define N_D7  2349

#define N_DS7 2489

#define N_E7  2637

#define N_F7  2794

#define N_FS7 2960

#define N_G7  3136

#define N_GS7 3322

#define N_A7  3520

#define N_AS7 3729

#define N_B7  3951

#define N_C8  4186

#define N_CS8 4435

#define N_D8  4699

#define N_DS8 4978




Comments