Learning Arduino

I’ve been reading Beginning Arduino by Michael McRoberts (2010). The book doesn’t have real pictures of each project. I’ve taken photo of each project I finished using a Arduino Uno and a small breadboard.

I’ll add circuit schematics after I learn circuit drawing program on Debian GNU/Linux.

5mm Green LED, 100ohm Resistor, two jumper wires

Three LEDs (red, green, yellow), 3x 100 Ohm resistors, 4x jumper wires

10x LEDs (red), 10x 100 Ohm resistors, jumper wires

6x LEDs (red), 1x 5k Ohm Potentiometer

1x LED, 1x 100 Ohm Resistor

3x LEDs(red, green, yellow), 3x 100 Ohm Resistors. I didn’t have a blue LED.

1x Piezo disc, 1x 2-Way screw terminal

1x Piezo disc, 1x 2-Way screw terminal, 1x LED, 1x 1M Ohm Resistor

1x Piezo disc, 1x 2-Way screw terminal, 1x LDR (Light Dependent Resistor), 1x 10K Ohm Resistor

Source code

I avoid mixing uppercase and lowercase in programming codes. Below codes are identical to the ones on the book except user defined variables and function names are all lowercase letters.

Project 1 – LED Flasher
// Project 1 - LED Flasher

int led_pin = 10;

void setup() {
pinMode(led_pin, OUTPUT);
}

void loop() {
digitalWrite(led_pin, HIGH);
delay(1000);
digitalWrite(led_pin, LOW);
delay(1000);
}

Project 2 – S.O.S Morse code signaler
// Project 2 - S.O.S Morse code signaler

// LED connected to pin 10
int led_pin = 10;

// run once
void setup()
{
// sets the pin as output
pinMode(led_pin, OUTPUT);
}

// main
void loop()
{
// 3 flashes - S
for (int x=0; x<3; x++) {
digitalWrite(led_pin, HIGH); // the LED is on
delay(150); // waits for 150ms
digitalWrite(led_pin, LOW); // the LED is off
delay(100); // waits for 100ms
}

// 100ms delay to cause slight gap between letters
delay(100);

// 3 flashes - O
for (int x=0; x<3; x++) {
digitalWrite(led_pin, HIGH);
delay(400);
digitalWrite(led_pin, LOW);
delay(100);
}

delay(100);

// 3 flashes - S
for (int x=0; x<3; x++) {
digitalWrite(led_pin, HIGH);
delay(150);
digitalWrite(led_pin, LOW);
delay(100);
}

// waits for 5 seconds for repeating the SOS signal
delay(5000);

}

Project 3 – Traffic lights
// Project 3 - Traffic lights

int led_delay = 10000; // delay in between changes is 10 seconds
int red_pin = 10;
int yellow_pin = 9;
int green_pin = 8;

