(Solved) Brake lights using a MPU 6050

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
}

Hi.

Apart from the long and numbered names for your variables, that snippet looks OK to me.
The blink without delay() sketch, also teaches you to keep track of what you are and what you have been doing.
As far as i can see, you are keeping an eye on time.
But you should also keep an eye on steps taken.
Thus far, you are just looking at whether your LED is (supposed to be) on or not.

If you want to do a debounce, you need to check a buttonState, or whatever signal you are looking at, multiple times.
If you're not looking at an on/off state, like your G Force sensor, you should check whether it is within a set range instead of a fixed value.

Now, let's assume you are within your threshold to start the debouncing.
This would be the time to do something with a global boolean, i'll call it debounceStart.
This would now be set to True.
In case it need to be reset, you'd set it to False of course.

I'll leave it up to you how to do that exactly.

If you've seen the threshold twice, you could assume you have performed a good debounce and do the action you want to do.
If seeing it twice isn't enough, you need to create something else than a boolean, and start counting.

I'm sure with these hints you can do what you like to do here.
Have fun coding.

The best way to handle bumps and vibration is to low-pass-filter the accelerometer signal.

If you are using Jeff Rowberg's library then you can invoke the 6050's internal digital signal processor to perform filtering down to 5Hz, e'g':

 // Options: BW_256 BW_188 BW_98 BW_42 BW_20 BW_10 BW_5 Hz
  imu.setDLPFMode(MPU6050_DLPF_BW_20);

This will reduce the higher noise frequencies.

You can then sample the acceleration and use a "running median" library, a median automatically throws away wildly wrong values for you, which an averaging filter would not.

After that it is down to setting the decelleration thresholds and a timer for the light.

If you like maths then you could look at using a Kalman Filter as that works well in the presence of noise.

Hi GerryHamilton.

Please don't edit your first post with the solution you found, after you have had replies (unless someone has told you to do so for a good reason).
Put a reply at the end of a thread instead.
It's very hard to determine what you changed at what point (only the time of your last change is shown), and it is confusing.
Bodmer's reply was made after your change.

It is OK to change the title of the 1st post the way you did it.

OK Thanks