WS2812B LED light project

#include <Adafruit_NeoPixel.h>

#define PIN 7
#define BRIGHTNESS 255
#define NUM_PIXELS  51

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
int ledPin            = 7;
int LeftPin           = 2;
int RightPin          = 3;
int ReversePin        = 4;
int tailLightPin      = 5;
int wellcomePin       = 6;



void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(LeftPin, INPUT);
  pinMode(RightPin, INPUT);
  pinMode(ReversePin, INPUT);
  pinMode(tailLightPin, INPUT);
  pinMode(wellcomePin, INPUT);

  strip.begin();
  strip.setBrightness(BRIGHTNESS);
  strip.show();
  //(color, EyeSize, SpeedDelay, ReturnDelay)
  //NewKITT(255, 0, 0, 4, 15, 15);
}

//Inputs and Outputs Functions//
void loop()
{ 
  // Voltage Check and start LEFT TURN
  if (digitalRead(LeftPin) == HIGH && digitalRead(RightPin) == LOW)
  {
    clearStrip(0, 0, 0);
    leftTurn();
  }

  // Voltage Check and start RIGHT TURN
  if (digitalRead(RightPin) == HIGH && digitalRead(LeftPin) == LOW)
  {
    clearStrip(0, 0, 0);
    rightTurn();
  }

  // Voltage Check and start REVERSE.
  if (digitalRead(RightPin) == LOW && digitalRead(LeftPin) == LOW && digitalRead(ReversePin) == HIGH)
  {
    clearStrip(0, 0, 0);
    backUp();
  }

  // Voltage Check and start HAZARD.
  if (digitalRead(LeftPin) == HIGH && digitalRead(RightPin) == HIGH)
  {
    clearStrip(0, 0, 0);
    Hazard();
  }

  // If all INPUT PINS are LOW revert to TAIL LIGHT.
  if (digitalRead(LeftPin) == LOW && digitalRead(RightPin) == LOW && digitalRead(ReversePin) == LOW && digitalRead(tailLightPin) == HIGH )
  {

    NewKITT(255, 0, 0, 4, 15, 15);
    
    runLight();

    
  }
  else
  {
  clearStrip(0, 0, 0);
  }
  
}

// LEFT TURN
void leftTurn()
{
  strip.setBrightness(BRIGHTNESS);
  for (int16_t i = (NUM_PIXELS - 1) / 1; i > -1; i--)
  {
    strip.setPixelColor(i, 255, 130, 0);
    strip.show();
    delay(10);
  }
  delay(10);

  for (int16_t i = NUM_PIXELS / 1; i > -1; i--)
  {
    strip.setPixelColor(i, 0, 0, 0);
    strip.show();
    delay(10);
  }
  delay(10);
}                                                                                                            //LEDs ANIMATION FUNCTIONS//

// RIGHT TURN
void rightTurn()
{
  strip.setBrightness(BRIGHTNESS);
  for (uint16_t i = NUM_PIXELS / 0; i < NUM_PIXELS; i++)
  {
    strip.setPixelColor(i, 255, 130, 0);
    strip.show();
    delay(10);
  }
  delay(10);

  for (uint16_t i = NUM_PIXELS / 0; i < NUM_PIXELS; i++)
  {
    strip.setPixelColor(i, 0, 0, 0);
    strip.show();
    delay(10);
  }
  delay(10);
}

// HAZARD
void Hazard()
{
  strip.setBrightness(BRIGHTNESS);
  middleFill(255, 130, 0, 10);
  strip.show();
}

// REVERSE
void backUp()
{
  strip.setBrightness(BRIGHTNESS);
  /*middleFill1(255,255,255, 15);*/
  //(color, StrobeCount, FlashDelay, EndPause)
  Strobe(255, 255, 255, 8, 50, 5);
  strip.show();
}

// SETALL
void setAll(byte red, byte green, byte blue)
{
  for (int i = 0; i < NUM_PIXELS; i++ )
  strip.setPixelColor(i, red, green, blue);
  strip.show();
}

// DIM RED, RUN-LIGHT STATE
void wellcomeLight()
{
  strip.setBrightness(BRIGHTNESS);
  spread(255, 255, 255 , 15);
  setAll(255, 255, 255);
  strip.show();
}

