Neopixel Patten Issue

Hi all,
I have this program that works 95% but need help with the code a bit
this is a turn signal and brake light and running lights. the following scenario I am trying to achieve is

15 pixels :

8 for left turn
8 for right turn
brake light all on if alone
running light on dimmed by default

when I press the brakes it overrides the running lights brighter then back to running lights once foot off pedal.

when I apply turn signal 8 on - 7 off ,unless I press the brakes or vice versa to have
8 signal lights on and the rest red steady.

hope it makes sense.

#include <Adafruit_NeoPixel.h>

#define LeftSignal A5     // Analog Pin 5 used as trigger for left turn-signal
#define RightSignal A4    // Analog Pin 4 used as trigger for right turn-signal
#define BrakeSignal A3  // Analog Pin 3 used as trigger for reverse light
#define PIN 2             // NeoPixel data pin  
#define BRIGHTNESS 40    // Full brightness control
#define NUM_PIXELS 15      // Total number of NeoPixels
#define WIPE_SPEED 70     // Sweep animation delay.

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);

int LeftBlinkerState  = 0;
int RightBlinkerState = 0;
int BrakeLightState = 0;
int runLightState     = 0;
int BrakeState      = 0;

uint32_t AMBER = strip.Color(255, 130, 0);
uint32_t RED   = strip.Color(255, 0, 0);
uint32_t BLUE  = strip.Color(0, 0, 255);
uint32_t OFF   = strip.Color(0, 0, 0);

/**
   Setup pins and do fancy start-up sequence.
   Red/White/Blue Larson scanner.
*/
void setup() 
{
  Serial.begin(115000);
  pinMode(LeftSignal, INPUT);
  pinMode(RightSignal, INPUT);
  pinMode(BrakeSignal, INPUT);
  strip.begin();
  strip.setBrightness(BRIGHTNESS);
  strip.show();
  //knightRider(1, WIPE_SPEED, 2, RED);
  //knightRider(1, WIPE_SPEED, 2, RED);
  //knightRider(1, WIPE_SPEED, 2, BLUE);
}

/**
   Main loop
*/
void loop()
{
  // Read the state of the left turn-signal voltage
  LeftBlinkerState = analogRead(LeftSignal);
  float LeftVoltage = LeftBlinkerState * (5.0 / 1023.0);
  Serial.println(LeftVoltage);

  // Read the state of the right turn-signal voltage
  RightBlinkerState = analogRead(RightSignal);
  float RightVoltage = RightBlinkerState * (5.0 / 1023.0);
  Serial.println(RightVoltage);

  // Read the state of the reverse lights voltage
  BrakeLightState = analogRead(BrakeSignal);
  float BrakeVoltage = BrakeLightState * (5.0 / 1023.0);
  Serial.println(BrakeVoltage);

  // If we have voltage clear the strip from 'run light state'
  // and start Larson scan to the left.
  if (LeftVoltage > 4.99)
  {
    runLightState = 0;
    BrakeState = 0;
    clearStrip();
    leftTurn();
    //delay(10);
  }
  else if (RightVoltage > 4.99 )
  {
    // If we have voltage clear the strip from 'run light state' start Larson scan to the right.
    runLightState = 0;
    BrakeState = 0;
    clearStrip();
    rightTurn();
    //delay(10);
  }
  else if (BrakeVoltage > 4.2)
  {
    runLightState = 0;
    BrakeState = 0;
    clearStrip();
    BrakeLight();
  }
  else if (RightVoltage < 3.0 && LeftVoltage < 3.0 && BrakeVoltage < 3.0) {
    // If there's < 3v (capasitor drain) revert to run light state.
    runLight();
  }

  //delay(200);
}

/**
   Larson scan for right turn.
*/
void rightTurn()
{
  strip.setBrightness(BRIGHTNESS);

  // Turn strip on.
  for (uint16_t i = NUM_PIXELS / 2; i < NUM_PIXELS; i++) {
    strip.setPixelColor(i, AMBER);
    strip.show();
    delay(80);
  }

  //delay(1);

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

  //delay(150);
}

/**
   Larson scan for Left turn.
*/
void leftTurn() {
  strip.setBrightness(BRIGHTNESS);

  for (int16_t i = (NUM_PIXELS - 1) / 2 ; i > -1; i--) {
    strip.setPixelColor(i, AMBER);
    strip.show();
    delay(10);
  }

  delay(300);

  for (int16_t i = NUM_PIXELS / 2; i > -1; i--) {
    strip.setPixelColor(i, OFF);
    strip.show();
    delay(10);
  }

  delay(150);
}

/**
   Helper function to make all LEDs color (c)
*/
void all(uint32_t c)
{
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
}

/**
   Nothing is goign on, red 'run' state.
*/
void runLight()
{
  if (runLightState == 0)
  {
    strip.setBrightness(BRIGHTNESS / 12);
    spread(WIPE_SPEED, RED);
    runLightState = 1;
    all(RED);
    strip.show();
  }
}

