Dynamic turn signals stop and parking lights - WS2812b

lesept:
OK, did you try the version of my answer #37?

If you want to get back to before, uncomment lines 44 and 58.

yes, I checked the version from # 37. Correct only the mass control for pin 4. The ground control for pin 4 must be the same as the pin 6 control. The rest works well only you need to improve this mass control for pin 4.

I'll check that this evening. I don't understand why there is a difference between left and right, as the codes are symmetric. I'll try it with an Arduino back home and then I'll have more insight of how it's working inside.

lesept:
I'll check that this evening. I don't understand why there is a difference between left and right, as the codes are symmetric. I'll try it with an Arduino back home and then I'll have more insight of how it's working inside.

OK I understand

Got it! I always make that mistake, with byte going down below 0 but instead of -1 they come back to 255!
So here is a version which should work:

#include <FastLED.h>
#define LED_PIN1     2
#define NUM_LEDS1   16

// buttons / switches for commands
#define StopButton  3
#define LeftSignal  4
#define PosSwitch   5
#define RightSignal 6

CRGB leds[NUM_LEDS1]; // 0-7 left, 8-15 right

byte NTurnLeft  = NUM_LEDS1 / 2 - 1;
byte NTurnRight = NUM_LEDS1 / 2 - 1;
bool TurnLeft   = false;  //
bool TurnRight  = false;  //
bool Stop       = false;  // stop light on or off ?
bool Position   = true;   // position light on or off ?

const byte BrightnessPos  =  40; // percentage
const byte BrightnessTurn = 100;
const byte BrightnessStop = 100;

unsigned long TurnDur     = 30ul;
unsigned long Chrono      = 0;

void ReadButtons () {
  TurnLeft  = digitalRead(LeftSignal);
  TurnRight = digitalRead(RightSignal);
  Stop      = !digitalRead(StopButton);
  Position  = !digitalRead(PosSwitch);
  delay(20); // for debouncing
}

void TurnSignalRightUp () {
  if (millis() - Chrono >= TurnDur) {
    Chrono = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - Chrono >= TurnDur) {
    Chrono = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnLeft --;
    if (NTurnLeft > 254) {
      NTurnLeft = 0;
    }
  }
}

void LightAll (byte Bright) {
  int red = 255 * Bright / 100;
  fill_solid(leds, NUM_LEDS1, CRGB(red, 0, 0));
}

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
  pinMode (StopButton,  INPUT_PULLUP);
  pinMode (LeftSignal,  INPUT_PULLUP);
  pinMode (PosSwitch,   INPUT_PULLUP);
  pinMode (RightSignal, INPUT_PULLUP);
//  Serial.begin(115200);
  Chrono = millis();
}

void loop() {
  ReadButtons ();

  if (!Position) {
    FastLED.clear();               // Position light OFF
  } else LightAll (BrightnessPos); // Position light ON

  if (Stop) LightAll (BrightnessStop);    // Stop light ON

  if (!TurnRight) TurnSignalRightUp ();   // Do the right wave up
  else NTurnRight = NUM_LEDS1 / 2 - 1;
  if (!TurnLeft) TurnSignalLeftDown ();   // Do the left wave down
  else NTurnLeft  = NUM_LEDS1 / 2 - 1;

  FastLED.show();
}

Please tell me!

Everything works the way I wanted :wink: Thank you very much, lesept :slight_smile: . You have a lot of knowledge about programming in Arduino. I do not know how I can thank you. I'm ashamed to continue to ask for help because you've helped me a lot but In the free time if you can improve it to make the emergency lights work. Because now, as I give the mass on pin 4 and pin 6, the rulers change between themselves. It would be good if you could get the effect of emergency lights by giving and removing masses from pins 4 and 6.

Thank you very much !

You mean that if both pins 4 & 6 are LOW, both left and right signs should be synchronized. OK, I'll do it shortly. Do you want that I put only one orange moving led when the stop button is pushed?

I will say it simpler. So that you can use the left and right direction indicators, at the same time . Now, when I plug in the masses on pin 4 and 6 at the same time, the waves change among themselves. Thus, you can not make the effect of hazard lights.

