top of page
Search

Blog 3: Arduino Programming

  • Writer: darelsuen
    darelsuen
  • Dec 2, 2022
  • 5 min read

Updated: Dec 3, 2022

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 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:

5. My learning reflection on the overall Arduino programming activities.


For the below activities, I had done them in pairs, with my partner, Dylan who was also my partner for the Arduino practical, below the 4 activities


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


ree

1. Below is the code/program I have used and the explanation of the code.

Code/program in a writable format

Explanation of the code

int sensorValue = 0;


void setup()

{

pinMode(A0, INPUT);

pinMode(LED_BUILTIN, OUTPUT);

}


void loop()

{

// read the value from the sensor

sensorValue = analogRead(A0);

// Turn LED on

digitalWrite(LED_BUILTIN, HIGH);

// Pause the program for <sensorValue> milleseconds

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

// Turn LED off

digitalWrite(LED_BUILTIN, LOW);

// Pause the program for <sensorValue> milleseconds

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

}


int sensorValue = 0; - creates an integer variable called sensorValue, and of starting value 0

pinMode(A0, INPUT); - sets analog pin A0 as input

pinMode(LED_BUILTIN, OUTPUT); - sets LED at pin 13 as output

sensorValue = analogRead(A0); - makes variable sensorValue = A0 input value

digitalWrite(LED_BUILTIN, HIGH); - turns on pin 13 LED

delay(sensorValue); - wait for sensorValue amount of milleseconds

digitalWrite(LED_BUILTIN, LOW); - turns off pin 13 LED

The rate at which the LED blinks is dependent on the resistance of the potentiometer.


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

Arduino – Control LED Brightness With a Potentiometer - The Robotics Back-End


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


As this was the first activity I have ever done with the MakerUno board, the first problem I encountered was very basic, I did not connect the wires correctly on my circuits. As it was my first activity, I did not understand the logic behind the wire connections and more so just followed the wiring configuration they gave on BrightSpace as well as various sources I found. But besides that, once the wiring issue was fixed, there were no other issues I encountered.


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



(paiseh the video a bit stretched, it was the first activity like I said above so I recorded it in portrait mode LOL)



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


1. Below is the code/program I have used and the explanation of the code.

Code/program in writable format

Explanation of the code

int light;


void setup() {

Serial.begin(9600);


}


void loop() {

light=analogRead(A0);

Serial.println(light);

delay(100);


}

int light; - creates an integer variable called light



Serial.begin(9600); - Serial. begin() establishes serial communication between your Arduino board and another device.


light=analogRead(A0); - make the value of variable light equal the value read by input A0


Serial.println(light); - displays the value of light


delay(100); - wait for 0.1 seconds


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


LDR with Arduino - Measure Light Intensity using Photoresistor



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


The main problem with the LDR was actually trying to find online sources for codes. As most online sources I found utilised an LED to track the resistance of the LDR, instead of the IDE. I asked one of my classmates and he provided me with the YouTube link for a video he found which helped us figure out how to do the code in the end.


4. 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)


ree

1. Below is the code/program I have used and the explanation of the code.

​Code/program in writable format

Explanation of the code

int LEDdelay = 0;

void setup()

{

pinMode(LED_BUILTIN, OUTPUT);

pinMode(12, OUTPUT);

pinMode(11, OUTPUT);

}

void loop()

{

LEDdelay = 100;

digitalWrite(LED_BUILTIN, HIGH);

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

digitalWrite(LED_BUILTIN, LOW);

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

digitalWrite(12, HIGH);

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

digitalWrite(12, LOW);

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

digitalWrite(11, HIGH);

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

digitalWrite(11, LOW);

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

}


int LEDdelay = 0; - creates an integer variable called LEDdelay, and of starting value 0

pinMode(LED_BUILTIN, OUTPUT); - sets the LED at pin 13 as output

pinMode(12, OUTPUT); - sets the LED at pin 12 as output

pinMode(11, OUTPUT); - sets the LED at pin 11 as output

LEDdelay = 100; - sets the value of LEDdelay as 100

digitalWrite(LED_BUILTIN, HIGH); - turns on the LED at pin 13 [When LED_BUILTIN is replaced by a number less than 13, the pin used will change accordingly to that number]

delay(LEDdelay); wait for a specified amount of milleseconds where the amount equals the value of LEDdelay

digitalWrite(LED_BUILTIN, LOW); - turns off the LED at pin 13 [When LED_BUILTIN is replaced by a number less than 13, the pin used will change accordingly to that number]


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


A problem I encountered was that initially, the LEDs flashed in a strange pattern as shown in the video below


(its in portrait mode again......)

I was not sure why the LEDs were flashing like that. After asking my classmates, I had realised that in the code, for the high and low, the pin number was wrong, leading to the strange flashing. Once the code was fixed, I decided to decrease the delay in between the blinks for fun.


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




