Sunday, November 28, 2021

Practical 2: Arduino Programming

Welcome to my week 4 blog🍉

This week we tried out how to write or modify a program in Arduino IDE and upload it to MAKER UNO Edu Kit, We also went through the practical session.

Objective: 
1. Familiar with the components in the maker uno kit.
2. Individual Arduino tasks.
3. Individual competency test
4. Group challenge

This activity will help us to understand some basic mathematics symbols and their interpretations of Arduino, at the end of this activity, I should be able to:
1. Modify the program and upload it to the uno board as required.
2. Decipher the code given.
3. Modify the code according to different needs


Individual Work on Input and Output Devices
a. Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE.
Figure 1: Potentiometer Analog Input setup

Simulation:

The LED is connected to pin13, a resistor is also connected to the LED  which is to prevent excess current goes through the LED and burning it out. The red wire is connected to "5V", the green wire is connected to "A0" and the brown wire is connected to "GND". The next step is to come up with the code, the analog signal (from 0 to 1023) is the input and the LED will show the output.

Code in Block

Code in text

 


// C++ code

//

int SensorValue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}

 

void loop()

{

  SensorValue = analogRead(A0);

  // turn the LED on

  digitalWrite(LED_BUILTIN, 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)

}


Table 1: code used

Video:
Vid 1: Potentiometer

This video shows that when the resistance of the potentiometer is being adjusted, the brightness of the LED will be controlled accordingly. As when resistance increases, the current flowing through the LED decreases, hence the LED will be dimmed. When resistance decreases, the current flowing through the LED increases, hence the brightness increases accordingly.

b.Interface an LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.

Figure 2: Setup of an LDR

An LDR is a light-dependent resistor, it is also called a photoresistor. The resistance changes with the intensity of the light, as light intensity increases, the resistance of the LDR decreases.

Simulation of LDR:

A simple code used:

Code in Block

Code in text

 


// C++ code

//

int SensorValue = 0;

 

int Photosensor = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}

 

void loop()

{

  SensorValue = analogRead(A0);

  digitalWrite(LED_BUILTIN, HIGH);

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

  digitalWrite(LED_BUILTIN, LOW);

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

}

Table 2: code used

So basically the input is the LDR A0, and the output is the LED pin13. When LED lights up, low resistance is experienced by the LDR and vice versa.

Video:
Vid 2: LDR Changes with light intensity

When the light is shone onto the photoresist, the LED is dimmed, a similar concept as the potentiometer. As the room light is not strong enough, so I used the phone's torchlight to shine on the LDR.
Code used: Code(a more complicated code from MERT Arduino & Tech)

What I learned from the input device:
I learned that input devices use analog and as I have to pay attention to the connection point of the cathode and anode of the LED. I messed up the connections a few times because the holes on the tinkercad is really small and it is difficult for me to find the position of the correct point.


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

Figure 4: Fading of LED


simulation:


code:

Code in block

Code in text

 




// C++ code

//

int Brightness = 0;


void setup()

{

  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(9, OUTPUT);

}


void loop()

{

  for (Brightness = 0; Brightness <= 255; Brightness += 5) {

    analogWrite(11, Brightness);

    analogWrite(10, Brightness);

    analogWrite(9, Brightness);

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

  }

  for (Brightness = 255; Brightness >= 0; Brightness -= 5) {

    analogWrite(11, Brightness);

    analogWrite(10, Brightness);

    analogWrite(9, Brightness);

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

  }

}


Since we want to light up 3 LEDs, so pins 11, 10, and 9 are used. The LEDs will light up from signal 0 to 255, then it will fade out from a signal of 255 to 0. The first thing is to come out with the fading function of 1 LED, then we just need to appoint the pin numbers for the other 2 LEDs.

video:

Vid 3: Fading of LED
Guide: Vid guide


d.Interface the DC motor to maker UNO board and program it to on and off using a push button on the board



Figure 5: setup for dc motor



Simulation:


Code used:

Code in Block

Code in text

 


// C++ code

//

int buttonstate = 0;

 

void setup()