I think it can be done quite simply: try this and tell me...

#include <FastLED.h>
#define LED_PIN1     2
#define NUM_LEDS1   16

// buttons / switches for commands
#define StopButton  3
#define LeftSignal  4
#define PosSwitch   5
#define RightSignal 6

CRGB leds[NUM_LEDS1]; // 0-7 left, 8-15 right

byte NTurnLeft  = NUM_LEDS1 / 2 - 1;
byte NTurnRight = NUM_LEDS1 / 2 - 1;
bool TurnLeft   = false;  //
bool TurnRight  = false;  //
bool Stop       = false;  // stop light on or off ?
bool Position   = true;   // position light on or off ?

const byte BrightnessPos  =  40; // percentage
const byte BrightnessTurn = 100;
const byte BrightnessStop = 100;

unsigned long TurnDur     = 30ul;
unsigned long ChronoLeft  = 0;
unsigned long ChronoRight = 0;

void ReadButtons () {
  TurnLeft  = digitalRead(LeftSignal);
  TurnRight = digitalRead(RightSignal);
  Stop      = !digitalRead(StopButton);
  Position  = !digitalRead(PosSwitch);
  delay(20); // for debouncing
}

void TurnSignalRightUp () {
  if (millis() - ChronoRight >= TurnDur) {
    ChronoRight = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - ChronoLeft >= TurnDur) {
    ChronoRight = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnLeft --;
    if (NTurnLeft > 254) {
      NTurnLeft = 0;
    }
  }
}

void LightAll (byte Bright) {
  int red = 255 * Bright / 100;
  fill_solid(leds, NUM_LEDS1, CRGB(red, 0, 0));
}

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
  pinMode (StopButton,  INPUT_PULLUP);
  pinMode (LeftSignal,  INPUT_PULLUP);
  pinMode (PosSwitch,   INPUT_PULLUP);
  pinMode (RightSignal, INPUT_PULLUP);
//  Serial.begin(115200);
  ChronoLeft = millis();
  ChronoRight = ChronoLeft;
}

void loop() {
  ReadButtons ();

  if (!Position) {
    FastLED.clear();               // Position light OFF
  } else LightAll (BrightnessPos); // Position light ON

  if (Stop) LightAll (BrightnessStop);    // Stop light ON

  if (!TurnRight) TurnSignalRightUp ();   // Do the right wave up
  else NTurnRight = NUM_LEDS1 / 2 - 1;
  if (!TurnLeft) TurnSignalLeftDown ();   // Do the left wave down
  else NTurnLeft  = NUM_LEDS1 / 2 - 1;

  FastLED.show();
}

I added the single led turn signal in case the stop button is pusshed, and corrected a bug. This version should be OK.

#include <FastLED.h>
#define LED_PIN1     2
#define NUM_LEDS1   16

// buttons / switches for commands
#define StopButton  3
#define LeftSignal  4
#define PosSwitch   5
#define RightSignal 6

CRGB leds[NUM_LEDS1]; // 0-7 left, 8-15 right

byte NTurnLeft  = NUM_LEDS1 / 2 - 1;
byte NTurnRight = NUM_LEDS1 / 2 + 1;
bool TurnLeft   = false;  //
bool TurnRight  = false;  //
bool Stop       = false;  // stop light on or off ?
bool Position   = true;   // position light on or off ?

const byte BrightnessPos  =  40; // percentage
const byte BrightnessTurn = 100;
const byte BrightnessStop = 100;

unsigned long TurnDur     = 30ul;
unsigned long ChronoLeft  = 0;
unsigned long ChronoRight = 0;

void ReadButtons () {
  TurnLeft  = digitalRead(LeftSignal);
  TurnRight = digitalRead(RightSignal);
  Stop      = !digitalRead(StopButton);
  Position  = !digitalRead(PosSwitch);
  delay(20); // for debouncing
}

