Go Down

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

lesept

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?
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 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.

lesept

I think it can be done quite simply: try this and tell me...
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 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();
}
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

I added the single led turn signal in case the stop button is pusshed, and corrected a bug. This version should 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 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();
}
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, so the code from the post: 47 works as it should.  :)
Thank you again for your help and time. I think the topic is resolved.  :smiley-wink:

lesept

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

gpdriver

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 https://mega.nz/#!8UgWnYLB!yrfuhI_r_WA0_ck6mV84aVjL4AcnRxpBLGIQ2aXDeYw

thank U

lesept

You can slow it down by increasing the value here
Code: [Select]
unsigned long TurnDur     = 30ul;
Try 60 or 100 instead of 30...
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

gpdriver

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

    everything working like harm

lesept

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

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?

AWOL

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.

alaminxp

Code: [Select]
#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.


Go Up