/*
* Project: LED-Project_3-LED-blinks_revisited
* Author: Jane-Maree Howard
* Date: Saturday 20/03/2010
* Platform: Arduino 18
* Purpose: To demonstrate variable blink times for external 3-colour LED
* Operation: Sets up output pins (4 off) in setup() method;
* Declares pin definition variables (4 off)
* Defines loop() to perform various LED blinking & delay operations
* Here we use a 3-colour LED and first turn them all ON, then OFF.
* Then we successively blink the RED, GREEN & BLUE
* LEDs, each with the same delay time, which ramps
* from 10ms up to 250 millisec & back down again.
* Here we use two 'for-loops': one to ramp up the Delays, &
* the other to ramp them back down again.
* This revisited version uses a function with an integer parameter
*/
int ledPin = 13; // on-board LED connected to digital pin 13
int redLedPin = 12; // RED LED connected to digital pin 12
int grnLedPin = 11; // GREEN LED connected to digital pin 11
int bluLedPin = 10; // BLUE LED connected to digital pin 10
// the variable delay is accomplished by..
int iMult; // ..the multiplier variable multiplied by..
int dLay = 10; // ..the delay variable, which is 10 milliseconds
// The setup() method runs once, when the sketch starts
void setup()
{
// initialize the 3 digital pins as outputs:
pinMode(ledPin, OUTPUT);
pinMode(redLedPin, OUTPUT); // Red LED output
pinMode(grnLedPin, OUTPUT); // Green LED output
pinMode(bluLedPin, OUTPUT); // Blue LED output
// turn on the on-board LED & wait 0.5 seconds
digitalWrite(ledPin, HIGH); // turn on-board LED on
delay(dLay*50); // delay 0.5 seconds (50 * 10ms)
}//end setup()
/* Loop first turns all 3 LEDs on succesively, then turns them off again.
Delay between each phase is about 100 milliseconds
Loop now turns RED, GREEN & BLUE LEDs on & off cyclically
with varying delay each time loop runs, &..
..repeats endlessly while Arduino has power
At the half-way point the order of the LED-cycle is reversed;
Viewers should try to spot this point! What indicates this?
*/
void loop()
{
digitalWrite(redLedPin, HIGH); // turn red LED on
delay(50*dLay); // delay 500 milliseconds
digitalWrite(grnLedPin, HIGH); // turn green LED on
delay(50*dLay); // delay 500 milliseconds
digitalWrite(bluLedPin, HIGH); // turn blue LED on
delay(100*dLay); // delay 1 second
// Now turn them all off..
digitalWrite(redLedPin, LOW);
digitalWrite(grnLedPin, LOW);
digitalWrite(bluLedPin, LOW);
delay(50*dLay); // ..& wait 0.5 seconds
// Ramps delays UP - Red, Green & Blue LEDs each turn on, then off,
// with the delay time increasing with each for-loop cycle
// from 10 - 200 milliseconds
for (iMult=1; iMult<=20; iMult++ ) // now start 'increasing delay' for-loop
{
flash(redLedPin); // flash red LED..
flash(grnLedPin); // flash green LED...
flash(bluLedPin); // flash blue LED...
}//end for(iMult++)
// Ramps delays DOWN again - Blue, Green & Red LEDs each turn on, then off,
// with the delay time decreasing with each for-loop cycle
// Note REVERSED ORDER now Blue-Green-Red
for (iMult=20; iMult>=1; iMult-- ) // now start 'increasing delay' for-loop
{
flash(bluLedPin); // flash blue LED..
flash(grnLedPin); // flash green LED...
flash(redLedPin); // flash red LED...
}////end for(iMult--)
//..& repeat
}//end loop()
/* Now we define a function to take care of the more repetitious bits of code,
in this case, turning variously-coloured LEDs on & off
*/
void flash(int lPin) // lPin is the parameter for, e.g. bluLedPin,
i.e. the BLUE LED
{
digitalWrite(lPin, HIGH); // ..turn LED on..
delay(iMult*dLay); // ..delay 10millisec * multiplier..
digitalWrite(lPin, LOW); // ..turn LED off..
delay(iMult*dLay); // ..delay 10millisec * multiplier..
}//end flash()
This code consumes 1402 bytes, as against 1726 for the 'old' version.
That's a saving of ~19%! Important in such a small device as Arduino..
WLED to xLights exploration
19 minutes ago
This is rather shorter than the original software.
ReplyDeleteI've tested it & it works (why shouldn't it?!)
I could've put the line
ReplyDeletedelay(iMult*dLay);
// ..delay 10millisec * multiplier
into the function & made things shorter still..
Well i did! And it did..
ReplyDeleteNearly 20% shorter is not to be sniffed at!