void TurnSignalRightUp () {
  if (millis() - ChronoRight >= TurnDur) {
    ChronoRight = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    if (Stop) leds[NTurnRight - 1] = CRGB(orangeR, orangeG, 0);
    else for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - ChronoLeft >= TurnDur) {
    ChronoRight = millis();
    int orangeR  = 255 * BrightnessTurn / 100;
    int orangeG  = 150 * BrightnessTurn / 100;
    if (Stop) leds[NTurnLeft] = CRGB(orangeR, orangeG, 0);
    else for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orangeR, orangeG, 0);
    NTurnLeft --;
    if (NTurnLeft > 254) {
      NTurnLeft = 0;
    }
  }
}

void LightAll (byte Bright) {
  int red = 255 * Bright / 100;
  fill_solid(leds, NUM_LEDS1, CRGB(red, 0, 0));
}

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
  pinMode (StopButton,  INPUT_PULLUP);
  pinMode (LeftSignal,  INPUT_PULLUP);
  pinMode (PosSwitch,   INPUT_PULLUP);
  pinMode (RightSignal, INPUT_PULLUP);
  //  Serial.begin(115200);
  ChronoLeft = millis();
  ChronoRight = ChronoLeft;
}

void loop() {
  ReadButtons ();

  if (!Position) {
    FastLED.clear();               // Position light OFF
  } else LightAll (BrightnessPos); // Position light ON

  if (Stop) LightAll (BrightnessStop);    // Stop light ON

  if (!TurnRight) TurnSignalRightUp ();   // Do the right wave up
  else NTurnRight = NUM_LEDS1 / 2 + 1;
  if (!TurnLeft) TurnSignalLeftDown ();   // Do the left wave down
  else NTurnLeft  = NUM_LEDS1 / 2 - 1;

  FastLED.show();
}

Ok, so the code from the post: 47 works as it should. :slight_smile:
Thank you again for your help and time. I think the topic is resolved. :wink:

Better test the code from answer #48, and use it if you find it ok.
Enjoy! I'd like a picture or video when you install it on your bike...

hello guys...good project,this is what I lookig for
I have one question, when I put right turn leds flashing with stroboskope efect ..could someone check it

Best regards

I made the video how screch working MEGA

thank U

You can slow it down by increasing the value here

unsigned long TurnDur     = 30ul;

Try 60 or 100 instead of 30...

thank U lesept .........value 5 is the best

everything working like harm

Good !

I have try to modify this for 2 separate strip one for left and another for right.

not able to success

Anyone can help?

alaminxp:
I have try to modify this for 2 separate strip one for left and another for right.

not able to success

Anyone can help?

Without seeing your code?
Not a hope.

#include <FastLED.h>

#define LED_PIN1     5
#define LED_PIN     6
#define NUM_LEDS    8

// buttons for commands
#define StopButton 3
#define ClignoButton 8
#define ClignoButtonR 4
#define PosButton 2
#define EmButton 9
#define PoliceButton 10
CRGB leds1[NUM_LEDS];
CRGB leds[NUM_LEDS];

byte NCligno = 0;
byte NClignoR = 0;
bool Cligno = false;
bool ClignoR = false;
bool Stop = false;
bool Em = false;
bool Police = false;
bool Position = true;

const byte BrightnessPos = 4; // %
const byte BrightnessCligno = 10;
const byte BrightnessStop = 10;

unsigned long ClignoDur = 20ul;
unsigned long ClignoDurR = 20ul;
unsigned long Chrono = 0;
unsigned long ChronoR = 0;

void ReadButtons () {
  Cligno = !digitalRead(ClignoButton);
  ClignoR = !digitalRead(ClignoButtonR);
  Stop = !digitalRead(StopButton);
  Position = !digitalRead(PosButton);
  Em = !digitalRead(EmButton);
  Police = !digitalRead(PoliceButton);
  delay(25);
}

void LightCligno (byte n) {
  int orange = 255 * BrightnessCligno / 100;
  for (int i = 0; i < n; i++) leds[i] = CRGB(orange, orange, 0);
}
void LightClignoR (byte n) {
  int orange = 255 * BrightnessCligno / 100;
  for (int i = 0; i < n; i++) leds1[i] = CRGB(orange, orange, 0);
}

