Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



LED Effects, using ...
 
Share:
Notifications
Clear all

[Solved] LED Effects, using millis() instead of delay()?

3 Posts
1 Users
0 Reactions
2,409 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
Topic starter  

Find below the full post of EssentialLED ...

Do you know how i could use millis instead of delay in the script below. I want to be able to add more effects and use a button but the delay takes out the button. so i have started again with a basic script but i have no idea how to have this run with millis, and can't get my head around the millis documentation 

#include <FastLED.h>
#define DATA_PIN 3
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 64
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
void setup() {
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
}
void loop()

fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
leds[2] = CRGB::Blue;
leds[3] = CRGB::Blue;
leds[4] = CRGB::Blue;
leds[5] = CRGB::Blue;
leds[6] = CRGB::Blue;
leds[7] = CRGB::Blue;
leds[8] = CRGB::Blue;
leds[23] = CRGB::Blue;
leds[24] = CRGB::Blue;
leds[39] = CRGB::Blue;
leds[40] = CRGB::Blue;
leds[55] = CRGB::Blue;
leds[56] = CRGB::Blue;
leds[57] = CRGB::Blue;
leds[58] = CRGB::Blue;
leds[59] = CRGB::Blue;
leds[60] = CRGB::Blue;
leds[61] = CRGB::Blue;
leds[62] = CRGB::Blue;
leds[63] = CRGB::Blue;
leds[48] = CRGB::Blue;
leds[47] = CRGB::Blue;
leds[32] = CRGB::Blue;
leds[31] = CRGB::Blue;
leds[16] = CRGB::Blue;
leds[15] = CRGB::Blue;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
leds[4] = CRGB::Red;
leds[5] = CRGB::Red;
leds[6] = CRGB::Red;
leds[7] = CRGB::Red;
leds[8] = CRGB::Red;
leds[23] = CRGB::Red;
leds[24] = CRGB::Red;
leds[39] = CRGB::Red;
leds[40] = CRGB::Red;
leds[55] = CRGB::Red;
leds[56] = CRGB::Red;
leds[57] = CRGB::Red;
leds[58] = CRGB::Red;
leds[59] = CRGB::Red;
leds[60] = CRGB::Red;
leds[61] = CRGB::Red;
leds[62] = CRGB::Red;
leds[63] = CRGB::Red;
leds[48] = CRGB::Red;
leds[47] = CRGB::Red;
leds[32] = CRGB::Red;
leds[31] = CRGB::Red;
leds[16] = CRGB::Red;
leds[15] = CRGB::Red;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
leds[2] = CRGB::Green;
leds[3] = CRGB::Green;
leds[4] = CRGB::Green;
leds[5] = CRGB::Green;
leds[6] = CRGB::Green;
leds[7] = CRGB::Green;
leds[8] = CRGB::Green;
leds[23] = CRGB::Green;
leds[24] = CRGB::Green;
leds[39] = CRGB::Green;
leds[40] = CRGB::Green;
leds[55] = CRGB::Green;
leds[56] = CRGB::Green;
leds[57] = CRGB::Green;
leds[58] = CRGB::Green;
leds[59] = CRGB::Green;
leds[60] = CRGB::Green;
leds[61] = CRGB::Green;
leds[62] = CRGB::Green;
leds[63] = CRGB::Green;
leds[48] = CRGB::Green;
leds[47] = CRGB::Green;
leds[32] = CRGB::Green;
leds[31] = CRGB::Green;
leds[16] = CRGB::Green;
leds[15] = CRGB::Green;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[0] = CRGB::Yellow;
leds[1] = CRGB::Yellow;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Yellow;
leds[4] = CRGB::Yellow;
leds[5] = CRGB::Yellow;
leds[6] = CRGB::Yellow;
leds[7] = CRGB::Yellow;
leds[8] = CRGB::Yellow;
leds[23] = CRGB::Yellow;
leds[24] = CRGB::Yellow;
leds[39] = CRGB::Yellow;
leds[40] = CRGB::Yellow;
leds[55] = CRGB::Yellow;
leds[56] = CRGB::Yellow;
leds[57] = CRGB::Yellow;
leds[58] = CRGB::Yellow;
leds[59] = CRGB::Yellow;
leds[60] = CRGB::Yellow;
leds[61] = CRGB::Yellow;
leds[62] = CRGB::Yellow;
leds[63] = CRGB::Yellow;
leds[48] = CRGB::Yellow;
leds[47] = CRGB::Yellow;
leds[32] = CRGB::Yellow;
leds[31] = CRGB::Yellow;
leds[16] = CRGB::Yellow;
leds[15] = CRGB::Yellow;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[28] = CRGB::Yellow;
leds[27] = CRGB::Yellow;
leds[36] = CRGB::Yellow;
leds[35] = CRGB::Yellow;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[18] = CRGB::Green;
leds[19] = CRGB::Green;
leds[20] = CRGB::Green;
leds[21] = CRGB::Green;
leds[26] = CRGB::Green;
leds[37] = CRGB::Green;
leds[42] = CRGB::Green;
leds[43] = CRGB::Green;
leds[44] = CRGB::Green;
leds[45] = CRGB::Green;
leds[34] = CRGB::Green;
leds[29] = CRGB::Green;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[14] = CRGB::Red;
leds[13] = CRGB::Red;
leds[12] = CRGB::Red;
leds[11] = CRGB::Red;
leds[10] = CRGB::Red;
leds[9] = CRGB::Red;
leds[22] = CRGB::Red;
leds[25] = CRGB::Red;
leds[38] = CRGB::Red;
leds[41] = CRGB::Red;
leds[54] = CRGB::Red;
leds[53] = CRGB::Red;
leds[52] = CRGB::Red;
leds[51] = CRGB::Red;
leds[50] = CRGB::Red;
leds[49] = CRGB::Red;
leds[46] = CRGB::Red;
leds[33] = CRGB::Red;
leds[30] = CRGB::Red;
leds[17] = CRGB::Red;
FastLED.show();
delay(200);
fill_solid( leds, NUM_LEDS, CRGB::Black);
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
leds[2] = CRGB::Blue;
leds[3] = CRGB::Blue;
leds[4] = CRGB::Blue;
leds[5] = CRGB::Blue;
leds[6] = CRGB::Blue;
leds[7] = CRGB::Blue;
leds[8] = CRGB::Blue;
leds[23] = CRGB::Blue;
leds[24] = CRGB::Blue;
leds[39] = CRGB::Blue;
leds[40] = CRGB::Blue;
leds[55] = CRGB::Blue;
leds[56] = CRGB::Blue;
leds[57] = CRGB::Blue;
leds[58] = CRGB::Blue;
leds[59] = CRGB::Blue;
leds[60] = CRGB::Blue;
leds[61] = CRGB::Blue;
leds[62] = CRGB::Blue;
leds[63] = CRGB::Blue;
leds[48] = CRGB::Blue;
leds[47] = CRGB::Blue;
leds[32] = CRGB::Blue;
leds[31] = CRGB::Blue;
leds[16] = CRGB::Blue;
leds[15] = CRGB::Blue;
FastLED.show();
delay(200);
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
Topic starter  