Output devices: Include push button to start/stop the previous task


1. Below is the code/program I have used and the explanation of the code.

Code/program in writable format

​Explanation of the code

int LEDdelay = 0;


void setup()

{

//start serial connection

Serial.begin(9600);

//configure pin 2 as an input and enable the internal pull-up resistor

pinMode(2, INPUT_PULLUP);

pinMode(LED_BUILTIN, OUTPUT);

pinMode(12, OUTPUT);

pinMode(11, OUTPUT);

}


void loop()

{


//read the pushbutton value into a variable

int sensorVal = digitalRead(2);

//print out the value of the pushbutton

Serial.println(sensorVal);

if (sensorVal == LOW) {

LEDdelay = 400;

digitalWrite(LED_BUILTIN, HIGH);

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

digitalWrite(LED_BUILTIN, LOW);

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

digitalWrite(12, HIGH);

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

digitalWrite(12, LOW);

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

digitalWrite(11, HIGH);

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

digitalWrite(11, LOW);

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

}

}


int LEDdelay = 0; - creates an integer variable called LEDdelay, and of starting value 0

Serial.begin(9600); - Serial. begin() establishes serial communication between your Arduino board and another device.


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

pinMode(LED_BUILTIN, OUTPUT); - sets the LED at pin 13 as output

pinMode(12, OUTPUT); - sets the LED at pin 12 as output

pinMode(11, OUTPUT); - sets the LED at pin 11 as output

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 == LOW) - if button is pressed, do the following {}

LEDdelay = 400; - sets the value of LEDdelay as 400

digitalWrite(LED_BUILTIN, HIGH); - turns on the LED at pin 13 [When LED_BUILTIN is replaced by a number less than 13, the pin used will change accordingly to that number]

delay(LEDdelay); wait for a specified amount of milleseconds where the amount equals the value of LEDdelay

digitalWrite(LED_BUILTIN, LOW); - turns off the LED at pin 13 [When LED_BUILTIN is replaced by a number less than 13, the pin used will change accordingly to that number]

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

Nil



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

Nil


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



Arduino Practical

In this practical, we were tasked to use an Arduino board and simple components (servo, LEDs, etc.) to make it more exciting.


The end product is assessed based on the criteria below:

1.Perform its main function (flapping)

2.Compact & 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 function


For the practical, I worked in pairs, along with Dylan. He mostly focused on the Arduino and programming while I focused on brainstorming on how to meet the assessment criterium. I first began by assembling the unicorn while Dylan researched some Arduino codes online that we could potentially use.

The unicorn parts were cut out on a piece of cardboard, similar to how you would assemble Gunpla.



ree

ree
















Instructions for assembly Cardboard horse parts



ree

Fully assembled unicorn


After the unicorn was assembled, Dylan and I had to think of how to use the servo to make the wings flap naturally and reliably. We had to take in consideration the criterium as well as the space constraints. Initially, we wanted to try to conceal the servo completely within the body of the unicorn, but deemed it unfeasible as the body was too narrow for the servo to fit without stretching it. Hence, we decided to place the servo outside.


ree

Brainstorming in progress 🤔🤔🤔🤔🤔


To make the wings flap, we decided to use the metal wires provided to tie a servo output around the base of the wings inside the unicorn. The code we used was a simple code to make the servo turn 90 degrees. The servo and the unicorn was taped to a base to fulfill the second and third criteria of reliability and compactness.



To fulfill the fourth criteria of aesthetic, I decided to tape a boundary around the base of the unicorn to blend the tape that held the servo down. I also coloured the hair and horn of the unicorn rainbow as well as draw a rainbow 'cutie mark' on the side of the horse to make it resemble rainbow dash from my little pony. (i am not a brony)


ree

ree

To fulfill the 5th criteria of having other functions beside the wing flaps, I decided to use the same mechanism that makes the wings move, but on the tail instead. We also wanted to add music to our board, but making it play and having the wings flap at the same time was much harder to program than we had expected. In the end, we chose to have the music play, then the wings flap and repeat.



ree

^Our final unicorn design and run



^Internal mechanism that makes the tail and wings flap (idk why WIX hates portrait videos........)


Arduino board code:


​Code/program in writable format

Explanation of the code

​#include "pitches.h"


// notes in the melody:

int melody[] = {

NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4

};


// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {

4, 8, 8, 4, 4, 4, 4, 4

};


#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

int piezoPin = 8;

int length = 15;

char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc "; // a space represents a rest

int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };

int tempo = 300;


void playTone(int tone, int duration)

