Hi Sid,
welcome to the forum! 😊Ā
I also notice that you lock "if(Stop==LOW)" inside the IF-loop where you test for Stop==HIGH - so that condition will never be reached (if I'm reading this right - note: always a good idea to attach the INO file to your post in the future, but as a new user you will have to post a few legit posts first before you can do that).
So example this part of the code: the bold red code will never be executed, since in this section Stop==HIGH.
if (Stop == HIGH) {
for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i) {
leds[i] = CRGB(red, green, blue);
leds[NUM_LEDS - i - 1] = CRGB(red, green, blue);
delay(SpeedDelay);
FastLED.show();
}
if (Stop == LOW) {
FastLED.clear();
}
}
Now you could move that IF outside of that initial IF-loop, but then this would set the LEDs to black each time - not what you had in mind.
So to avoid all that, I'd probably modify the colorSwpie function a little bit, since the leds are all turned off when all 3 (left, right, stop) are not HIGH.
Ā
Tip: You can use shorter colors notations, see the FastLED color reference list if you'd like to use those (not required of course, but it does make the code more readable if you use just a few basic colors). For exampleĀ CRGB(0x00,0x00,0x00) = CRGB::BlackĀ Ā andĀ Ā CRGB(0xFF,0xFF,0x00) = CRGB::Yellow. 😊Ā
Also not sure if required, but I think you have to do a FastLED.show() after a FastLED.clear(). I could be wrong though.
Ā
So you could do something like this (I removed the speedDelay since it didn't have much of a purpose there):
!! Don't forget:
You will have to add the beginning of your code (#defines, #include, int's and void setup()) - since this code was a little incomplete, probably because I tried putting it in code format in your post.
The bold parts of the code are my changes.
...
#define BrakeDelay 150
#define BlinkerDelay 60
...
void loop() {
Stop = digitalRead(Pin1);
Left = digitalRead(Pin2);
Right = digitalRead(Pin3);
colorWipe(CRGB::Yellow);
colorWipe(CRGB::Black);
}
void colorWipe(CRGB WipeColor) {
if (Stop == HIGH) // brake - outsides to center
{
for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i)
{
leds[i] = WipeColor;
leds[NUM_LEDS - i - 1] = WipeColor;
delay(BrakeDelay);
FastLED.show();
}
}
else if (Right == HIGH) // right turn - right to center
{
for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++)
{
leds[i] = WipeColor; // removed the "mirror" leds
FastLED.show();
delay(BlinkerDelay); // change the number here to change the speed of the LEDs
}
}
else if (Left == HIGH) //left turn - left to center
{
for (int i = NUM_LEDS / 2 - 1; i >= 0; i--)
{
leds[i] = WipeColor; // removed the "mirror" leds
FastLED.show();
delay(BlinkerDelay); // change the number here to change the speed of the LEDs
}
}
else
{
FastLED.clear();
FastLED.show();
}
}
Ā
I couldn't resist so I made another modification, so Stop becomes red and Left/Right remains yellow (note: I have not tested this with switches).
...
#define BrakeDelay 150
#define BlinkerDelay 60
...
void loop() {
Stop = digitalRead(Pin1);
Left = digitalRead(Pin2);
Right = digitalRead(Pin3);
if( (Stop==HIGH) || (Left==HIGH) || (Right==HIGH))
{
if(Stop==HIGH)
{
colorWipe(CRGB::Red);
}
else
{
colorWipe(CRGB::Yellow);
}
colorWipe(CRGB::Black);
}
else
{
FastLED.clear();
FastLED.show();
}
}
void colorWipe(CRGB WipeColor) {
if (Stop == HIGH) // brake - outsides to center
{
for (int i = 0; i <= (NUM_LEDS / 2 - 1); ++i)
{
leds[i] = WipeColor;
leds[NUM_LEDS - i - 1] = WipeColor;
delay(BrakeDelay);
FastLED.show();
}
}
else if (Right == HIGH) // right turn - right to center
{
for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++)
{
leds[i] = WipeColor;
FastLED.show();
delay(BlinkerDelay);
}
}
else if (Left == HIGH) //left turn - left to center
{
for (int i = NUM_LEDS / 2 - 1; i >= 0; i--)
{
leds[i] = WipeColor;
FastLED.show();
delay(BlinkerDelay);
}
}
}
Ā
And another note: this all can be done more efficient, but I think this should do what you want it to do, 😊Ā