Hi EssentialLED,

Well, you can use millis() here, but I'm not sure it will do what you expect it to do.
millis() in essence return the time, since the Arduino got powered up, in milliseconds (1000 milliseconds = 1 second).

So let's say we want to implement delay(200) by using millis(), you'd basically have to store the start time, and wait time to reach startTime + 200, before proceeding. Something like this:

unsigned long startTime = 0;

// ...

startTime = millis();
while (millis() < startTime+200) {
  // do something here while we are waiting
}

 

In plain English:

1. Store the current time in startTime
2. Keeping doing (while) something as long as the current time is less that the startTime+200.

In that loop you could read the button for example, or print something to the serial monitor, or do simply nothing.
I hope that makes sense.

However, now comes the problem ...
Say in that time frame the button gets pressed, you'd probably want to call some sort of function to change the effect.
This will work, however, when that new function is done, it will return to this loop and continue where it left off.
When the called function has something similar (using millis() waiting for the button yet again), and this function will call yet another effect function, then your Arduino will keep accumulation positions where it should return to, which in the end (potentially) will chew up all your memory.

This is why I use the Interrupt and Reset function (here). What I do there is save the current effect in EEPROM (survives a reset), and reset the Arduino, and once the Arduino starts it will look for the value in the EEPROM so it knows what effect to start with.

I hope this helps, but I realize this may make it even more confusing 🤣 

Note: A reset may not be needed, if you design your effect functions in such a way that it can return to the main loop when it sees that you pressed the button (and changed a global variable to store what the effect should be). The advantage of the reset is that it can brutally stop an effect.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
Topic starter  

p.s. forgot to mention:

When filling a range with a color, you can also do this:

fill_solid(start_led, number_of_leds, color);

 

So you could rewrite

leds[0] = CRGB::Yellow;
leds[1] = CRGB::Yellow;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Yellow;
leds[4] = CRGB::Yellow;
leds[5] = CRGB::Yellow;
leds[6] = CRGB::Yellow;
leds[7] = CRGB::Yellow;
leds[8] = CRGB::Yellow;

 

as (start at leds[0], fill 9 leds with yellow)

fill_solid(leds[0], 9, CRGB::Yellow);

 

This may save some space in your code.


   
ReplyQuote
Share: