Sunday, January 16, 2011

Project 4 - Motion Activated LED


/*
Project 4 - Motion Activated LED

LED will blink if the Motion Sensor Detects Motion

http://arduinosketches.blogspot.com
*/
int ledPin = 2; // Sets LED variable
int switchPin = 0; // Sets Motion Sensor variable
int value = 0; // Motion Sensor data variable

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(4, OUTPUT);
}
void loop() {
value = digitalRead(switchPin); // Get data from Moton Sensor
if (HIGH == value) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

This project uses a PIR Motion Sensor to detect motion and activate an LED. The Motion Sensor uses a 3 pin connector and is connected to the GND, VIN, and the digital 0 pin on the Arduino. While this setup is extremely simple, the motion sensor only detects motion or no motion. How fast or how far you move does not influence the LED in anyway.

I wish I could find the original source of who developed this simple motion sensing code, and I believe SparkFun.com may have been a starting point while searching for this code example.

Project 3 - Fade 2 LEDs


/*
Project 3: Fade 2 LEDs

Alternates Fades between 2 LEDs

http://arduinosketches.blogspot.com/
*/

int i = 0; // Integer used for counting

void setup() {
pinMode(11, OUTPUT); // Sets LED Pins
pinMode(9, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
}

void loop(){

for (i = 0; i < 255; i++) { // loops from 0 to 254 (fade in)

analogWrite(9, i); // set Red LED brightness
analogWrite(3, -i); // set Green LED brightness
delay(10); //Without the delay, the fades are not visible
}
for (i = 255; i > 0; i--) { //loop from 255 to 1 (fade out)
analogWrite(9, i); //set Red LED brightness
analogWrite(3, -i); //Green the LED brightness
delay(10);
}
}



This script alters the intensity of light emitted from 2 LEDs. By using the 'analogWrite' function, we tell the Arduino which LED to fade, and at what interval based on the current count of our integer 'i.'

For an extra challenge, try adding a third LED and fade the LEDs from left to right, and back again.

Thursday, January 13, 2011

Arduino Project 2 - Variable Delay Blinking LED



/*
Blink - Variable Delay

Blinks an LED at accelerating and decelerating delay times.
This example code is in the public domain.

http://arduinosketches.blogspot.com/
*/

float rest, delayTime, delayChange;
boolean check;

void setup() {
// initialize the digital pins as outputs.
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
delayTime = 200;//Set the initial Delay Time
}

void loop() {
rest = delayTime;
//Check to see whether to accelerate or decelerate the delay
//based on the value of the boolean(check)
if(check == false){
delayChange = 10;
delayTime = delayTime - delayChange;//Decrease delay
}else{
delayChange = 20;
delayTime = delayTime + delayChange;//Increase delay
}
digitalWrite(8, HIGH); // turn LED ON
delay(rest);//LED ON for length of (rest)
digitalWrite(8, LOW); // turn LED OFF
delay(rest);//LED OFF for length of (rest)
//--Switches the Boolean(check) once limits have been met
if(rest <= 50){ check = true; } if(rest >= 210){
check = false;
}
}


Day 2 of Arduino Projects.

This code blinks a Blue LED at varying speeds. Caught in an endless loop, the LED will simply blink slower and faster until the unit is powered off.

This bit of code differs from the first project in that variables and a boolean are defined so that they may be dynamically altered as the program is run.

Many more types of equations could be used to increase and decrease the blinking rate, so it will be up to you to apply what you know about physics to give an interesting blink rate to the LED.

Arduino Project 1 - 3 Blinking LEDs



/*
Blink - 3 LEDs
Turns on 3 LEDs in succession
This example code is in the public domain.
*/
void setup() {
// initialize the digital pins as outputs.
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(4, OUTPUT);
pinMode(2, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH); // Green LED on
delay(100); // wait
digitalWrite(13, LOW); // Green LED off
delay(500); // wait
digitalWrite(7, HIGH); // Blue LED on
delay(200); // wait
digitalWrite(7, LOW); // Blue LED off
delay(50); // wait
digitalWrite(2, HIGH); // Red LED on
delay(75); // wait
digitalWrite(2, LOW); // Red LED off
delay(200); // wait
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
delay(350);
}

This is the first Arduino project I've created, based on the first project in the book Getting Started with Arduino. The first project in this book has you create a single blinking LED, but I had 3 LEDs to try out so I added the lines for the additional lights.

The Arduino is a device I was fortunate to hear about last year. It has become even more popular since then, and will probably become even more popular thanks to a recent documentary about the device, available at Vimeo.

Arduino The Documentary (2010) English HD from gnd on Vimeo.


What makes this device special is that it allows for the easy prototyping of electronic devices. This means that never before has it been easier to take your old electronics and turn them into a new machine, controlled by a program you write yourself. There is also a growing resource of other Open Source projects available, which can then be shared worldwide.

The best part is, the software and most of the learning materials are all available for free at:
http://www.arduino.cc

The Arduino board itself can be bought at:
http://www.sparkfun.com
30 Arduino Projects for the Evil Genius