{

for (long i = 0; i < duration * 1000L; i += tone * 2)

{

digitalWrite(piezoPin, HIGH);

delayMicroseconds(tone);

digitalWrite(piezoPin, LOW);

delayMicroseconds(tone);

}

}


void playNote(char note, int duration)

{

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

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

{

if (names[i] == note)

{

playTone(tones[i], duration);

}

}

}


void setup() {

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

// iterate over the notes of the melody:

pinMode(piezoPin, OUTPUT);

}



void loop() {

{

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

{

if (notes[i] == ' ')

{

delay(beats[i] * tempo);

} else

playNote(notes[i], beats[i] * tempo);

delay(tempo / 2);

noTone(8);

}

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

{

for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(5); // waits 5ms for the servo to reach the position

}

for (pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(5); // waits 5ms for the servo to reach the position

}

}

}


​​#include"pitches.h" - refers to the inclusion of all the pitches in a library so that the code can refer to the library if the code is being called.

int melody[] - this is an array where integer information are stored in melody array.

int noteDurations[] - this stores information of the note duration

#include<Servo.h> - refers that it includes Servo's Library

Servo myservo;- this line refers to create a servo object named 'myservo'

int pos = 0; - creating an integer variable called pos, and of starting value, 0

int piezoPin = 8; - creating an integer variable called piezoPin, and of starting value, 8

int length = 15; - creating an integer variable called length, and of starting value, 15

char notes[] - stores information of the notes, unlike int [], this stores characters / letters

int beats[] - stores information of the beats

int tempo = 300; - creating an integer variable called tempo, and of starting value, 300

voidplayTone(int tone, int duration) - section which plays the tone

for (long i = 0; i < duration * 1000L; i += tone * 2) - long signifies that the target type will have width of at least 32 bits. Int i is set to 0. If i is less than the duration multiplied by 1000 (L tells the compiler that the number is of type long), i value will become i value + tone multiplied by 2. For this command, it will do the following {} for each new value of i

digitalWrite(piezoPin, HIGH); - turns the piezoPin on

digitalWrite(piezoPin, LOW); - turns the piezoPin off

delayMicroseconds(tone); - delay amount of microseconds where amount is equal to value of tone

voidplayNote(char note, int duration) - section which plays the notes

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; - stores information of note names

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; - stores information of tone

for (int i = 0; i < 8; i++) - integer i is set to 0. When i lesser than 8, i will increase by 1. For this command, it will do the following {} for each new value of i

if (names[i] == note) - if value of names is equal to note value, do the following {}

playTone(tones[i], duration); - play tone value and duration value

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

pinMode(piezoPin, OUTPUT); - setting piezoPin as an output

for (int i = 0; i < length; i++) - i is equal to 0. When value of i is less than length value, value of i will increase by 1. For this command, it will do the following {} for each new value of i

if (notes[i] == ' ') - if value of notes is equal to ASCII value of ' ', do the following {}

delay(beats[i] * tempo); - wait for beat value multiplied by tempo

else playNote(notes[i], beats[i] * tempo); else play note value and beat value multiplied by tempo

delay(tempo / 2); - wait for tempo value divided by 2

noTone(8); - stop playing tones on pin 8

for (int i=0; i < 10; i++) - i value is equal to 0. When i is less than 10, value of i will increase by 1. For this command, it will do the following {} for each new value of i

for (pos = 0; pos <= 90; pos += 1) - set pos to 0. When pos is less than 90, value of pos will increase by 1. For this command, do the following {}

myservo.write(pos); - It will write the value of pos as and be sent to the servo motor.

delay(5); - wait for 5 milleseconds

for (pos = 90; pos >= 0; pos -= 1) - set pos to 90. When pos is greater than 0, value of pos will decrease by 1. For this command, do the following {}




Below is my Learning Reflection on the overall Arduino Programming activities


Even though I was from the infocomm club in secondary school, I was quite unfamiliar with Arduino programming. In my secondary school, I mostly did app design and programming as well as Scratch Animation and drag-and-drop programming, hence, coded programming was an unfamiliar thing to me. Nonetheless, having the chance to play and use the Arduino and MakerUno has been very fun and beneficial. Programming in today's age is very relevant and up-and-coming as tech and AI will likely be the driving factors for Industry 4.0 and more.


The experience of using the MakerUno kit itself had its ups and downs as it was frustrating whenever our program wouldn't go as we had thought, but oh so satisfying when it did. It was a little bit of an inconvenience to write this blog as I had to redo the 4 activities above as when I had did it the first time, I had only taken videos of the things moving, but did not save the code as the BrightSpace did not tell us to. So I had to redo every single activity again which was a massive inconvenience.


But overall, the experience has been very fun and would aid greatly in my FYP if necessary as well as in my future work.

 
 
 

Comments


CP5070-2022-2B02-Group2-SuenKaChunDarel

bottom of page