void setup()
{
pinMode(red_pin, OUTPUT);
pinMode(yellow_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
}

// main
void loop()
{
digitalWrite(red_pin, HIGH); // the red light is on
delay(5000); // wait 5 seconds

digitalWrite(yellow_pin, HIGH); // the yellow light is on
delay(2000); // wait 2 seconds

digitalWrite(green_pin, HIGH); // the green light is on
digitalWrite(red_pin, LOW); // the red light is off
digitalWrite(yellow_pin, LOW); // the yellow light is off
delay(led_delay); // wait led_delay milliseconds

digitalWrite(yellow_pin, HIGH); // the yellow light is on
digitalWrite(green_pin, LOW); // the green light is off
delay(2000); // wait 2 seconds

digitalWrite(yellow_pin, LOW); // the yellow light is off

}

Project 4 – Interactive Traffic Lights
// Project 4 - Interactive Traffic Lights

int car_red = 12; // the car lights
int car_yellow = 11;
int car_green = 10;

int ped_red = 9; // the pedestrian lights
int ped_green = 8;

int button = 2; // button pin
int cross_time = 5000; // time allowed to cross
unsigned long change_time; // time since button pressed

void setup()
{
pinMode(car_red, OUTPUT);
pinMode(car_yellow, OUTPUT);
pinMode(car_green, OUTPUT);
pinMode(ped_red, OUTPUT);
pinMode(ped_green, OUTPUT);

pinMode(button, INPUT);

// turn on the green light
digitalWrite(car_green, HIGH);
digitalWrite(ped_red, HIGH);
}

// main
void loop()
{
int state = digitalRead(button);

/* check if button is pressed and it is over 5 seconds since
the last button press */
if (state == HIGH && (millis() - change_time) > 5000)
{
// call the function to change the lights
change_lights();
}
}

void change_lights()
{
digitalWrite(car_green, LOW);
digitalWrite(car_yellow, HIGH);
delay(2000);

digitalWrite(car_yellow, LOW);
digitalWrite(car_red, HIGH);
delay(1000);

digitalWrite(ped_red, LOW);
digitalWrite(ped_green, HIGH);
delay(cross_time);

// flash the pedestrian green light
for (int x = 0; x < 10; x++)
{
digitalWrite(ped_green, HIGH);
delay(250);
digitalWrite(ped_green, LOW);
delay(250);
}

// turn the pedesterian red light on
digitalWrite(ped_red, HIGH);
delay(500);

digitalWrite(car_yellow, HIGH);
digitalWrite(car_red, LOW);
delay(1000);
digitalWrite(car_green, HIGH);
digitalWrite(car_yellow, LOW);

// record the time since last change of lights
change_time = millis();
}

Project 5 – LED Chase Effect
// Project 5 - LED Chase Effect

// create array for LED pins
byte led_pin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int led_delay = 65; // delay between changes
int vdirection = 1;
int current_led = 0;
unsigned long change_time;

void setup()
{
for (int x = 0; x led_delay)
{
change_led();
change_time = millis();
}
}

void change_led()
{
for (int x = 0; x < 10; x++)
{
digitalWrite(led_pin[x], LOW); // turn off all LEDs;
}

digitalWrite(led_pin[current_led], HIGH); // turn on the current LED
current_led += vdirection; // increment by the vdirection value
// change direction if we reach the end
if (current_led == 9) { vdirection = -1; }
if (current_led == 0) { vdirection = 1; }
}

Project 6 – LED Chase Effect with variable delay
// Project 6 - LED Chase Effect with variable delay

// create array for LED pins
byte led_pin[] = {8, 9, 10, 11, 12, 13};
int led_delay = 65; // delay between changes
int vdirection = 1;
int current_led = 0;
unsigned long change_time;

int pot_pin = 2; // select the input pin for the pontentiometer

void setup()
{
for (int x = 0; x led_delay)
{
change_led();
change_time = millis();
}
}

void change_led()
{
for (int x = 0; x < 6; x++)
{
digitalWrite(led_pin[x], LOW); // turn off all LEDs;
}

digitalWrite(led_pin[current_led], HIGH); // turn on the current LED
current_led += vdirection; // increment by the vdirection value
// change direction if we reach the end
if (current_led == 5) { vdirection = -1; }
if (current_led == 0) { vdirection = 1; }
}

Project 7 – Pulsating lamp
// Project 7 - Pulsating lamp

int led_pin = 11;
float sin_val;
int led_val;

void setup()
{
pinMode(led_pin, OUTPUT);
}

// main
void loop()
{
for (int x = 0; x < 180; x++)
{
// convert degrees to radians then obtain sine value
sin_val = sin( x * (3.1412 / 180) );
led_val = int( sin_val * 255 );
analogWrite(led_pin, led_val);
delay(25);
}
}

Project 9 – LED fire effect
// Project 9 - LED fire effect

int led_pin1 = 9;
int led_pin2 = 10;
int led_pin3 = 11;

void setup()
{
pinMode(led_pin1, OUTPUT);
pinMode(led_pin2, OUTPUT);
pinMode(led_pin3, OUTPUT);
}

// main
void loop()
{
analogWrite(led_pin1, random(120)+135);
analogWrite(led_pin2, random(120)+135);
analogWrite(led_pin3, random(120)+135);
delay(random(100));
}

Project 10 – Serial controlled mood lamp
// Project 10 - Serial controlled mood lamp

char buffer[18];
int red, green, blue;

int red_pin = 11;
int green_pin = 10;
int blue_pin = 9;

void setup()
{
Serial.begin(9600);
Serial.flush();
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
}

void loop()
{
if (Serial.available() > 0)
{
int index = 0;
delay(100); // let the buffer fill up
int num_char = Serial.available();
if (num_char > 15)
{
num_char = 15;
}
while (num_char--)
{
buffer[index++] = Serial.read();
}
split_string(buffer);
}
}

void split_string(char* data)
{
Serial.print("Data entered: ");
Serial.println(data);
char* parameter;
parameter = strtok(data, " ,");
while (parameter != NULL)
{
set_LED(parameter);
parameter = strtok(NULL, " ,");
}

// clear the text and serial buffers
for (int x = 0; x < 16; x++)
{
buffer[x] = '';
}
Serial.flush();
}

void set_LED(char* data)
{
if ((data[0] == 'r') || (data[0] == 'R'))
{
int ans = strtol(data+1, NULL, 10);
ans = constrain(ans, 0, 255);
analogWrite(red_pin, ans);
Serial.print("Red is set to: ");
Serial.println(ans);
}
if ((data[0] == 'g') || (data[0] == 'G'))
{
int ans = strtol(data+1, NULL, 10);
ans = constrain(ans, 0, 255);
analogWrite(green_pin, ans);
Serial.print("Green is set to: ");
Serial.println(ans);
}
if ((data[0] == 'b') || (data[0] == 'B'))
{
int ans = strtol(data+1, NULL, 10);
ans = constrain(ans, 0, 255);
analogWrite(blue_pin, ans);
Serial.print("Blue is set to: ");
Serial.println(ans);
}
}

Project 11 – Piezo Sounder Alarm
// Project 11 - Piezo Sounder Alarm

float sin_val;
int tone_val;

void setup()
{
pinMode(8, OUTPUT);
}

// main
void loop()
{
for (int x = 0; x < 180; x++)
{
// convert degrees to radians
sin_val = sin(x * (3.1412 / 180));

// generate a freqency from the sine value
tone_val = 2000 + int(sin_val * 1000);
tone(8, tone_val);
delay(2);
}
}

Project 12 – Piezo Sounder Melody Player
// Project 12 - Piezo Sounder Melody Player

#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494

#define _1 1
#define _2 0.5
#define _4 0.25
#define _8 0.125
#define _16 0.0625

int tune[] = {NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3, NOTE_A3,
NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3, NOTE_E3, NOTE_G3,
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4};

float duration[] = {_8, _2+_8, _16, _4, _4, _2, _2,
_2, _4, _4, _2+_4, _4, _4, _4, _4+_8, _8,
_4, _4, _4, _8, _8, _4, _4, _4, _4,
_2+_4};

int length;

void setup()
{
pinMode(8, OUTPUT);
length = sizeof(tune) / sizeof(tune[0]);
}

// main
void loop()
{
for (int x = 0; x < length; x++)
{
tone(8, tune[x]);
delay(1500 * duration[x]);
noTone(8);
}
delay(5000);
}

Project 13 – Piezo Knock sensor
// Project 13 - Piezo Knock sensor

int led_pin = 9; // Digital pin 9
int piezo_pin = 5; // Analog pin 5
int threshold = 120; // The sensor value to reache before activation
int sensor_value = 0; // store the sensor value
float led_value = 0; // brightness of the LED

void setup()
{
pinMode(led_pin, OUTPUT);
// Flash the LED twice to show the program has started
digitalWrite(led_pin, HIGH);
delay(150);
digitalWrite(led_pin, LOW);
delay(150);

digitalWrite(led_pin, HIGH);
delay(150);
digitalWrite(led_pin, LOW);
delay(150);
}

// main
void loop()
{
sensor_value = analogRead(piezo_pin); // Read the value from the sensor
if (sensor_value >= threshold)
{
led_value = 255; // If knock detected, set brightness to max
}
analogWrite(led_pin, int(led_value));
led_value = led_value - 0.05; // Dim the LED slowly
if (led_value <= 0)
{
led_value = 0; // Make sure the value does not go below zero
}
}

Project 14 – Light Sensor
// Project 14 - Light Sensor

int piezo_pin = 8; // Piezo on pin 8
int ldr_pin = 0; // LDR (Light Dendent Resistor) on analog pin 0
int ldr_value = 0; // LDR value

void setup()
{
// nothing
}

void loop()
{
ldr_value = analogRead(ldr_pin); // read from the LDR
tone(piezo_pin, 1000); // play a 1000Hz
delay(25);
noTone(piezo_pin); // stop the tone
delay(ldr_value);
}

About janpenguin

Email: janpenguin [at] riseup [dot] net Every content on the blog is made by Free and Open Source Software in GNU/Linux.
This entry was posted in GNU/Linux and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s