Created
February 17, 2014 09:33
-
-
Save WORMSS/9047543 to your computer and use it in GitHub Desktop.
Blink for Multiple LEDs with durations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Author: WORMSS | |
* | |
* Blink with no Delay | |
* - This allows for multiple LEDs to be used. | |
* - This allows for delayed start of sequence. | |
* - This allows for different on and off delays. | |
* | |
* Instructions: | |
* - Change the LENGTH variable below with the amount of LEDs you wish to use. | |
* - Within the setup() function play around with the mLights[] settings to change what you wish. | |
* - mLights[0] refers to the first light. mLights[1] refers to the second light. | |
* - | |
* - mLights[0].pin refers to the pin of the first LED. | |
* - mLights[1].onInterval refers to the duration the second LED should stay on for. | |
* - mLights[2].offInterval refers to the duration the third LED should stay off for. | |
*/ | |
// Amount of Lights to change | |
const unsigned char LENGTH = 3; | |
/* | |
* This is the setup of the LightAction structure. | |
* - This will be used to hold all the Lights individual settings | |
*/ | |
typedef struct LightAction { | |
// The number of the Pin, max value 255 | |
unsigned char pin; | |
// The state of the Light, either HIGH (on) or LOW (off) | |
unsigned char state; | |
// The time in milliseconds the next time the light needs to change state | |
unsigned long nextTime; | |
// The interval in milliseconds the light should stay off for. | |
unsigned long offInterval; | |
// The interval in milliseconds the light should stay on for. | |
unsigned long onInterval; | |
} LightAction; | |
/* | |
* This is the array that holds all the of the lights in a second variable | |
* - If you are unfamiliar with arrays, I recommend a quick google search for "Arrays in C" | |
*/ | |
LightAction mLights[LENGTH]; | |
/* | |
* This will hold the current time for the device | |
* - We set the time in this variable to save from calling millis() multiple times. | |
*/ | |
unsigned long mCurrentTime; | |
void setup() { | |
/* | |
* This section is setting up each Light | |
* - The number within the brackets increases, This is the number of the light. | |
* - Note: we start with 0 | |
* - This section you can add, remove, change these settings for your individual needs. | |
* - use nextTime to delay its start time. | |
*/ | |
mLights[0].pin = 12; | |
mLights[0].offInterval = 6000; | |
mLights[0].onInterval = 10000; | |
mLights[0].nextTime = 5000; | |
mLights[1].pin = 7; | |
mLights[1].offInterval = 3500; | |
mLights[1].onInterval = 1000; | |
mLights[2].pin = 2; | |
mLights[2].offInterval = 12000; | |
mLights[2].onInterval = 10000; | |
/* | |
* | |
* No need to change anything from this point on | |
* | |
*/ | |
// Loops through the lights and sets the pins to OUTPUT and turn on if state is HIGH | |
for (unsigned int i = 0; i < LENGTH; i++) { | |
pinMode(mLights[i].pin, OUTPUT); | |
if ( mLights[i].state ) { | |
digitalWrite(mLights[i].pin, HIGH); | |
} | |
} | |
} | |
// This function tests if the light needs to be changed yet | |
void processLightAction(LightAction *action) { | |
if (mCurrentTime > action->nextTime) { | |
/* | |
* Flip the state from HIGH to LOW, or LOW to HIGH. | |
* This increments the number by 1, but if the number turns to 2 returns it to 0. | |
* The reason this works is because LOW is 0, and HIGH is 1. | |
*/ | |
++action->state %= 2; | |
/* | |
* Set the time of when the next action needs to be triggered. | |
* If the state is HIGH increment the time by onInterval, | |
* otherwise increment the time by offInterval. | |
*/ | |
action->nextTime += action->state ? action->onInterval : action->offInterval; | |
// Set the pin to the state | |
digitalWrite(action->pin, action->state); | |
} | |
} | |
void loop() { | |
/* | |
* Set mCurrentTime to the amount of milliseconds the device has been turned on. | |
* mCurrentTime will be used within processLightAction function. | |
*/ | |
mCurrentTime = millis(); | |
// Loop through each Light and pass to the processLightAction function | |
for (unsigned int i = 0; i < LENGTH; i++) { | |
processLightAction(&mLights[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment