0

How to create delay while using millis
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to create delay while using millis

by dark_matter on Wed Sep 11, 2019 1:48 am

Hello Forum,

Hope you can help with this issue. Seems like it should be simple but it has me stumped. I've shamelessly borrowed several code examples for debouncing a switch and multi-tasking along with the neopixels light shows.

https://blog.adafruit.com/2009/10/20/ex ... ebouncing/
https://learn.adafruit.com/multi-taskin ... ino-part-3

I've hacked them together and everything is working except for one light effect that I have created. The effect simulated a police light bar. Alternating banks of lights at different rates that change every few seconds. The effects works fine with delay() to control the timings but of course that blocks the reading of the button to step through the different light effects.

If I try to use millis() to control the timing of the pulsed lights then I can't break out of the method. Seems like the button is not being detected. The button is detected for all other light effect methods, but not for the method when I use millis() to control timing.

I'd be grateful if someone can point me in the right direction. The troublesome code is in PoliceUpdate method.

Code file attached, compiled against Flora with IDE 1.8.9 and NeoPixel Digital RGB LED (20)

Thanks!

longboard_v4.ino
(15.09 KiB) Downloaded 6 times

dark_matter
 
Posts: 4
Joined: Sun Aug 06, 2017 11:31 pm

Re: How to create delay while using millis

by dastels on Wed Sep 11, 2019 11:51 am

For debouncing I usually use https://playground.arduino.cc/Code/Bounce/.

The general idea to this sort of thing is to separate your sequences into single steps. The update functions should advance the light effect one step... they shouldn't have loops. Your use of milis() seems reasonable: you use it to decide whether the effect should be stepped. The update functions should not contain loops apart from iterating over the pixels to set their colours. And the update functions shouldn't have delays() in them. You want them to do as little as possible so control can get back to the main loop asap.

Dave

dastels
 
Posts: 230
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to create delay while using millis

by dark_matter on Fri Sep 13, 2019 1:04 pm

Hi Dave,

Thanks for the advise. I used the Bounce2 library to clean things up. Much nicer! I understand your explanation of the loops and millis but still trying to wrap my brain around how to rewrite my method to pulse the lights with delays between the pulses. I'll post my solution when I get there. Could be a while. I appreciate your response.

Marty

dark_matter
 
Posts: 4
Joined: Sun Aug 06, 2017 11:31 pm

Re: How to create delay while using millis

by dastels on Fri Sep 13, 2019 2:51 pm

A state machine approach might be applicable. Maybe https://learn.adafruit.com/circuitpython-101-state-machines/code will give you some ideas. It's written for CircuitPython, but the concepts are language independent.

dastels
 
Posts: 230
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.