ARDUINO(PRAC 2)
HELLO!! Welcome back to my blog👋!! This week we had a lab practical based on Arduino programming. Before moving on to the practical, we actually had a lesson based package to finish up so we can be more familiarized to Arduino. me and my groupmates documented on how we did our 4 different challenges. So scroll down to see more about our journey👇👇!!
Learning Activity
After learning about what and how an Arduino looks like, we decided to move on to the activities. I did this as a pair with my teammate. I did take some pictures and videos for you to get a reference of what I actually mean.
The first challenge was called “hello world!”. We clicked on the following in order, “Tools” > “Examples” > “ Basics” > “Blink” . our example code looked something like this:
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(, LED_BUILTINOUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
After this, we learnt to translate some of words such as
LED_BUILTIN is the output as well as HIGH translating to on the light while LOW translating to off the light.
I also learnt that if i do wish to on for longer and off for shorter i need to use “delay()” .
We then changed LED_BUILTIN to another number ranging from 2-13. We changed it to 8 and pin 8 on the arduino started blinking. The video is shown below.
Moving on, the second challenge was called, “programmable button”. We clicked on the following in order, “Tools” > “Examples” > “ 2. Digital ” > “DigitalInputPullup” . our example code looked something like this:
/*
Input Pull-up Serial
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
input on pin 2 and prints the results to the Serial Monitor.
The circuit:
- momentary switch attached from pin 2 to ground
- built-in LED on pin 13
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
HIGH when the switch is open, and LOW when it is closed.
created 14 Mar 2012
by Scott Fitzgerald
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial
*/
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(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
}
Then we translated the code. I learnt the following
pinMode(2, INPUT_PULLUP) : it means that pin 2 is the input as well that it will remain HIGH, in order words it will remain on.
int sensorVal = digitalRead(2) : it means that pin 2 is the input and writes the number variable, sensorVal. In other words it is also used as an on and off switch. Hence it will be shown as LOW when pressed and HIGH when released.
Serial.println(sensorVal) : it translates to the valve of sensorVal when pressed.
A picture is shown below when its is not pressed:
From this code when the button is not pressed it will not light up but when pressed it will light up. This can also be seen when sensorVal is high. A video is shown below on what will happen when pressed and not pressed.
We challenged ourselves even further by letting pin 13 blink 5 times before turning off. The code will look like something seen below:
/*
Input Pull-up Serial
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
input on pin 2 and prints the results to the Serial Monitor.
The circuit:
- momentary switch attached from pin 2 to ground
- built-in LED on pin 13
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
HIGH when the switch is open, and LOW when it is closed.
created 14 Mar 2012
by Scott Fitzgerald
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial
*/
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(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
for (int i=0; i < 5; i++)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}}
}
To make this happen,we replaced digitalWrite(13, HIGH) with
for (int i=0; i < 5; i++)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
We began to translate some words which are :
int i=0 : it means the temporary integer “i” equals to 0.
I++ : it means “i” value + 1.
The video of the arduino blinking 5 times is shown below:
Moving on, we went to the third challenge called, “ Make some noise!” .We clicked on the following in order, “Tools” > “Examples” > “ 2. Digital ” > “toneMelody” . our example code looked something like this:
/*
Melody
Plays a melody
circuit:
- 8 ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
*/
#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
};
void setup() {
// 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() {
// no need to repeat the melody.
}
We translated some words such as:
#include "pitches.h" : it means that the code can refer to the library of pitches when the code is being called out.
int melody[] : it means where integer information are being stored in a melody array .
int noteDurations[] : it means quite similar to melody except it stores notes duration information.
In the loop, int thisNote refers to the declaration of temporary variable.
The video of the arduino playing is shown below:
We decided to challenge ourselves by combining both the button and the melody. The code will look something like shown below.
/*
Input Pull-up Serial
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
input on pin 2 and prints the results to the Serial Monitor.
The circuit:
- momentary switch attached from pin 2 to ground
- built-in LED on pin 13
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
HIGH when the switch is open, and LOW when it is closed.
created 14 Mar 2012
by Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/InputPullupSerial
*/
#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
};
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(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
} else {
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);
}
}
The video of this occurring on the arduino is shown below:
We moved onto the last challenge called the “servo” . This involved a servo motor which moves to an angle which is imputed. The motor we used ranged from 0 - 180o . continuous servos move at a full 360o . We clicked on the following in order, “Tools” > “Examples” > “ servo ” > “sweep” . our example code looked something like this:
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/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 = 20; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 150; pos >= 20; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
We did some translation which consisted of:
Servo myservo : this means to create a servo object called myservo.
int pos = 0 : this means creating a variable called pos and its starting value is 0.
myservo.write(pos) : this means it will write the value of pos and send to the motor.
We then proceeded to get the wires to connect the motor to the arduino. We took any 3 colours to connect it. We took the brown of the servo to connect to the GND ; red to 5V ; orange to DATA.
The video of the servo motor moving from 20 to 150 and back to 0 is shown below:
// C++ code
//
int photosensor = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop()
{
photosensor = analogRead(A0);
Serial.println(photosensor);
analogWrite(9, map(photosensor, 0, 1023, 0, 255));
delay(100); // Wait for 100 millisecond(s)
}
Now moving onto our actual practical, we were tasked to make a cardboard made unicorn and make its wings flap using Arduino. We did make a procedure on how we made our unicorn's wings flap. So scroll down to learn more about it 👇👇.
PRACTICAL: ARDUINO FLAPPY WINGS UNICORN
Are you interested in having a flapping unicorn of your own? Well, not to worry, as we have just the way for you to create your own! Here is an Instruction Manual to guide you along!
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 which looks something like this :
Next, you will separate the parts from the template and categorise them to which part of the unicorn’s body it forms. Here is a reference for you to follow:
Once your parts have been properly group, 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 1
Figure 1: Arduino code
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 2 down below. After doing so, what you should being seeing on your screen is as shown in figure 3.
Figure 2: Arduino servo
Figure 3:Arduino servo sweep
Figure 4: final Arduino code
Once you have arrived at the screen as shown in figure 3, 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 4 (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 4)
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
}
}
LINK TO OUR FINAL CODE:https://drive.google.com/uc?export=download&id=1u8q87_28DU4WGkCMugkZxQ9Zg3R47Zx4Tshxizdm4Os
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
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 does not matter), your servo motor and your arduino maker uno. Follow the connection as shown in figure 5. 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 5
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)
REFLECTION:
In this practical, I learnt more about using arduino as well as coding to make some objects to do certain actions. At first we did some elimination of ideas and came up with 1 main idea of using arduino as well as the servo to make the wings of the unicorn flap. For the servo we had to try changing the angles in the code for servo. But after many tries, we got an angle we were satisfied with. We did encounter some difficulties such as the placement of our servo. We had to place the servo perpendicular to the table,this is due to the wings being able to flap only in a certain position , hence the placement was really important. We tried so many different placements and with the help of the lecturers,we finally got a good placement that all of us were happy with.after getting the placements, we were figuring out how we were going to place it inside the unicorn without gluing it. We did not want to glue the servo onto the unicorn as it is much harder to remove it again if we do want to change position. But we saw other groups gluing the servo , and our lecturer did tell us that after the glue hardens, it is able to be removed. Hence we all decided to glue it inside. We also wanted to add extra things such as adding music or lights around the unicorn. But we had difficulty in finding music codes that other groups are not using. But we did not get demotivated by the fact we could not add extra stuff to our unicorn. We instead decorated our unicorn to be really pretty. This made our unicorn the most eye-catching out of all the groups. One good thing our group did was, distributing the roles to each one of us, so that everyone has contributed something to this practical. We did have certain difficulties mainly on our code as well as the placement of our servo. We did have an emergency at the end, in which the servo’s arm actually fell off when we were doing our final test run. Thankfully we all did not panic and we exchanged ideas on fixing it. We calmly fixed the servo’s arm and felt very relieved. Learning about coding using arduino, made it more interesting since we were never exposed to this in much detail. Overall, this practical was very fruitful and I am more familiar with coding using arduino as well as it made me more resilient and not to give up after one failure. I am glad everyone of my groupmates left with happy smiles on our faces.
Comments
Post a Comment