Dynamic turn signals stop and parking lights - WS2812b

Hi guys :slight_smile:
I have question for you. I want to make lighting for my motorcycle trunk.
I have a WS2812b led strip (8 led per page) and Arduino pro mini. I tried to write something myself but the problem is that I do not know the arduino programming environment ( and I can not write code for controlling such lighting. Can any of you help me write such a code?
I apologize if I wrote this topic in the wrong part.

Greetings.

I tried to write something myself

So, post that.

I wrote something like that. These are dynamic directions. I do not know how to do the rest, i.e. parking lights and braking, and how to do it using the voltage from the motorcycle.
Can anyone help me to write this code to control these LEDs?

Thank you

DSGFlash:
Can anyone help me to write this code to control these LEDs?

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(); 
}

I can help you with the code because I like the project, but:

  • I'm not sure that FastLED can dynamically control the brightness, and set different brightness values in a single led strip
  • And you'll have to provide 5V or 3.3V input for the commands.

Here is a first version

#include <FastLED.h>

#define LED_PIN1     2
#define NUM_LEDS1    16

// buttons for commands
#define StopButton 3
#define ClignoButton 4
#define PosButton 5

CRGB leds[NUM_LEDS];

byte NCligno = 0;
bool Cligno = false;
bool Stop = false;
bool Position = true;
const byte BrightnessPos = 40; // %
const byte BrightnessCligno = 100;
const byte BrightnessStop = 100;

unsigned long ClignoDur = 100ul;
unsigned long Chrono = 0;

