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