/**
   We're in stop mode turn brake light on.
*/
void BrakeLight()
{
  if (BrakeState == 0)
  {
    strip.setBrightness(BRIGHTNESS + 25);
    spread(WIPE_SPEED, RED);
    BrakeState = 1;
    all(RED);
    strip.show();
  }
}

// Function for larson scan.
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color)
{
  uint32_t old_val[NUM_PIXELS]; // up to 256 lights!

  for (int i = 0; i < cycles; i++) {
    for (int count = 1; count < NUM_PIXELS; count++) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x > 0; x--) {
        old_val[x - 1] = dimColor(old_val[x - 1], width);
        strip.setPixelColor(x - 1, old_val[x - 1]);
      }
      strip.show();
      delay(speed);
    }

    for (int count = NUM_PIXELS - 1; count >= 0; count--) {
      strip.setPixelColor(count, color);
      old_val[count] = color;
      for (int x = count; x <= NUM_PIXELS ; x++) {
        old_val[x - 1] = dimColor(old_val[x - 1], width);
        strip.setPixelColor(x + 1, old_val[x + 1]);
      }
      strip.show();
      delay(speed);
    }

  }
}

// Set all LEDs to off/black.
void clearStrip()
{
  for (int i = 0; i < NUM_PIXELS; i++)
  {
    strip.setPixelColor(i, 0x000000);
    strip.show();
  }
}

// Dim a color across a width of leds.
uint32_t dimColor(uint32_t color, uint8_t width)
{
  return (((color & 0xFF0000) / width) & 0xFF0000) + (((color & 0x00FF00) / width) & 0x00FF00) + (((color & 0x0000FF) / width) & 0x0000FF);
}

// Function to light LEDS from the center one at a time (spreading).
void spread(uint16_t speed, uint32_t color)
{
  clearStrip();
  delay(300);
  int center = NUM_PIXELS / 2;
  for (int x = 0; x < center; x++)
  {
    strip.setPixelColor(center + x, color);
    strip.setPixelColor(center + (x * -1), color);
    strip.show();
    delay(speed);
  }
}

Do you have a question?

TheMemberFormerlyKnownAsAWOL: Do you have a question?

I have this program that works 95% but need help with the code a bit this is a turn signal and brake light and running lights. the following scenario I am trying to achieve this how????

So, how does it work now? (“95%” doesn’t really cut it)

TheMemberFormerlyKnownAsAWOL: So, how does it work now? ("95%" doesn't really cut it)

everything works apart from the override of brake to turn signal and vice versa

destiny2008:
everything works apart from the override of brake to turn signal and vice versa

And what does that mean, please?
What does that describe, in terms of how the code behaves now, and how you want it to behave?

TheMemberFormerlyKnownAsAWOL: And what does that mean, please? What does that describe, in terms of how the code behaves now, and how you want it to behave?

turn L works turn R works brake works tail works

brake + turn no go - only the first activated works I need them to co work together split pixels.

clear?

destiny2008: turn L works turn R works brake works tail works

brake + turn no go - only the first activated works I need them to co work together split pixels.

clear?

Yes.

That's the way your sequences work because you are using delay for the timing.

Just don't.

If you want your brake light to override everything, you can not use if().. elseif().. elseif() statements in your main loop. That logic only give you 1 function (left turn OR right turn OR brake) but it sounds like you may want both. If the Brake can be combined with a turn signal, it must be it's own stand-alone if() statement.

AND.... you will probably need to do more of a state machine type approach (search for FSM. Finite State Machine)

blh64: If you want your brake light to override everything, you can not use if().. elseif().. elseif() statements in your main loop. That logic only give you 1 function (left turn OR right turn OR brake) but it sounds like you may want both. If the Brake can be combined with a turn signal, it must be it's own stand-alone if() statement.

AND.... you will probably need to do more of a state machine type approach (search for FSM. Finite State Machine)

I am sure there is something much simpler... case /break ?

destiny2008: I am sure there is something much simpler... case /break ?

Not if you want your turn signals to flash like they do now. On for 300ms and then off. Repeat next time through loop(). Those are states that need to exist without the delay() if a brake light is also going to be ON/OFF. You could certainly do it with a switch statement, but thinking more like a state machine should make the code more straight forward (or maybe that's just me)

blh64: Not if you want your turn signals to flash like they do now. On for 300ms and then off. Repeat next time through loop(). Those are states that need to exist without the delay() if a brake light is also going to be ON/OFF. You could certainly do it with a switch statement, but thinking more like a state machine should make the code more straight forward (or maybe that's just me)

are you familiar on how to start with fsm?

destiny2008: are you familiar on how to start with fsm?

Google

similar to this behaviour https://www.youtube.com/watch?v=k-R9h7aB3q8&feature=youtu.be

blh64: Google

i asked are YOU?!

destiny2008:
i asked are YOU?!

Yes, I am familiar with how FSMs work. I have used them many times in multiple projects. There is a FSM library available. I personally did not care for it and implemented my own. And yes, several of the functions that did the update use switch() statements. That doesn’t mean I need to go into a large, detailed post about how to do it when simply searching Google will give you tons of hits and code and videos. Much more information than I could give.