void ReadButtons () {
    Cligno = !digitalRead(ClignoButton);
    Stop = !digitalRead(StopButton);
    Position = !digitalRead(PosButton);
    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 LightAll (byte b) {
  int red = 255*b/100;
  for (int i=0;i<NUM_LEDS1;i++) leds[i] = CRGB(red,0,0);
}

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

void loop() {
  ReadButtons ();
    
  if (!Position) {
    FastLED.clear();
  } else LightAll (BrightnessPos);
  
  if (Stop) LightAll (BrightnessStop);
    
  else if (Cligno) {
    if (millis()-Chrono>ClignoDur) {
      Chrono = millis();
      NCligno = (NCligno+1)%NUM_LEDS1;
      LightCligno (NCligno);
    }
  }
  
  FastLED.show();
}

It only addresses one led strip of 16 leds, for only one side. When it works, we'll work on the version for two led strips.

It assumes 3 buttons, which are actually pushbuttons. One for the dim red lights (I called it position, as in French) connected between pin 5 and GND. One for the stop light, connected between pin 3 and GND. The last one for turn signal, between pin 4 and GND (called cligno for clignotant).

If no button is pushed, all the leds are off.
If the stop button is pushed, whatever the status of the other buttons, all the leds are on, red with high brightness.
If only the position button is pushed, all the leds are red but with lower brightness. Brightness values can be modified, from 0 to 100.
If the turn button is pushed, the leds lit one after the other, hopefully orange, with high brightness. If the position button is also pushed, the remaining leds are red with lower brightness.

Test it, and tell me what you see...

It is exactly as you wrote. Lights stop and position lights, work well. In turn direction lights, I changed the value of "unsigned long ClignoDur to value = 0ul, because at the value of 100ul LEDs was flashing. At this point in the code you need to add 1 because Arduino throws an error: CRGB leds[NUM_LEDS1]; Regarding the directions lights, it would be necessary to synchronize them with those that I have in a motorcycle. That they ignite and off at the same moment. The direction light function now works in the loop. It seems to me that this loop would have to be removed. Therefore, that after each time a mass pulse is given, the left or right direction would perform only one full cycle of the dynamic direction. Then it will be easier to synchronize with them the direction indicator that I have in the motorcycle. And the ability to adjust the "swimming" speed of the dynamic direction indicators for the right and left turn signals. I hope you know what I mean. You also have to divide them into the left and right side after 8 led. Regarding the control I will try to make a transistor-based circuit or use relays.
@lesept Thank you for your help :slight_smile:

Hi
I'm more comfortable with code than with electronics, so I can only help on the code part. I don't know how to synchronize the turn lights with what you call the impulse of the mass. Maybe someone else in this forum understands better than me what it is and how to wire the arduino to catch that signal.

I supposed in my code that you would be testing with pushbuttons but in the real implementation, I think that the commands will be from switches, except for the stop light. In this case, the delay line should be removed from the code, and you should use a capacitance to cancel the possible bounces at the stop button. You'll find lots of information about this on the internet.

Then, the value of ClignoDur should be set to a non zero value, and this value will help synchronize the wave and your existing turn light.
This will need to be set by trial and error, unless you can precisely measure the period of your existing turn signal.

I'll provide a new version with the 2 directions, you ll need another button or switch, one for turning left and one for turning right. I mean for testing purpose, but for the real implementation, you'll have to tell me how you get the control signals from your bike.

Do you also need a 'warning' signal, as we call it in French, ie both turn waves synchronized? If yes, this will require another button or switch to command it.

The direction light function now works in the loop. It seems to me that this loop would have to be removed. Therefore, that after each time a mass pulse is given, the left or right direction would perform only one full cycle of the dynamic direction. Then it will be easier to synchronize with them the direction indicator that I have in the motorcycle. And the ability to adjust the "swimming" speed of the dynamic direction indicators for the right and left turn signals. All signals from the motorcycle are controlled using 12v. Mass control for arduino can stay I will try to make a circuit using transistor or relays. Warning lights (emergency) you do not have to do this this can be done using the left and right turn signal.

What you call "mass signal" must be a parasitic signal that I would translate as "a call for current" from the light bulb when it's switched on (but my knowledge in electronics is quite low so there may be another explanation). I don't think it can be fed directly to a pin of the arduino. You should catch it with another circuit which will provide a 'correct" signal to the Arduino.

Now, for the code: do you have switches?

If yes, I can change the code accordingly. If you can put a switch for left / right direction, and add a pushbutton that would simulate that "mass signal", I can rewrite the code to manage that. There, we need to decide when to stop the wave, maybe with another push on this pushbutton for the moment.

Similarly, you can put ON/OFF switches for position and stop lights, although I don't think the code needs any change for those.

Here is the new version :

#include <FastLED.h>

#define LED_PIN1     2
#define NUM_LEDS1    16

// buttons for commands (all connected between pin and GND)
#define StopButton 3
#define ClignoSwitch 4
#define PosSwitch 5
#define MassSignal 6

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

byte NCligno = 0;
bool Cligno = false; // received mass signal ?
bool ClignoState = false; // turn light on or off ?
bool ClignoDir = false; // true : right, false : left
bool Stop = false;
bool Position = true;
const byte BrightnessPos = 40; // %
const byte BrightnessCligno = 100;
const byte BrightnessStop = 100;

unsigned long ClignoDur = 10ul;
unsigned long Chrono = 0;

void ReadButtons () {
  ClignoDir = digitalRead(ClignoSwitch); // put a ! if not the correct direction
  Cligno = !digitalRead(MassSignal);
  Stop = !digitalRead(StopButton);
  Position = !digitalRead(PosSwitch);
  delay(20); // To be removed eventually
  if (Cligno) ClignoState = !ClignoState;
}

void LightCligno (byte n, bool Direction) {
  byte Offset = (Direction) ? 8 : 0;
  int orange = 255 * BrightnessCligno / 100;
  for (int i = 0; i < n; i++) leds[i + Offset] = CRGB(orange, orange, 0);
}

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

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
  pinMode (StopButton, INPUT_PULLUP);
  pinMode (ClignoSwitch, INPUT_PULLUP);
  pinMode (PosSwitch, INPUT_PULLUP);
  pinMode (MassSignal, 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

  else if (ClignoState) {
    if (millis() - Chrono > ClignoDur) {
      Chrono = millis();
      NCligno = (NCligno + 1) % (NUM_LEDS1 / 2);
      LightCligno (NCligno, ClignoDir);
    }
  }

  FastLED.show();
}

So you need switches (or pushbuttons and you keep them pushed to simulate the switches) for turn and position lights, and pushbuttons for stop and mass signals. The strip is divided in 2 parts (left and right) and hopefully the lit part will depend on the direction switch... They begin to flash when the mass button is pushed and stop when it is pushed again.

Try it and tell me how it works...

Works only the right-hand direction on pin 6, on pin 4 not works, the left-hand direction. This direction still works in the loop. The wave works on 7 diodes and should be on 8. The diodes from 8 to 14 are on. Diode 15 should also light up (diodes count from 0 like Arduino).
The left turn signal works when the right turn signal is on and gives the mass on pin 4. It also lights up in a wave of 7 LEDs, from: 0 to: 6, LED 7 should also light up and the wave should work the other way from diode: 7 to diode: 0. The mass impulse be given at the frequency with which the blinkers of the motorcycle flash. So it must work on the principle that after giving the mass the direction must perform one full cycle of the wave and after the next mass application it must again make a full wave cycle and so on over and over when the mass impulse is applied. If we used the mass to stop the wave then the direction would flash every other cycle.
Yes I have switches I do it on the contact board. Can the disappearance of the mass pulse cause the wave to stop? What I mean is that arduino gets a mass pulse from the motorcycle turn signal on the input, lights the wave (one cycle, no loop), the mass impulse from the motorcycle disappears and then the wave is stopped and so on and on? I know it is easier to do in the loop but then it will be harder to synchronize the direction indicators with each other.

Position lights and brake lights work well and you do not have to change anything in the code for them.

Works only the right-hand direction on pin 6, on pin 4 not works, the left-hand direction.

Actually, all the leds are connected to pin 2. Pin 6 is connected to the switch: please refer here to see how to connect the switch.

Here is a new version with turn signal going up and down.

#include <FastLED.h>

#define LED_PIN1     2
#define NUM_LEDS1    16

// buttons for commands (all connected between pin and GND)
#define StopButton 3
#define ClignoSwitch 4
#define PosSwitch 5
#define MassSignal 6

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

byte NCligno = 0;
bool Cligno = false; // received mass signal ?
bool ClignoState = false; // turn light on or off ?
bool ClignoDir = false; // true : right, false : left
bool ClignoUp = true; // true : lights from 0 to 7, false : the other way
bool Stop = false;
bool Position = true;
const byte BrightnessPos = 40; // %
const byte BrightnessCligno = 100;
const byte BrightnessStop = 100;

unsigned long ClignoDur = 10ul;
unsigned long Chrono = 0;

void ReadButtons () {
  ClignoDir = digitalRead(ClignoSwitch); // put a ! if not the correct direction
  Cligno = !digitalRead(MassSignal);
  Stop = !digitalRead(StopButton);
  Position = !digitalRead(PosSwitch);
  delay(20); // To be removed eventually
  if (Cligno) ClignoState = !ClignoState;
}

void LightCligno (byte n, bool Direction) {
  byte Offset = (Direction) ? 8 : 0;
  int orange = 255 * BrightnessCligno / 100;
  for (int i = 0; i < n; i++) leds[i + Offset] = CRGB(orange, orange, 0);
  if (!ClignoUp && !Position) leds[n] = CRGB(0, 0, 0);
  if (!ClignoUp &&  Position) {
    int red = 255 * BrightnessPos / 100;
    leds[n] = CRGB(red, 0, 0);
  }
}

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

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds, NUM_LEDS1);
  pinMode (StopButton, INPUT_PULLUP);
  pinMode (ClignoSwitch, INPUT_PULLUP);
  pinMode (PosSwitch, INPUT_PULLUP);
  pinMode (MassSignal, 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

  else if (ClignoState) {
    if (millis() - Chrono > ClignoDur) {
      Chrono = millis();
      LightCligno (NCligno, ClignoDir);
      if (ClignoUp) {
        NCligno += 1;
        if (NCligno == NUM_LEDS1 / 2) ClignoUp = !ClignoUp;
      }
      else {
        NCligno -= 1;
        if (NCligno == 0) ClignoUp = !ClignoUp;
      }
    }
  }

  FastLED.show();
}