void LightAll (byte b) {
  int red = 255 * b / 100;
  for (int i = 0; i < NUM_LEDS; i++) leds1[i] = CRGB(red, 0, 0);
  for (int i = 0; i < NUM_LEDS; i++) leds[i] = CRGB(red, 0, 0);
}
void LightEm (byte n) {
  int orange = 255 * BrightnessCligno / 100;
  for (int i = 0; i < n; i++) leds[i] = CRGB(orange, orange, 0);
  for (int i = 0; i < n; i++) leds1[i] = CRGB(orange, orange, 0);
}

void LightPolice (byte b) {
  int red = 255 * b / 100;
  for (int i = 0; i < b; i++) leds1[i] = CRGB(0, 0, red);
  for (int i = 0; i < b; i++) leds[i] = CRGB(red, 0, 0);
}

void setup() {
  FastLED.addLeds<WS2812, LED_PIN1, GRB>(leds1, NUM_LEDS);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode (StopButton, INPUT_PULLUP);
  pinMode (ClignoButton, INPUT_PULLUP);
  pinMode (ClignoButtonR, INPUT_PULLUP);
  pinMode (PosButton, INPUT_PULLUP);
  pinMode (EmButton, INPUT_PULLUP);
  pinMode (PoliceButton, INPUT_PULLUP);
  Serial.begin(115200);
  Chrono = millis();
  ChronoR = millis();
}

void loop() {
  ReadButtons ();

  if (!Position) {
    FastLED.clear();
  } else LightAll (BrightnessPos);

  if (Stop) LightAll (BrightnessStop);

  if (Cligno) {
    if (millis() - Chrono > ClignoDur) {
      Chrono = millis();
      NCligno = (NCligno + 1) % NUM_LEDS;
      LightCligno (NCligno);
    }

  }
  if (ClignoR) {
    if (millis() - ChronoR > ClignoDurR) {
      ChronoR = millis();
      NClignoR = (NClignoR + 1) % NUM_LEDS;
      LightClignoR (NClignoR);
    }
  }

  if (Em) {
    if (millis() - Chrono > ClignoDur) {
      Chrono = millis();
      NCligno = (NCligno + 1) % NUM_LEDS;
      LightCligno (NCligno);
    }
    if (millis() - ChronoR > ClignoDurR) {
      ChronoR = millis();
      NClignoR = (NClignoR + 1) % NUM_LEDS;
      LightClignoR (NClignoR);
    }
  }

//  if (Police) LightPolice (BrightnessStop);


  if (Police) {
    if (millis() - Chrono > ClignoDur) {
      Chrono = millis();
      NCligno = (NCligno + 1) % NUM_LEDS;
      LightPolice (NCligno);
    }
    if (millis() - ChronoR > ClignoDurR) {
      ChronoR = millis();
      NClignoR = (NClignoR + 1) % NUM_LEDS;
      LightPolice (NClignoR);
    }
  }

  
  FastLED.show();
}

I have done 2 separate Strip.

Now here is the problem with police light animation
I am not able to achieve the result like police Light Effect.

would this still work with 107 leds ?

Paul__B:
No, but I will help you to show your code:

#include <FastLED.h>

#define LED_PIN     2
#define NUM_LEDS    16

CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
 
}

void loop() {

leds[7] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[6] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[5] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[4] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[3] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[2] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[1] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[0] = CRGB(255,255,0);
 FastLED.show();
 delay(70);               // left turn signal

FastLED.clear(); {

}
leds[8] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[9] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[10] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[11] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[12] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[13] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[14] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[15] = CRGB(255,255,0);
 FastLED.show();
 delay(70);             // right turn signal

FastLED.clear();
}

Paul__B:
No, but I will help you to show your code:

#include <FastLED.h>

#define LED_PIN     2
#define NUM_LEDS    16

CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
 
}

void loop() {

leds[7] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[6] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[5] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[4] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[3] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[2] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[1] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[0] = CRGB(255,255,0);
 FastLED.show();
 delay(70);               // left turn signal

FastLED.clear(); {

}
leds[8] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[9] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[10] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[11] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[12] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[13] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[14] = CRGB(255,255,0);
 FastLED.show();
 delay(60);
 leds[15] = CRGB(255,255,0);
 FastLED.show();
 delay(70);             // right turn signal

FastLED.clear();
}