void runLight()
{
  strip.setBrightness(BRIGHTNESS /2);
  spread(255, 0, 0 , 15);
  setAll(255, 0, 0);
  strip.show();
}

// SET ALL LEDS TO OFF/BLACK
void clearStrip(byte red, byte green, byte blue)
{
  for ( int i = 0; i < NUM_PIXELS; i++)
  {
    strip.setPixelColor(i, red, green, blue);
    strip.show();
  }
}

// FUNCTION TO LIGHT LEDS FROM THE CENTER ONE AT A TIME (SPREADING).
void spread(byte red, byte green, byte blue, int SpeedDelay)
{
  int center = NUM_PIXELS / 2;
  for (int i = 0; i < center; i++)
  {
    strip.setPixelColor(center + i, red, green, blue);
    strip.setPixelColor(center + (i * -1), red, green, blue);
    strip.show();
    delay(SpeedDelay);
  }
}

// MIDDLEFILL AND REVERSE
void middleFill(byte red, byte green, byte blue, int SpeedDelay)
{
  clearStrip(0, 0, 0);
  int center = NUM_PIXELS / 2;

  for (int i = 0; i < center; i++)
  { // START FROM THE MIDDLE, LIGHTING AN LED ON EACH SIDE
    strip.setPixelColor(center + i, red, green, blue);
    strip.setPixelColor(center + (i * -1), red, green, blue);
    strip.show();
    delay(SpeedDelay);
  }

  for (int i = 0; i < center; i++)
  { // REVERSE
    strip.setPixelColor(i, strip.Color(0, 0, 0));
    strip.setPixelColor(NUM_PIXELS - 1 - i, strip.Color(0, 0, 0));
    strip.show();
    delay(SpeedDelay);
  }
  clearStrip(0, 0, 0);
}

// NEWKITT
void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay)
{
  CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
}

