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.

2 comments:

  1. Pretty cool I tried it myself.

    ReplyDelete
  2. how can i provide variable delay to serially print something delay being provided by an outside interface, say a volume control

    ReplyDelete