technogeekca:
I'm not quite at the point where I understand what you mean.
Both timers are running all the time as far as I know because the LED's are always Blinking.
Do however explain further for future reference.
Edit!!!!!!, maybe I do get what your saying. Below is the Sketch with the "currentTimeB = millis();" moved down just before the if statement for the second loop.
Thanks again.
const int k_numLEDs = 8; //Define the number of LED's
const int kPinLeds[k_numLEDs] = {22, 23, 24, 25, 26, 27, 28, 29}; //Define MEGA 2560 Digital Pins
void setup()
{
for (int i = 0; i < k_numLEDs; i++) //Set all Eight Pins to OUTPUT's
{
pinMode(kPinLeds[i], OUTPUT);
digitalWrite(kPinLeds[i], LOW); //Set all Eight Pins LOW
}
delay(100);
}
// the current time
unsigned long currentTimeA; //Set the currentTime variable for Timer A
unsigned long currentTimeB; //Set the currentTime variable for Timer B
// last time that a LED changed state
unsigned long lastActionTimeA = 0; //Set the last time an LED chnged State A
unsigned long lastActionTimeB = 0; //Set the last time an LED chnged State B
void loop()
{
// LED counter; I use a char instead of a byte so we can use negative values
static char counter = 0;
// direction; up = 1, down = -1
static char direction = 1;
// get the current time
currentTimeA = millis(); //set currentTimeA to current milliseconds
// if it's time
if (currentTimeA - lastActionTimeA >= 300) //Flash Pin 29 LED
{
{byte leadStateA = digitalRead(29);
leadStateA = !leadStateA;
digitalWrite(29, leadStateA);}
lastActionTimeA = currentTimeA;
}
else
{
// nothing to do; we have to wait till the current led is switched off
}
currentTimeB = millis(); //set currentTimeB to current milliseconds
if (currentTimeB - lastActionTimeB >= 1000)
{
// read the status of the current led
byte ledStateB = digitalRead(kPinLeds[counter]);
// toggle the status before we write it to the pin
ledStateB = !ledStateB;
// set current pin
digitalWrite(kPinLeds[counter], ledStateB);
// remember the last time that we did an action
lastActionTimeB = currentTimeB;
// if led state equals LOW, we have done the cycle (on/off) for one LED
if (ledStateB == LOW)
{
// next time, do next led
counter += direction;
// if all leds done for forward sequence or for reverse sequence
if (counter == k_numLEDs -1 || counter == -1) //-1 Exclude Pin 29 LED
{
// change direction
direction *= -1;
// update counter; multiply by 2 so we don't do the last pin again
counter += direction * 2;
}
}
else
{
// nothing to do; we have to wait till the current led is switched off
}
}
}
OK, lets see if this works. From code in #9
change:
{
// read the status of the current led
byte ledStateB = digitalRead(kPinLeds[counter]);
// toggle the status before we write it to the pin
ledStateB = !ledStateB;
// set current pin
digitalWrite(kPinLeds[counter], ledStateB);
// remember the last time that we did an action
lastActionTimeB = currentTimeB;
// if led state equals LOW, we have done the cycle (on/off) for one LED
if (ledStateB == LOW)
{
// next time, do next led
counter += direction;
// if all leds done for forward sequence or for reverse sequence
if (counter == k_numLEDs -1 || counter == -1) //-1 Exclude Pin 29 LED
{
// change direction
direction *= -1;
// update counter; multiply by 2 so we don't do the last pin again
counter += direction * 2;
}
}
else
{
// nothing to do; we have to wait till the current led is switched off
}
}
to:
{
if ( ledStateB == LOW ) // the next state
{
counter += direction; // next light
}
if ( counter == -1 || counter == k_numLEDs ) // illegal values for counter so turn around
{
direction *= -1; // change direction
counter += direction; // do the last LED again
lesStateB == HIGH; // keep it back on one more time count
}
writeDigital( kPinLeds[counter],ledStateB);
ledStateB = !ledStateB ; // flip state
}
I should note that you should move both lastActionTimeA and lastActionTimeB
before setup and set them to millis() in setup();
It may not be needed but then you don't assume that millis() always
starts at 0, even though I expect it does.
Check for matching {}
Dwight