{

  pinMode(2, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}

 

void loop()

{

  buttonstate = digitalRead(2);

  if (buttonstate == 1) {

    digitalWrite(LED_BUILTIN, HIGH);

  } else {

    digitalWrite(LED_BUILTIN, LOW);

  }

  delay(10); // Delay a little bit to improve simulation performance

}

Table 4: code used for dc motor

For this code, the pushbutton is set to pin2, when the button is pressed, the motor is activated, when the button is released, the motor will be stopped. Hence a condition is introduced to control the input signal and hence the output which is the motor shows the outcome.

What I learned from the output device: 
I felt that the output device is more straightforward than the input, even though they have similar concepts. The positive wire is always connected to "5V" and the negative wire is connected to the "GND". 

Reflection for individual tasks:

It took me a really long time to figure out the setup of the Arduino board using the tinkercad as it was my first time trying the Tinkercad. For the coding part, I think the tip is to identify the basic relationship between the input and the output and put them into different devices we would use. That can give a quick start on the code, but I still need to modify the code a few times to achieve the desired outcome. I felt the easiest one is the 3 LEDs, maybe it is because we have covered similar things during the experiment, so I felt more convenient doing that. But for the rest, I think it was fun but a little bit difficult to try it out on both the tinkercad and the real Arduino board. I did refer to different videos to help myself to understand the concepts and the working principles behind the codes, and  I tried to come out with the codes by myself as well. Overall, I think these tasks are challenging but also interesting.


Practical


Competency test:
1. Traffic Light (Make pins 8,10,12 light up one by one with a delay of 1 second)
2. Check for computer number


Group Challenge: Arduino Flappy wings unicorn

Final Product:
Figure 6: Final Product




Instruction Manual:

SUPPLIES:

  • Computer with the Arduino software installed x 1

  • Arduino uno x 1 

  • Cardboard made unicorn x 1

  • A servo motor x 1

  • Jump wires (As many)

  • Large Cardboards x 1/2

  • Super Glue /hot glue x1

  • Metal wire x 1/2

  • Marker pens (As many)


STEP 1: CONSTRUCTING THE CARDBOARD UNICORN


The first step to constructing your magical and wonderful unicorn. You will require a template that looks something like this :



Next, you will separate the parts from the template and categorize them to which part of the unicorn’s body it forms. Here is a reference for you to follow:

Figure 7


Once your parts have been properly grouped, you are now ready to assembly your unicorn. Tip: Form each body part separately and then combine them in the end.




Here is what all your parts are supposed to look like before the FINAL assembly:

Head












Body


Finally, you can put all the parts together. By adding the mane, tail and wings, you will successfully get your UNICORN.

Additional tip: At the end of your project, you can glue the parts together for extra security








STEP 2:PROGRAMMING

So for this project we will be using the Arduino software to modify a servo code to use as a mechanism for the wings of the unicorn to flap. Firstly, let us now open our Arduino software. You will be brought to this page as shown in figure 8.

Figure 8


So what you will do next is to click on ‘Files’, click on ‘Examples’, click on ‘servo’ and select ‘sweep’. If you are still unsure of how to do it, you can refer to figure 9 down below. After doing so, what you should be seeing on your screen is as shown in figure 10.


Figure 9


Figure 10


Figure 11


Once you have arrived at the screen as shown in figure 10, we next have to modify the code such that the servo will adjust according to how we want the wings to flap in terms of speed and the degree of how the wing flaps.

For this project the values we will be using for the time taken would be ‘1’ for the delay and ‘300’ for the degree. Delay means the time taken for the servo to move from ‘0-300’ degrees. Follow the modifications we have made to the delay and the degree in figure 11 (highlighted in yellow).

If you are still unsure of the modifications, you can just use the modified code below(same code as shown in figure 8)


The modified Arduino code :

/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.


 modified 8 Nov 2013

 by Scott Fitzgerald

 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep

*/


#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


void setup() {

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

}


void loop() {

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

    // in steps of 1 degree

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

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

  }

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

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

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

  }

}

STEP 3: DECORATING(COLOURING AND THE GRASS, CLOUDS)


As we wanted to hide the motor and wires from the unicorn, we decided to make 2 pieces of grass, one on each side of the unicorn’s body. Then we realized that the unicorn is not very stable, so we made a cloud shaped plane to support the unicorn.

To make it more vibrant, we coloured the grass cut outs green and the cloud shaped plane blue.

Materials needed:

  • 2 pieces of grass cut outs

  • 1 piece of cloud plane cut out

Figure 12



STEP 4: FINAL ASSEMBLY (GLUING THE SERVO AND POKE HOLE INTO WING WIRE THINGY)

To connect the Arduino to the servo what you will need are 3 wires(the colours of your wire do not matter), your servo motor and your Arduino maker uno. Follow the connection as shown in figure 8. Connect the brown wire on the servo to the ‘GND’ on the Arduino, the red wire on the servo to ‘5V’ on the Arduino and the orange wire on the servo to ‘PIN9’ on the Arduino.

FIGURE 13


We used a metal wire to connect the servo and the wings of the unicorn together.


We then used hot glue, super glue can also be used, to glue the servo inside the unicorn at the side, so that it won't be very obvious that it is there.


There it is our very own unicorn flapping its wings by itself USING ARDUINO!!



Placement of the motor in our unicorn(on the left side)


Flapping Unicorn:
Vid 4: flapping unicorn





Reflection:

From this practical, I have learnt how a code can change the unicorn so drastically. As if without the code, it just looks like a normal cardboard toy, while with the movement of the wings, it brings it to another level. 
At the start of the practical, we first assembled the unicorn, then we made a quick decision on the kind of code we should use, a smaller spinning angle but a much shorter delay to make a continuous movement of the wings. However we took a while to think about how we should place the motor so it moves both wings together, even if we adjusted for a few positions, the movement is still not ideal. Then we thought about using the metal wire to connect the wings to the spin arm, and it worked well. But the motor moved really vigorously, we were scared that without a support it would damage the body of the unicorn. That was the most frustrating part as we were trying to make support and it did not work, and we had no idea how we can fix this, but we still tried different methods and hopefully, we can make the flapping consistent and the unicorn is steady. Then we realized that other groups just glued the motor onto the body of the unicorn and it was quite steady. We did not want to do this at first because we were afraid that if we glued it wrongly it would be very difficult to take the motor off and that is our main concern. In the last half an hour, we finally fixed the motor by using a hot glue gun to glue it on the inner body side of the unicorn, and it was quite steady. 
Lastly, we can finally put in the decorations we have made much earlier, which are the 2 pieces of grass and 1 piece of cloud plane, even though the blue colour is not very obvious due to the cardboard’s colour. Despite the decorations, we also wanted to add in entrance music but we did not leave much time so we did not complete that within the time given. We also noticed there is 1 lightbulb that we can use to make the eyes shine, but that was too late. In conclusion, I found this practical very interesting and meaningful as the flapping of wings is very cool, and how we can use different codes to achieve this.