top of page
Search

Arduino Programming

  • Writer: isabelleadora
    isabelleadora
  • Dec 3, 2022
  • 4 min read

We started early in week 3 as Arduino programming was something new to all of us and it would take us a while for all of us to get the hang of it. We used the Maker UNO Kit to help us kickstart our Arduino journey.


In this blog, I will show the codes used for the input and output devices we were tasked to work on, explanation of said code, references used, problems encountered & how I fixed them and video evidence of the code working.


Input device - Potentiometer

Code & Explanation

​Code

​Explanation

int sensorValue = 0;

void setup()

{

pinMode(A0, INPUT);

pinMode(13, OUTPUT);

Serial.begin(9600);

}

void loop()

{

sensorValue = analogRead(A0);

Serial.println(sensorValue);

digitalWrite(13, HIGH);

delay(sensorValue);

digitalWrite(13, LOW);

delay(sensorValue);

}

​Reads 0 as an integer value



Sets up pin A0 as an input and pin 13 as an output

Establish the serial communication between Arduino Board and computer



Reads the value from the sensor before displaying the value on Serial monitor

To turn on the LED on and off with a delay given by sensorValue in between




Reference used for code


Problems encountered & how it was fixed

Originally, we connected the Cathode of the LED to the Ground pin and the Anode of the LED to the 5V pin. This caused the LED to not light up. We fixed it by looking online and the guide that the Cathode is the longer leg, hence should be connected to the power source (5V pin) and the Anode is the shorter leg, hence should be connected to the Ground pin.

Video evidence





Input device - LDR

Code & Explanation

​Code

​Explanation

int light;

void setup() {

Serial.begin(9600);

}

void loop() {

light= analogRead(A0);

if(light<100){

digitalWrite(13, LOW);

}else{

digitalWrite(13, HIGH);

}

Serial.println(light);

delay(0);

}

​Sets integer as 'light'


Establish the serial communication between Arduino Board and computer


Reads the voltage value of light passing through pin A0


If the value is less than 100, LED will turn off. Otherwise, it is turned on.




The value will be printed on the serial monitor

Reference used for code https://youtu.be/qKku-mmwNIA


Problems encountered & how it was fixed Initially, in the code, it was written "(light<30)". But when I overed the LDR with my finger, the LED light still remained turned on. After checking the serial monitor, I realised the reading showed it being 80-90 when I covered the LDR with my finger. Hence, I changed it to (light<100), which fixed the problem.


Video evidence




Output device - 3 LEDs

Code & Explanation

​Code

​Explanation

int brightness = 0;

void setup() {

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}


void loop() {

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

}


}

​Creates an integer variable called brightness, and of starting value, 0


Sets up Pins 9, 10 and 11 as the output




The LED will turn on from brightness of 0 to 255 in small increments for outputs 9, 10 and 11. There will be a delay of 150 milliseconds before each output is turned on.




The LED will turn off from brightness of 255 to 0 in small increments for outputs 9, 10 and 11. There will be a delay of 150 milliseconds before each output is turned off.


(Code for void loop shows that the LED will fade from pin 9 to 10)

Reference used for code https://youtu.be/X8dHbdhnGKY


Problems encountered & how it was fixed The LED was not fading in the correct sequence so we changed the output to match it to the correct LED. But overall, the problems encountered for this was minimal.


Video evidence



Output device - Push button to start 3 LEDs

Code & Explanation

​Code

​Explanation

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

}

}

}

}

​​Creates an integer variable called brightness, and of starting value, 0

Establish the serial communication between Arduino Board and computer

Sets up pin 2 as an input and enable the internal pull up resistor (always HIGH by default)

Sets up pin 9, 10 and 11 as the output


Reads the input of pin 2 and writes it as an integer, sensorVal


If the button is not pressed, the LED for 9, 10 and 11 will be turned off.


Otherwise when button is pressed, the LED will turn on from brightness of 0 to 255 in small increments for outputs 9, 10 and 11. There will be a delay of 150 milliseconds before each output is turned on. The LED will turn off from brightness of 255 to 0 in small increments for outputs 9, 10 and 11. There will be a delay of 150 milliseconds before each output is turned off. (Shows that the LED will fade from pin 9 to 10)



Reference used for code


Problems encountered & how it was fixed It took us a while to figure out the code as there were many missing brackets. After adding in the missing brackets, the code managed to work.


Video evidence



Practical

For this practical, we were tasked to make the wings of a cardboard unicorn flap using a servo, and also add in a secondary function. The secondary function we chose was to play music. The code for the servo was easy as we already prepared it for our pre practical. What was hard was finding an angle that would make the wings flap.

After we decided on an angle, we decided to tape the servo inside the unicorn so that it would be compact. Though, there was a lot of laughter from others as the placement of the servo was quite funny. We also did a lot of trial and error for the speed of how fast the wings should flap. The one in the video flapped really wuickly as the delay we input was 0.

We also added music from My Little Pony, a cartoon that some of us grew up with. The code for music was made by Alvin as he is the most musically inclined in our group. We also decided that the wings should flap more slowly as it would be more majestic. We added a slot and tab as well so that it would be easier to dismantle after we finished the practical and designed our unicorn to look like Rainbow Dash from My Little Pony and ta-da! Our final product is shown below.



Overall, it was such a fun practical and the 3 hours in the lab flew by really quickly.

ree

Reflection

I never actually thought that we would be taking programming during my time in DCHE and so, I took an elective to learn. Surprisingly, it also taught Arduino Programming, so I was not completely lost when Dr Noel introduced this to us. However, in CPDD, it challenged me more as the pre-practicals asked us to modify the code in order for a certain outcome to happen. For example, merging the push button code with the music code. Initially, I just copied codes from the resources given to me or online but I did not understand what it meant. However, after a lot of practice, I realised there was a pattern and through trial and error, we can get what we want to achieve. For example, we needed to use ';' to terminate the code and '{}' to enclose multiple lines of code. The Maker UNO kit was definitely not easy to use from the get-go and we used youtube tutorials to learn how to use them. We only had 2 for 4 members so we had to do rotations on who gets the Maker UNO kit. Fortunately, we could use an online simulator called TinkerCAD and I realised we did not have to type out the code one by one of the Arduino application itself. Instead, we could use the code blocks editor to make it easier for ourselves. It will help generate the code for us and it is downloadable.


From the practical, Dr Noel commended us on using slot & tab which is what we learnt from ICPD, and said the reliability of the main function was good. However, he said we could improve more on the aesthetics of the final design and I will keep this in mind for our prototyping.


Overall, this is a very useful skill to have not only in prototyping for CPDD and FYP but especially with the tech sector rapidly rising in terms of job opportunities.

 
 
 

Recent Posts

See All
Hypothesis Testing

In week 14, we learnt about Hypothesis Testing. In this blog, I will be determing whether a factor from the DOE practical affects the...

 
 
 

Comments


bottom of page