/*
Author: voidcast
Created on: 1/7/2012
URL: http://voidcast.wordpress.com
********************************
* Simple Arduino LED flasher *
********************************
Licensed under Creative Commons - Attribution (http://creativecommons.org/licenses/by/3.0/)
You are free to share, modify and use this code for commercial purposes as long as you attribute the
original author
*/
/*
All the LEDs need to be connected to the arduino pins in sequence.
The pin where they start from and where they end need to be put in below.
Everything else remains unchanged
I have used 1K resistors in series with most of the LEDs except for a few which weren't bright enough.
For those I've used 220 Ohm resistors. Arduino allows a max of 40mA of current through each of its pins,
but the recommended amount is not to exceed 20mA. Assuming a worst-case drop of 1V (it's normally 2V) across the LEDs
and given the source to be 5V on the arduino, the current that flows through is (5-1)/1K = 4mA for most of them
and (5-1)/220 = 18mA for the others with lower resistance. These are well within the limits.
Pin numbers can be from 0-13 but keep in mind that pin 13 already has a 1K resistance to it.
*/
int startPin = 1;
int endPin = 12;
void setup()
{
//Setup all the pins for OUTPUT as given below
for(int i=startPin; i<=endPin; i++)
{
pinMode(i, OUTPUT);
}
//Serial.begin(9600);
}
//Turn on the led at the specified pin
void ledOn(int pin)
{
digitalWrite(pin, HIGH);
}
//Turn off the led at the specified pin
void ledOff(int pin)
{
digitalWrite(pin, LOW);
}
/*
Have a single light that traverses the length of the series.
Can be either forward or backward.
*/
void ledSerial(int time, int count, boolean forward)
{
for(int j=1; j<=count; j++)
{
if(forward)
{
for(int i = startPin; i <= endPin; i++)
{
ledOn(i);
delay(time);
ledOff(i);
}
}
else
{
for(int i = endPin; i >= startPin; i--)
{
ledOn(i);
delay(time);
ledOff(i);
}
}
}
}
/*
All the LEDs blink together the specified (count) number of times
*/
void ledBlinkAll(int time, int count)
{
ledReset();
for(int j = 1; j <= count; j++)
{
for(int i = startPin; i <= endPin; i++)
{
ledOn(i);
}
delay(time);
for(int i = startPin; i <= endPin; i++)
{
ledOff(i);
}
delay(time);
}
}
/*
Have the LEDs blink alternately
*/
void ledBlinkAlternate(int time, int count)
{
ledReset();
for(int j = 1; j <= count; j++)
{
for(int i = startPin; i <= endPin; i+=2)
{
ledOn(i);
}
delay(time);
for(int i = startPin; i <= endPin; i+=2)
{
ledOff(i);
}
for(int i = startPin+1; i <= endPin; i+=2)
{
ledOn(i);
}
delay(time);
for(int i = startPin+1; i <= endPin; i+=2)
{
ledOff(i);
}
}
}
//Turn off all the LEDs
void ledReset()
{
for(int i = startPin; i <= endPin; i++)
{
ledOff(i);
}
}
/*
The LEDs light up in order and turn off in order, like a snake that grows and shrinks.
This one can be modified to accept another argument so that you can specify whether you want the "snake"
to grow or shrink. But I figured this is what I would use the most.
*/
void ledStretch(int time, int count, boolean forward)
{
for(int j=1; j<=count; j++)
{
if(forward)
{
for(int i=startPin; i<=endPin; i++)
{
ledOn(i);
delay(time);
}
for(int i=startPin; i<=endPin; i++)
{
ledOff(i);
delay(time);
}
}
else
{
for(int i=endPin; i>=startPin; i--)
{
ledOn(i);
delay(time);
}
for(int i=endPin; i>=startPin; i--)
{
ledOff(i);
delay(time);
}
}
}
}
// Appears as if the light is entering from one side and filling all the LEDs up
void ledFiller(int time, int count, boolean forward)
{
int lastFilledLed = endPin; //Just so that it's not garbage or zero. This is set again depending on whether you want forward or backward.
for(int j=1; j<=count; j++)
{
ledReset();
if(forward)
{
lastFilledLed = endPin;
while(lastFilledLed >= startPin)
{
for(int i=startPin; i<=lastFilledLed; i++)
{
ledOn(i);
delay(time);
ledOff(i);
}
ledOn(lastFilledLed);
lastFilledLed--;
}
}
else
{
lastFilledLed = startPin;
while(lastFilledLed <= endPin)
{
for(int i=endPin; i>=lastFilledLed; i--)
{
ledOn(i);
delay(time);
ledOff(i);
}
ledOn(lastFilledLed);
lastFilledLed++;
}
}
}
}
/*
Two lights start from either end or from the middle and either grow inwards or outwards
This one requires 2 parallel streams which I call "threads" here. As one increases the other decreases.
Odd number of LEDs needs to be handled differently from even number of LEDs
*/
void ledSweep(int time, int count, boolean sweepIn)
{
for(int j=1; j<=count; j++)
{
int numberOfLeds = endPin - startPin + 1;
ledReset();
delay(time);
if(numberOfLeds % 2 == 0) //Even number of LEDs
{
if(sweepIn)
{
int thread1 = endPin;
int thread2 = startPin;
while(thread1 > thread2)
{
ledOn(thread1);
ledOn(thread2);
delay(time);
thread1--;
thread2++;
}
}
else
{
int thread1 = numberOfLeds/2;
int thread2 = thread1 + 1;
while(thread2 <= endPin)
{
ledOn(thread1);
ledOn(thread2);
delay(time);
thread1--;
thread2++;
}
}
}
else //Odd number of LEDs
{
if(sweepIn)
{
int thread1 = endPin;
int thread2 = startPin;
while(thread1 > thread2)
{
ledOn(thread1);
ledOn(thread2);
delay(time);
thread1--;
thread2++;
}
ledOn(thread1); //Turn on the middle Led. At this point, thread1 = thread2 = middleLed
delay(time);
}
else
{
int middleLed = (numberOfLeds + 1)/2 + startPin - 1; //Get the pin number of the middle Led
int thread1 = middleLed - 1;
int thread2 = middleLed + 1;
ledOn(middleLed);
delay(time);
while(thread2 <= endPin)
{
ledOn(thread1);
ledOn(thread2);
delay(time);
thread1--;
thread2++;
}
}
}
}
}
void loop()
{
ledSweep(100,3,true);
ledSweep(100,3,false);
ledSerial(40, 6, true);
ledSerial(40, 6, false);
ledBlinkAll(400, 4);
ledBlinkAlternate(400,8);
ledStretch(50, 3, true);
ledStretch(50, 3, false);
ledFiller(50, 1, true);
ledFiller(50, 1, false);
ledFiller(50, 1, true);
ledFiller(50, 1, false);
}