Arduino Programming
Arduino Programming
For the Arduino Programming blog entry, we are mainly going to talk about this few points below
Documentation of programmed arduino boards and explaining the program codes used
Documented what I have learned from interfacing an input device to arduino board
Documented what I have learned from interfacing an output device to arduino board
Explained the problems faces and how I fixed them
Short video of th execute program work
Reflection on arduino programming
Introduction to Arduino and Maker Uno Board
Arudino is an open source platform which uses easy-to-use hardware and software to build electronics projects. It was made in mind to be more accessible to people of other professions and anyone interested in creating interactive objects or environments. There are many type of arduino variants as each board fits a different purpose/need and even SP have their own variant of the arduino board.
Figure 1:Maker Uno Board
The figure above is a Maker Uno Board that we are currently using for all our arduino programming work.It is my first time dealing with programming and I am excited to learn more about this.
The maker uno board consists of 10 different parts.It has
Piezo Buzzer slide switch
Piezo Buzzer
Power Pin
Analog Pin
Micro USB B Type connector
Rest Button
Programmable Button
Series of LED for Digital I/O
PWM Pin
Digital Pin
Figure 2:Position of components
Documentation of programmed arduino boards and explaining the program codes used
There were 4 tasks assigned to us to get more familiar with the e arduino software and learn how to program it to the way we want it to work.
The 4 tasks are
Hello World!
Make some noise!
Servo
Hello world!
Firstly we will need to ensure that the Maker Uno Board is connected to cothe correct HDMI port.
The next step will be to go to file>examples>01.Basics>Blink
Figure 3:How to get the blink code
A new project code should appear and it should look something like this.
For the activity, our goal is to program the Arduino Board to blink.
Now we will decipher some parts of the code and what purpose do they serve.
Void setup(){
what this does is set up the board’s pin identity.In this blink example, we are using the built-in LED as an output.
Void loop(){
This will cause the code to run on an infinite loop.
*/
// 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_BUILTIN, OUTPUT);
}
// 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
}
What this code does is program the built-in LED which is PIN13 to have a HIGH/LOW which allows the LED to switch on and switch off. In between this action, there is a delay of 1000ms which is 1 second.
You can modify the code to have a longer delay by changing the delay value or changing the BUILTIN_LED to another pin number for the other LED lights to blink
Link for the video to blink
Make some noise
Firstly,we would need to load in the code for make some noise before we begin.
This can be done by going to file>Examples>02. Digital>toneMelody
Figure 4: How to get the code for make some noise
Now we are going to decipher the code for this activity.
#include“pitches.h”
This tells the code to include all the pitches in the library so that the program can refer to the library when the code is being called.
Int melody[]
This is where the integer information is stored in the melody array.
Int noteDurations[]
This is similar to melody, however, it stores information on the note duration.
Code
/*
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.
}
Video for make some noise
Servo
Firstly, we would need to load in the code. We can do this by going to file>Examples>Servo>Sweep.
Figure 5: How to get the code for servo
Let decipher what the code means.
#include<Servo.h>
This refers that it includes Servo’s library.
Servo myservo;
This line refers to create a servo object named ‘myservo
Int pos=0
This line creates an integer variable called pos and has a starting value of 0.
For this activity we would also need 3 male to male dupont wires.
1 Bright colour wire such as red,yellow or white for 5V
1 Dull colour wire such as black,grey or blue for GND
1 Fun colour wire such as orange,green or purple for DATA
We would also need a 180° servo motor with:
Orange wire for DATA
Red wire for 5V
Brown wire for GND
Code for Servo
/* 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 <= 180; 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 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
Video for servo
Activity 1: Potentiometer
Code used for potentiometer activity
// C++ code
//
int sensor_vslur = 0;
int sensor_value = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// helpful single-line comment here
sensor value = analogRead(A0);
// turn the LED on
digitalWrite(LED_BUILTIN, HIGH);
// pause the program for <sensor value> millseconds
delay(1000 * sensor value); // Wait for 1000 * sensor value millisecond(s)
// turn the LED off
digitalWrite(LED_BUILTIN, LOW);
// pause the program for <sensor value> millseconds
delay(1000 * sensor value); // Wait for 1000 * sensor value millisecond(s)
}
Problems Faced
We did not face any major problems during the activities as we followed the online package closely and ensured that we understand the objective of the activity and knew what we were doing.
Reflection on Arduino Programming
Having gone through this lesson on Arduino programming, I felt that I have addressed my long-term fear of doing programming as it always seems very foreign and looks very hard for someone who is new to it. I felt that this lesson will be very useful for me to apply in the near future as it is key to have such an important skill to be more appealing to employers. I can see Arduino programming being applied in our CPDD projects and making it easier for us to do things that will be particularly useful. I can use this skill in this future as most of the things in the future will be automated and they would need programmers to programe codes for all these automated machines to work. This would increase productivity as we can divert our human resources to more important work. I will continue to find out more about coding on resources such as youtube or online forums which can allow me to have better knowledge of programming and I can also improve on my work.
Comments
Post a Comment