//CENTER TO OUTSIDE
void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = ((NUM_PIXELS - EyeSize) / 2); i >= 0; i--) {
    setAll(0, 0, 0);

    strip.setPixelColor(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      strip.setPixelColor(i + j, red, green, blue);
    }
    strip.setPixelColor(i + EyeSize + 1, red / 10, green / 10, blue / 10);

    strip.setPixelColor(NUM_PIXELS - i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      strip.setPixelColor(NUM_PIXELS - i - j, red, green, blue);
    }
    strip.setPixelColor(NUM_PIXELS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

    strip.show();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

//OUTSIDE TO CENTER
void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for (int i = 0; i <= ((NUM_PIXELS - EyeSize) / 2); i++) {
    setAll(0, 0, 0);

    strip.setPixelColor(i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      strip.setPixelColor(i + j, red, green, blue);
    }
    strip.setPixelColor(i + EyeSize + 1, red / 10, green / 10, blue / 10);

    strip.setPixelColor(NUM_PIXELS - i, red / 10, green / 10, blue / 10);
    for (int j = 1; j <= EyeSize; j++) {
      strip.setPixelColor(NUM_PIXELS - i - j, red, green, blue);
    }
    strip.setPixelColor(NUM_PIXELS - i - EyeSize - 1, red / 10, green / 10, blue / 10);

    strip.show();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause)
{
  for (int j = 0; j < StrobeCount; j++)
  {
    setAll(red, green, blue);
    strip.show();
    delay(FlashDelay);
    setAll(0, 0, 0);
    strip.show();
    delay(FlashDelay);
  }
}

Welcome to the forum. Thanks for using code tags.

Is there a question ?

Hi Everyone,

Sorry for some reason my original message did not included in the first post. I am working on a LED light for my vehicle. Ran into problem which I can not able to solve and would be much appreciated if someone can give me some insight.

My project consist of WS281B LEDs and have animation based on the inputs. Right now I got stuck at the TAIL LIGHT.

Right now how I set it up is when all other signals are LOW and tailLight signal is HIGH it will run two functions.

NewKITT runLight

What I would like to do is that NewKITT function to run just once then to the runLight function and the runLight will continue to stays on until the signal is not met.

Right now if I would to includes NewKITT function it kept repeating the animation. All of the codes are pretty much copy and pasted from other's example. If anyone could help me out again I would be much appreciated.

Thank You All.

brz:
What I would like to do is that NewKITT function to run just once then to the runLight function and the runLight will continue to stays on until the signal is not met.

Add a count of how many times you have done NewKITT…

  static int doneNewKITT= 0;  // count of times we have done NewKITT

  // If all INPUT PINS are LOW revert to TAIL LIGHT.
  if (digitalRead(LeftPin) == LOW && digitalRead(RightPin) == LOW && digitalRead(ReversePin) == LOW && digitalRead(tailLightPin) == HIGH )
  {
    if( doneNewKITT < 1 ){
      NewKITT(255, 0, 0, 4, 15, 15);      // do NewKITT one time only
      doneNewKITT= doneNewKITT + 1;
    }
    runLight();
  }
  else
  {
     doneNewKITT= 0;
     clearStrip(0, 0, 0);
  }

Yours,
TonyWilk

Hi Tony,

It works the way I wanted it to be now. I can't express my gratitude enough for your help. I have had been googling and beating my brain out for more than a month now and I just couldn't get it to work. Again Thank You VERY MUCH!! Now I can put put my project together and don't have to stay up late anymore.

Thank You, Eric.

Hi Everyone,

I am back again and hoping to get some help from you guys. Thanks to TonyWilk I got everything to work on my bench test. I then installed all the hardwares to my vehicle.

Everything is working as it should be then when I turn on my turn signals the turn signals activated and alternated with the tail light. I then realize that the turn signals from my vehicle alternates ON and OFF as oppose to during the bench test I hold down the switch to activate the turn signals which is a constant ON to the INPUT Pin.

I was wondering is there a way for me to count the ON and OFF pulses from the turn signals to get the turn signals to activate but the tail light to ignore it until there is no more pulses from the turn signals?

Again I be much appreciated

Thank you, All.

Hi,

brz:
I then realize that the turn signals from my vehicle alternates ON and OFF as oppose to during the bench test I hold down the switch to activate the turn signals which is a constant ON to the INPUT Pin.

Looks like your turn signal animation ‘ripples’ the LEDs ON and then off again, taking about 1 second.

I guess this should be slightly shorter than the ON/OFF of the vehicle signal, so the LED animation follows the same timing like:

  Blink ON     LEDs ripple ON
  Blink OFF    LEDs ripple OFF
  ...                              <-- don't let Tail light activate here 
  Blink ON     LEDs ripple ON
  etc.

At first it might seem that you could simply make the Turn animation a bit longer so then it finishes the Blink ON signal is there again. This would actually work, just, maybe.
The problem would be that if Turn finishes and Blink is back ON again, the next Turn animation is a bit late, the next would be later and so on… so your animation gets out of step and after several blinks it would get completely out of step and the Tail Light would come on.

What you need is a timer which you set when the turn animation runs which prevents it reverting to the Tail Light animation in case another ‘Blink’ is going to happen.

There is a tutorial here: Avoid Delay which describes how to make timers using the millis() function.

In your case you need:

unsigned long holdoffTimer;    // declare a variable for the timer at the top of your code


  // after leftTurn() and rightTurn(), set the holdoff timer like this:

  holdoffTimer= millis();


  // Before you test to do the Tail Light, test this timer like:

  if( (millis() - holdoffTimer)  < 500 )
  {
    // less that 500mS since last turn indication, so skip the Tail Light
    return;
  }

Yours,
TonyWilk

// Before you test to do the Tail Light, test this timer like:

  if( (millis() - holdoffTimer)  < 500 )
  {
    // less that 500mS since last turn indication, so skip the Tail Light
    return;
  }

Hi Tony,

Thank you again for your help. Sorry for being stupid, I have no problem following the first two codes. The last one I get a bit confused where to put the code in. Again I really appreciate for you to take your time to help me out. Thank You.

Hi TonyWilk,

I got it to work now after I played with a codes a bunch of times. Thanks again, you probably got sick of me saying that word but I am really appreciated your help.

:)