I understand only pinn number 4, it is not possible to turn on the direction. Only with pin 6, and pin 4 switches to the other direction indicator. This must work independently that pin 6 can be used to turn the right turn signal on and off. By contrast, the pin 4 must also work independently to turn the left turn signal on and off. In this code, it is not possible to turn on the left blinker with pin 4. You must turn on the right turn signal with pin 6 and then hold the mass on pin 4 then the left direction works. Now the wave works after 8 LEDs per side as it should, but there is another wave effect. I sent you a movie on PM as there is now a wave effect and how it should be. Can you change the lighting order of the left turn signal wave? Now the wave of the left direction starts from diode 0 to diode 7, if you can change it on starts from diode 7 and ends with diode: 0. Thank you very much for your help, you helped me a lot and I'm sorry that I am still asking for help, but I can not do it myself.

No worries, I said I'd help you and I'm glad to do it. I'll watch the videos, which one does show the effect you want?

I don't understand what you mean here:

I understand only pin number 4, it is not possible to turn on the direction. Only with pin 6, and pin 4 switches to the other direction indicator. This must work independently that pin 6 can be used to turn the right turn signal on and off. By contrast, the pin 4 must also work independently to turn the left turn signal on and off. In this code, it is not possible to turn on the left blinker with pin 4. You must turn on the right turn signal with pin 6 and then hold the mass on pin 4 then the left direction works.

Pin 4 should be connected to a switch. This device provides either LOW or HIGH to pin 4, depending on the direction it points to. Did you read how to connect the switch in the URL I recommended? So depending on the value the pin receives, the left turn or the right turn signal is shown. At least, this is what I tried to code :slight_smile:

Then, pin 6 is connected to the so-called mass signal. For now, it launches the wave and another push stops it.

A movie called: "It must be so.mp4" shows the desired effect. At the same time, the diodes from the range from 0 to: 7 must light up in the reverse order, that is from the diode: 7 to the diode: 0. Exactly this code works as you wrote.
I sent you a movie in PM how the work looks like, see if it works correctly.

Thanks for the video, it's nice to see it working. I think you should slow it down by increasing the value of ClignoDur
So, is this ok for you as it is ?

I will manage the change in speed.

Ok, please explain again what you need, I didn't understand your answer #15...

Hi
I understood this morning that the 'mass signal' must be used for the synchronization of the turn signal (the wave, up and down), so I'll send you another version taking this into account. The mass signal will launch only one wave (up and down) on the side given by the direction switch.

I had an idea, tell me what you think. For now, if you have the sign turn on and if you brake, the sign turn disappears and you only see the brake lights as long as you push the stop button. But I can add a reminder of the turn sign while the stop lights are lit: instead of the wave, I can lit only one led, going up and down.

It's not a complex modification, what do you think about it?