Hi all,
I'm new to the Arduino and I'm attempting to get a variable number of leds to flash at a fixed on/off rate. The following code almost works, but not quite - What am missing?
/*
*
* Blink Multiple LEDs without Delay on/off times individually assigned
*
*/
const int LEDcount = 2; // Number of LEDS used (start @ 2 - KISS)
byte pin[LEDcount] = {9,10}; //Pins assigned to those LEDS
unsigned int oninterval[LEDcount] = {100, 2000}; // On intervial time for leds (max 65535)
unsigned int offinterval[LEDcount] = {2900,2000}; // Off intervial time for leds (max 65535)
unsigned long timeoff[LEDcount] = {0,0}; // What time was the LED last tumned OFF
unsigned long timeon[LEDcount] = {0,0}; // What time was the LED last tuned ON
void setup()
{
for (int i=0; i<LEDcount; ++i)
pinMode(pin[i], OUTPUT); //initilize all used pins are outputs
}
void loop()
{
unsigned long CurrentTime = millis(); // assign current elapsed time to variable "CurrentTime"
for (int i=0; i<LEDcount; ++i) // loop through all LEDS
{
if (CurrentTime - timeoff[i] > offinterval[i])
{
digitalWrite(pin[i], HIGH); //Turn LED on
timeoff[i] = CurrentTime;// Set on timeon = current time (relative 0)
}
if (CurrentTime - timeon[i] > oninterval[i])
{
digitalWrite(pin[i], LOW); //Turn LED off
timeon[i] = CurrentTime; // Set on timeon = current time (relative 0)
}
}
}