Need help getting started

I need a little nudge in the right direction.

I’m writing a program to flash a brake light for 4 seconds. The flash rate will increase until it goes steady ON. The flash rate sequence is simple and I have it working.

Brakes applied----light flash sequence begins, 4 seconds to steady ON.

Brakes released–light goes OFF

How can I interrupt the sequence within the first 4 seconds and go back to start (OFF) if the brakes are released before the 4 second string ends ?

Eos:
I need a little nudge in the right direction.

I’m writing a program to flash a brake light for 4 seconds. The flash rate will increase until it goes steady ON. The flash rate sequence is simple and I have it working.

Brakes applied----light flash sequence begins, 4 seconds to steady ON.

Brakes released–light goes OFF

How can I interrupt the sequence within the first 4 seconds and go back to start (OFF) if the brakes are released before the 4 second string ends ?

How are you determining the state of the brake pedal ? Are you polling/reading the pin or is it tied to an interrupt ? Since you've not posted your code, I'll have to guess you're using the delay() function. If so there are better ways to time the ramping. Can't say more w/o looking at your code.

Basically you want to break your timing up into intervals that are equal to the response time you need. i.e., if you have a 4 second delay and want to respond to the interrupt in .1 second, then you will need 40 x 0.1 second delays with an input check between each one.

If you still have my email address, send me the segment of code and I can fix it for you.

e.g.

for (int i = 0; i < 40; i++)
{
    // Read pushbutton to interrupt sequence
    if (digitalRead(INPUT_PIN) == 0)
    {
        break;
    }
    else
    {
        // Assuming this part of the sequence executes in less than 100 ms
        doFlashSequence();
    }
    delay(100);
}

The delay will have to be tweaked a bit depending on how long doFlashSequence() takes.

This is what I have so far. It’s a free running loop used to visualize and adjust the flash pattern. Output is set to pin 1 for Trinket.

int led = 1;

void setup() {

pinMode(led, OUTPUT);

}

void loop() {

digitalWrite(led, HIGH);

delay(15);

digitalWrite(led, LOW);

delay(200);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(200);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(50);

digitalWrite(led,HIGH);

delay(15);

digitalWrite(led,LOW);

delay(15);

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(10);

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(10);

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(10);

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(10);

digitalWrite(led,HIGH);

delay(50);

digitalWrite(led,LOW);

delay(10);

digitalWrite(led,HIGH);

delay(2500);

digitalWrite(led,LOW);

delay(2500);

}

Try this. Not tested, but it illustrates the concept.

const int led = 1;
const int switch = 2;

void setup() 
{ 
    pinMode(led, OUTPUT); 
    pinMode(switch, INPUT_PULLUP);
}

void loop()
{
    // Fill this array with your delays (this is a more compact version of what you have in your post)
    int blinks[] = {15,200,15,200,15,50,15,50};
    // Get number of elements
    int num = sizeof(blinks)/sizeof(blinks[0]);

    for (int i= 0; i < num/2; i+=2)
    {
        digitalWrite(led, HIGH);
        if (pollingDelay(blinks[i]))
            break;
        digitalWrite(led, LOW);
        if (pollingDelay(blinks[i+1]))
            break;
    }
}

///
/// Delays for parameter 1. Returns true if switch pressed
/// false otherwise
///
bool pollingDelay(int t)
{
    for (int i=0; i < t; i++)
    {
        if (digitalRead(switch) == LOW)
            return true;
        delay(1);
    }
    return false;
}

You can also do it with AttachInterrupt, but that limits the pins you can use for the pushbutton.

@ the OP;

Hmmm, about the timings you have in your post. Can you explain the reasoning behind them ? My 1’st guess at this would have been to have a min ON time (enough to be seen as on) followed by a variable OFF time. The OFF time then decreasing until it’s zero and the brake light is on 100% of the time.

And to illustrate a concept (pseudo-code) …

void loop() {
  // read the brake pedal switch
  if (digitalRead(BrakePin) == ACTIVATED)
  {
    doFlashSequence();
  }
  else
  {
    // brake is not activated, reset time, turn off LEDs
    BrakeTime = millis();
    LEDs = off;
  }
}

… then the timing of on/off for the LEDs is derived from the BrakeTime variable inside of doFlashSequence(). You might want to add some limited form of switch debouncing, but the general idea holds true.