Arduino Programming
For this week's CPDD lesson, we are taught to do Arduino programming on the Uno maker board. since the whole class was new to this skill, everyone had to complete 4 task to give us an idea about Arduino programming before we attend the practical.
Activity 1: Hello World!
This activity required us to program the Arduino board so that there is a single light that will blink consistently.
Code:
/*
Blink
Turns an LED on for one second,
then off for one second, repeatedly.
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.
http://www.arduino.cc/en/Tutorial/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_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
}
Video for demonstration:
Challenge Yourself:
This activity is similar to the "Hello World" activity except there is a twist
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(3, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Activity 2: Programmable button
In this activity we had to program the Arduino board so that by click the button on the board it would make the light light up.
Code:
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);
}
}
Video link for demstration:
Challenge yourself:
The twist that was added to this challenge was that we had to program the board so that with a press of the button, the light would blink 5 times.
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 {
//ON PIN13 FOR 5 MS AND OFF
FOR 5MS
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
}
Video link for demonstration:
Activity 3: Make some noise!
For this activity we have to program the board to make music.
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.
http://www.arduino.cc/en/Tutorial/Tone
*/
#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 link for demonstration:
Challenge yourself:
The twist was that we have to combine both the "Programmable button" and "make some noise" in one code so that by clicking the button, it would produce the music
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) {
//DO NOTHING!
} else {
//PLAY TONE!
}
}
Video link for demonstration:
Activity 4: Servo
This activity had us program the board to turn on a servo and make it rotate
Code:
/* 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 = 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 15ms 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 15ms for the
servo to reach the position
}
}
Video for demonstration:
Challenge yourself:
The challenge part makes us program the board so that the servo can rotate back and forth
Video for demonstration
https://youtu.be/mJaoyDHtnlQ
INDIVIDUAL TASK
We were then tasked with an individual assignment which required us to interfaced an input device and also an output device.
INPUT DEVICES
For input device there were 2 tasks assigned, the first being a potentiometer and the second one being a Light Resistant Diode.
POTENTIOMETER
The first task: Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE
I first made a simulation on tinkercad in order to get a rough idea how to set it up later for the Arduino board.LIGHT-RESISTANT DIODE (LDR)
For the next task: Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE
OUTPUT DEVICES
FADE
DC Motor
Reflection
I used to think that Arduino programming would be extremely challenging and hard to learn but, after actually practicing and using it, now I think it is a extremely useful skill to learn and that it would certainly benefit greatly in my capstone project if I were to master this skill. So next I will try to practice and experiment with other circuits to hone my skills even further in Arduino programming
No comments:
Post a Comment