| Figure 1: Potentiometer Analog Input setup |
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) } |
b.Interface an LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.
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) } |
Video:
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) } } |
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 } |
Practical
| 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)
No comments:
Post a Comment