Hello All
My wife bought an electric bike and I want to put tail and brake lights on it.
I have 2 x Neopixels wheel leds for the tail lights doing the "Classic rainbow theater"
and the brake lights have a MPU 6050 measuring the G Force.
Everything works up to here.
The brake lights have 5 levels of leds depending on the G Force
When the G Force reaches a certain value that level of leds switches on,
but the leds go off as soon as it falls below the value. To quick to be seen.
I need to slow the on time so it can be seen using the "flash without delay"
and I also need to delay the start time to stop the leds coming on because of bumps
using the "debounce"
I have tried both sketches and still no joy.
Later on I want to flash the Neopixels at level 3 to red and use the turn signal to flash them amber.
I can not use the delay() because of the Neopixel wheels
const int BrakeLight_1_ledPin = 3; // Level 1 lights
const int BrakeLight_2_ledPin = 4; // Level 2 lights
const int BrakeLight_3_ledPin = 5; // Level 3 lights
const int BrakeLight_4_ledPin = 6; // Level 4 lights
const int BrakeLight_5_ledPin = 7; // Level 5 lights
unsigned long previousMillis = 0;
boolean BrakeLight_1_ledState = false;
boolean BrakeLight_2_ledState = false;
boolean BrakeLight_3_ledState = false;
boolean BrakeLight_4_ledState = false;
boolean BrakeLight_5_ledState = false;
int BrakeLight_1_buttonState = LOW;
int BrakeLight_2_buttonState = LOW;
int BrakeLight_3_buttonState = LOW;
int BrakeLight_4_buttonState = LOW;
int BrakeLight_5_buttonState = LOW;
long OnTime = 250; // milliseconds of on-time
long OffTime = 750; // milliseconds of off-time
unsigned int G_Force;
void setup ()
{
pinMode(BrakeLight_1_ledPin, OUTPUT);
pinMode(BrakeLight_2_ledPin, OUTPUT);
pinMode(BrakeLight_3_ledPin, OUTPUT);
pinMode(BrakeLight_4_ledPin, OUTPUT);
pinMode(BrakeLight_5_ledPin, OUTPUT);
}
void loop ()
{
unsigned long currentMillis = millis();
int BrakeSwitchVal = (G_Force); //read and store brake switch status
// I have mapped the G_Force from the MPU 6050 from 0 to 100
if((BrakeSwitchVal >= 20) && (BrakeLight_1_ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
BrakeLight_1_ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(BrakeLight_1_ledPin, BrakeLight_1_ledState); // Update the actual LED
}
else if ((BrakeLight_1_ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
BrakeLight_1_ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(BrakeLight_1_ledPin, BrakeLight_1_ledState); // Update the actual LED
}
Serial.println (BrakeLight_1_ledState);
}
Thanks.
I'll put the finished product on you tube and thingiverse
I think I have it sorted
try:
///////////// Beginning of Brake light stuff
// Pin 0 and 1 are Tx & Rx (Pin 2 is for the accel chip "INT")
const byte BrakeLight_1_ledPin = 3; // Level 1 lights (or ledPin)
const byte BrakeLight_2_ledPin = 4; // Level 2 lights
const byte BrakeLight_3_ledPin = 5; // Level 3 lights
const byte BrakeLight_4_ledPin = 6; // Level 4 lights
const byte BrakeLight_5_ledPin = 7; // Level 5 lights
unsigned long buttonPushedMillis;
unsigned long ledTurnedOnAt;
unsigned long turnOnDelay = 200; // wait to turn on LED
unsigned long turnOffDelay = 1000; // turn off LED after this time
bool BrakeLight_1_ledReady = false; // flag for when button is let go
bool BrakeLight_2_ledReady = false;
bool BrakeLight_3_ledReady = false;
bool BrakeLight_4_ledReady = false;
bool BrakeLight_5_ledReady = false;
bool BrakeLight_1_ledState = false; // for LED is on or not.
bool BrakeLight_2_ledState = false;
bool BrakeLight_3_ledState = false;
bool BrakeLight_4_ledState = false;
bool BrakeLight_5_ledState = false;
////////////////// End of Brake light stuff
void setup ()
{
//////////Start of Brake light stuff
pinMode(BrakeLight_1_ledPin, OUTPUT);
pinMode(BrakeLight_2_ledPin, OUTPUT);
pinMode(BrakeLight_3_ledPin, OUTPUT);
pinMode(BrakeLight_4_ledPin, OUTPUT);
pinMode(BrakeLight_5_ledPin, OUTPUT);
digitalWrite(BrakeLight_1_ledPin, LOW);
digitalWrite(BrakeLight_2_ledPin, LOW);
digitalWrite(BrakeLight_3_ledPin, LOW);
digitalWrite(BrakeLight_4_ledPin, LOW);
digitalWrite(BrakeLight_5_ledPin, LOW);
////////// End of Brake light stuff
}
void loop()
{
//Brake Light Code////////////////////////////////////////////////////////////////////////////////////////
unsigned int G_Force; // I have the G Force from the MPU 6050 mapped into 5 lots of 20
int BrakeSwitchVal = (G_Force); //read and store brake switch value
unsigned long currentMillis = millis();
if (BrakeSwitchVal >= 19)
{
buttonPushedMillis = currentMillis;
BrakeLight_1_ledReady = true;
}
if (BrakeLight_1_ledReady)
{
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay)
{
digitalWrite(BrakeLight_1_ledPin, HIGH);
BrakeLight_1_ledState = true;
ledTurnedOnAt = currentMillis;
BrakeLight_1_ledReady = false;
}
}
if (BrakeLight_1_ledState)
{
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay)
{
BrakeLight_1_ledState = false;
digitalWrite(BrakeLight_1_ledPin, LOW);
}
}
Serial.print ( "Brake light 1 ");
Serial.println (BrakeLight_1_ledState);
// you can copy the other 4 brake levels in here
}