I have a small led strip program using the delay function where leds chase in one direction in red and then change to green in the other direction, in order to get two leds of, diferent colours, running in different directions at the same time I think I'd need to use the miilis() function and not delay().
I was hoping someone could help me change the delay() function in the code below to millis() and then I'll spend some time trying to work out the rest, I've been reading a few webpages on this but cant quite get my head around the syntax.
To use mllis() for timing instead of delay() you need to restructure your program to keep loop() running freely
An example
unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;
byte count = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
}
void loop()
{
currentTime = millis();
if (currentTime - startTime >= period)
{
Serial.println(count);
startTime = currentTime;
count++;
if (count >= 10)
{
count = 0;
}
}
}
Of course, instead of printing you could do almost anything. If you want more than one pattern then when you have finished the current one change to another one by having the action controlled by a variable
which just illuminates two LEDs each time, shows them, and extinguishes them. A (the) trick you'll see if you look closely is the index used to refer to the second color
NUM_LEDS - dot - 1
which will just run through the LEDs in reverse from dot as dot goes from 0 to (NUM_PIXELS - 1).
Plug in a few values and see. Or run this loop in your code and see.
But do get back to millis() and, perhaps, state machines one day. This would be a good candidate.
You have to turn everything inside-out. You use static (or global) variables so they retain their value across calls to loop(). You reduce the loops to separate increment and test operations so only one step of the loop happens each time loop is called. Then you use millis() to schedule the steps of the loop. Both loops were roughly the same so a state variable is used to keep track of which of the two loops is the current loop.
void loop()
{
unsigned long currentMillis = millis();
static unsigned long previousMillis = 0;
static boolean RedMode = true;
static int dot = 0;
if (currentMillis - previousMillis >= delayPeriod)
{
previousMillis = currentMillis;
// Time to do one step of the loop
if (RedMode)
{
// chase forward red
leds[dot] = CRGB::Red;
FastLED.show();
leds[dot] = CRGB::Black;
}
else // GreenMode
{
// chase backward green
leds[(NUM_LEDS - 1) - dot] = CRGB::Green;
FastLED.show();
leds[(NUM_LEDS - 1) - dot] = CRGB::Black;
}
// End of the looped stuff, increment the loop
dot++;
// Check to see if the loop is over
if (dot >= NUM_LEDS)
{
dot = 0; // Re-start the loop
RedMode = !RedMode; // Togle the mode
}
}
}
alto777:
Nice code as usual @johnwasser, but it does not “get two leds of, diferent colours, running in different directions at the same time”, the OP’s stated objective.
Sorry, I missed that. To do that they don't need millis():
void loop()
{
for (int dot = 0; dot < NUM_LEDS; dot++)
{
// chase forward red
leds[dot] = CRGB::Red;
// chase backward green
leds[(NUM_LEDS - 1) - dot] = CRGB::Green;
FastLED.show();
// Turn both of those off the next time .show() is called
leds[dot] = CRGB::Black;
leds[(NUM_LEDS - 1) - dot] = CRGB::Black;
delay(delayPeriod);
}
}