Page 1 of 1

Need help getting started

Posted: Sat Mar 07, 2015 8:20 pm
by 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 ?

Re: Need help getting started

Posted: Sat Mar 07, 2015 9:42 pm
by Mee_n_Mac
Eos wrote: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.

Re: Need help getting started

Posted: Sun Mar 08, 2015 4:58 pm
by lyndon
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.
Code: Select all
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.

Re: Need help getting started

Posted: Sun Mar 08, 2015 5:10 pm
by Eos
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);

}

Re: Need help getting started

Posted: Sun Mar 08, 2015 8:36 pm
by lyndon
Try this. Not tested, but it illustrates the concept.
Code: Select all
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.

Re: Need help getting started

Posted: Sun Mar 08, 2015 8:42 pm
by Mee_n_Mac
@ 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.

Re: Need help getting started

Posted: Sun Mar 08, 2015 9:36 pm
by Mee_n_Mac
And to illustrate a concept (pseudo-code) ...
Code: Select all
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.