Go Down

Topic: Dynamic turn signals stop and parking lights - WS2812b (Read 2432 times) previous topic - next topic

lesept

Try with this:

Code: [Select]
#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;
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 orange  = 255 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orange, orange, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
      TurnRight = true;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - Chrono > TurnDur) {
    Chrono = millis();
    int orange  = 255 * BrightnessTurn / 100;
    for (int i = 0; i < NTurnLeft; i++) leds[i] = CRGB(orange, orange, 0);
    NTurnLeft --;
    if (NTurnLeft < 0) {
      NTurnLeft = 0;
      TurnLeft = true;
    }
  }
}

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;

  FastLED.show();
}
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

Try with this
Now it works well (that's what I meant).  ;)  For pin number 4 you have to do the same only LEDs are to light up from the diode: 7 to diode: 0. Mass control must work the same as for pin 6.

lesept

Sorry, this must be better for pin 4:
Code: [Select]
#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 orange  = 255 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orange, orange, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
      TurnRight = true;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - Chrono > TurnDur) {
    Chrono = millis();
    int orange  = 255 * BrightnessTurn / 100;
    for (int i = 0; i < NTurnLeft; i++) leds[i] = CRGB(orange, orange, 0);
    NTurnLeft --;
    if (NTurnLeft < 0) {
      NTurnLeft = 0;
      TurnLeft = true;
    }
  }
}

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

Do the position lights work (pin 5)?
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

Pin 4 is still working badly. See the movie that I sent you. The control of this pin 4 must work in the same way as for pin 6, only the LEDs light must be up from: 7 to: 0. Yes, the position lamps are working. The rest works fine except for pin 4

lesept

This one must be OK:
Code: [Select]
#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 orange  = 255 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i < NTurnRight; i++) leds[i] = CRGB(orange, orange, 0);
    NTurnRight ++;
    if (NTurnRight > NUM_LEDS1) {
      NTurnRight = NUM_LEDS1;
      TurnRight = true;
    }
  }
}

void TurnSignalLeftDown () {
  if (millis() - Chrono > TurnDur) {
    Chrono = millis();
    int orange  = 255 * BrightnessTurn / 100;
    for (int i = NUM_LEDS1 / 2; i > NTurnLeft; i--) leds[i] = CRGB(orange, orange, 0);
    NTurnLeft --;
    if (NTurnLeft < 0) {
      NTurnLeft = 0;
      TurnLeft = true;
    }
  }
}

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();
}
(Hopefully :) )
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

This one must be OK
The direction of the wave is good but the diodes must light up from: 7 to: 0. In this code they light up from: 8 to: 1. After giving the mass on the pin: 4, the wave performs one full cycle and goes out it should shine to the moment of holding the mass. And go out after subtracting the mass. Correct it and it will be fine :)

lesept

In function TurnSignalLeftDown, change
Code: [Select]
    for (int i = NUM_LEDS1 / 2; i > NTurnLeft; i--) leds[i] = CRGB(orange, orange, 0);
to
Code: [Select]
    for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orange, orange, 0);


it should shine to the moment of holding the mass. And go out after subtracting the mass. Correct it and it will be fine :)
Do you mean that the leds (either left or right) must stay ON while led 4 or 6 are at GND?
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

If this change worked, then try this :

Code: [Select]
#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;
//      TurnRight = true;
    }
  }
}

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 < 0) {
      NTurnLeft = 0;
//      TurnLeft = true;
    }
  }
}

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();
}
The turn signs should stay lit while pins 4 or 6 are at GND.

I also change the orange color to have it more ... orange :)
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

In function TurnSignalLeftDown, change
Code: [Select]
   for (int i = NUM_LEDS1 / 2; i > NTurnLeft; i--) leds[i] = CRGB(orange, orange, 0);

to
Code: [Select]
   for (int i = NUM_LEDS1 / 2 - 1; i >= NTurnLeft; i--) leds[i] = CRGB(orange, orange, 0);

Do you mean that the leds (either left or right) must stay ON while led 4 or 6 are at GND?
Ok, I changed the line now, it's shining like it was supposed to be. No no. The turn signal right under pin 6 works well and so is to stay. Look at the movie that I sent to you how the left turn signal behaves after giving a constant mass on pin 4. It should behave the same as diodes on pine 6. When I give the mass to the pin: 4 wave should perform one full cycle and stop. Uncouple the mass and it should go out.

lesept

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

If you want to get back to before, uncomment lines 44 and 58.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

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.

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.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

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

lesept

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:
Code: [Select]
#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!
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

DSGFlash

Everything works the way I wanted ;) Thank you very much, lesept  :) . 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 !

Go Up