Hi Guys,
New here and trying to learn some coding and some led stuff. I’m a little stuck with trying to make a single button press last for a desired amount of time so here’s my project so far!
I’ve been having a go with millis() but it’s probably a good idea to show you how far I got before trying to use this function since the coding language is a little beyond me at the moment hence posting here!
Here’s my code without the Millis. It simply has 2 buttons which control one strip. One button acts as a turn signal ‘sweep’ the other acts as a brake light which I’m trying ‘mix’ with the turn signal rather than having separate strips that perform the two tasks:
#include "FastLED.h"
#define NUM_LEDS 16
CRGB leds[NUM_LEDS];
const int indicatorPin = 2;
int indicatorState = 0;
const int brakePin = 1;
int brakeState = 0;
int currentIndicatorLED = 0;
void setup() {
pinMode(indicatorPin, INPUT);
pinMode(brakePin, INPUT);
FastLED.addLeds<WS2812B, 6, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812B, 5, RGB>(leds, NUM_LEDS);
LEDS.setBrightness(50);
}
void indicatorSweep()
{
if (currentIndicatorLED < NUM_LEDS)
{
leds[currentIndicatorLED] = CRGB(255, 0, 0);
currentIndicatorLED++;
// Deal with the other leds after the current lit indicator led
for (int led = currentIndicatorLED; led < NUM_LEDS; led++)
{
// Are the brakes on ?
if (brakeState == HIGH)
{
// Light the led as a brake
leds[led] = CRGB(0, 255, 0);
}
else
{
// Blank the led as there is no brake
leds[led] = CRGB(0, 0, 0);
}
FastLED.show();
}
}
else
{
currentIndicatorLED = 0;
}
delay(40);
}
void loop() {
indicatorState = digitalRead(indicatorPin);
brakeState = digitalRead(brakePin);
if (indicatorState == HIGH) {
indicatorSweep();
} else {
FastLED.clear();
FastLED.show();
currentIndicatorLED = 0;
}
}
Here’s a video of this in action, sorry for the audio:
[https://www.youtube.com/watch?v=jvzaoZ6Mv_o
I made the turn signal green to see it clearly but the IndicatorSweep() function is a loop that deals with 1 led per loop and then moves onto the next one. This allows the button state to be read at each led in turn which allows the brake light to jump in at any time. The only drawback (and also the last hurdle for my project) is that when you let go of the button, the sweep ceases. I’d like the sweep to go all the way to the end with 1 button press. I’ve tried while loops, interrupts, switch/case statements and then realised all I need to do is emulate holding the button down for 600ms (15 leds x 40ms between leds).
Now, here’s my attempt with millis(). At the moment, I’m only testing the green sweep part:
#include "FastLED.h"
#define NUM_LEDS 15
CRGB leds[NUM_LEDS];
const int indicatorPin = 2;
unsigned long currentMillis;
byte indicatorPintimerRunning = 0;
unsigned long indicatorPinstartTime;
unsigned long indicatorPinelapsedTime;
unsigned long indicatorPinonDuration = 1000;
int currentIndicatorLED = 0;
void setup() {
FastLED.addLeds<WS2812B, 6, RGB>(leds, NUM_LEDS);
pinMode (indicatorPin, INPUT_PULLUP); // wire pinX to connect to Gnd when button is pressed
}
void indicatorSweep()
{
if (currentIndicatorLED < NUM_LEDS)
{
leds[currentIndicatorLED] = CRGB(255, 0, 0);
FastLED.show();
currentIndicatorLED++;
}
else
{
FastLED.clear();
FastLED.show();
delay(100);
currentIndicatorLED = 0;
}
delay(35);
}
void loop(){
currentMillis = millis();
// check if button is pressed to start an action
if (digitalRead(indicatorPin) == LOW && indicatorPintimerRunning == 0){
indicatorPintimerRunning = 1;
indicatorPinstartTime = currentMillis;
indicatorSweep();
// start whatever action is to occur
}
// check if time to stop
if (indicatorPintimerRunning == 1){
indicatorPinelapsedTime = currentMillis - indicatorPinstartTime;
if (indicatorPinelapsedTime >= indicatorPinonDuration){
// stop whatever action is to occur
FastLED.showColor(CRGB::Black);
indicatorPintimerRunning = 0;
}
}
}
Here’s a vid of this in action:
[https://www.youtube.com/watch?v=wX7CJS1p1Hk
You can see that at the moment, it’s performing the loop for 1 second (activate first led, wait 1 second and activate the next led) and isn’t actually emulating holding the button for 1 second.
Like I said, I’m struggling with the terminolgy of this code to understand how to modify it so I’m a little stuck.
Do you guys think it’s possible to use a variant of the millis() setup to achieve what I’m after? If so, how? If not, can ayone think of anything else I could do to achieve this?
Thanks in advance guys, I’m very excited about finishing this one!
Kyle](https://www.youtube.com/watch?v=wX7CJS1p1Hk)](https://www.youtube.com/watch?v=jvzaoZ6Mv_o)