1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

led sequencer without delay help

Discussion in 'Arduino' started by adown, Jan 23, 2015.

  1. adown

    adown Member

    Joined:
    Jun 16, 2006
    Messages:
    61
    Likes:
    0
    Hi, I'm new to Arduino. I am trying to program a 3 LED sequencer like the old Cougar tail lights. I have a sketch that does what I want, but using the delay doesn't let the bulbs extinguish right away every time. I tried to format the blink without delay for what I need, but it just randomly flashes instead of sequencing. Can someone help me implement the mills() timer and remove the delay. The LED's are supposed to light up in sequence, staying lit until the final one lights, and then they all shut off. Or, if the input is still HIGH, they will stay lit. Thanks
    Code (c):

    const int brakesPin = 2;     // the number of the pushbutton pin
    int timer = 200;  // The higher the number, the slower the timing.

    // variables will change:
    int brakesState = 0;         // variable for reading the pushbutton status

    void setup() {
      // use a for loop to initialize each pin as an output:
      for (int thisPin = 10; thisPin < 13; thisPin++)  {
        pinMode(thisPin, OUTPUT);
        pinMode(brakesPin,INPUT);
      }
    }

    void loop() {
      // read the state of the pushbutton value:
      brakesState = digitalRead(brakesPin);

      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (brakesState == HIGH)
      // loop from the lowest pin to the highest:
      for (int thisPin = 10; thisPin < 13; thisPin++) {
        // turn the pin on:
        digitalWrite(thisPin, HIGH);
        delay(timer);                
      }
      else // turn the pin off:
      for (int thisPin = 10; thisPin < 13; thisPin++)
      {
        digitalWrite(thisPin, LOW);
      }
    }
     
     
    Last edited: Jan 24, 2015
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    I have written this on a pic.... The code works well.... BUT!!! do you have three LED's because the for statement only executes twice.
     
  3. adown

    adown Member

    Joined:
    Jun 16, 2006
    Messages:
    61
    Likes:
    0
    Oops, yes, the 12 should be a 13. I changed it in the final sketch after figuring that out. My problem is, sometimes when I take the input low, the LED's instantly extinguish, and sometimes they stay lit for a second longer. I have a 10K resistor to gnd on the input, but I think it's got to do with the delay. I need it to extinguish instantly every time.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK

    The reason they don't switch off as expected it the "switch on" is always executed... You need a "Brakes on buffer"

    Code (pseudo code):

    brakeson = switch

    if brakeson not equal brakesonbuffer
       {
       if brakeson
              do brakesonstuff
       else
             do brakesoffstuff
       brakesonbuffer equals  brakeson
       }
     
    Now the LED's will only change if the switch changes
     
  6. adown

    adown Member

    Joined:
    Jun 16, 2006
    Messages:
    61
    Likes:
    0
    Hi Ian, I'm not sure we're on thew same page. When I take brakePin HIGH, I do get the brakes sequence. When I take the brakesPin LOW, they shut off. The problem is, sometimes there is a delay to shut off, and I think it is because I am using a delay(timer). I can't figure out how to get the sequence with the millis() counter to work with 3 LED's. Thanks
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    The way your code works:-

    Read switch
    if switch on
    light leds in sequence ( with delays )​
    else
    clear led's ( no delays )​


    So if you turn off your brake lights as you are entering the switch on branch of code... The LED's wont shut off until the on sequence has finished.... Three 200mS delays = 600mS so potentially you could see up to 600mS delay after turning off your brake lights.... If you use a buffer... It will be instant as you are not writing to the port every cycle...
     
  8. adown

    adown Member

    Joined:
    Jun 16, 2006
    Messages:
    61
    Likes:
    0
    I added the brakeson buffer although I'm still trying to understand it. It hit me while I was writing this. OK, I did a "int brakesobbuffer = brakesState" before setup, where it looked like Ian had it in void loop(). It didn't like it in the loop, because it wasnt declared in the scope, but I am sure I am close. Is it correct?

    Code (text):
    const int brakesPin = 2;  // the number of the pushbutton pin
     
    int timer = 200;  // The higher the number, the slower the timing.
     
    int brakesState = 0;  // variable for reading the pushbutton status
     
    int brakesonbuffer = brakesState; //Initialize brakes switch buffer
     
    void setup() {
     
      // use a for loop to initialize each pin as an output:
     
      for (int thisPin = 10; thisPin < 13; thisPin++)  {
     
      pinMode(thisPin, OUTPUT);
     
      pinMode(brakesPin,INPUT);
     
      }
     
    }
     
    void loop() {
     
      // read the state of the pushbutton value:
     
      brakesState = digitalRead(brakesPin);
     
      if (brakesState != brakesonbuffer) {
     
      // check if the pushbutton is pressed.
     
      // if it is, the buttonState is HIGH:
     
      if (brakesState == HIGH)
     
      // loop from the lowest pin to the highest:
     
      for (int thisPin = 10; thisPin < 13; thisPin++) {
     
      // turn the pin on:
     
      digitalWrite(thisPin, HIGH);
     
      delay(timer);
     
      }
     
      }
     
      else // turn the pin off:
     
      for (int thisPin = 10; thisPin < 13; thisPin++) {
     
      digitalWrite(thisPin, LOW);
     
      }
     
    }
     
  9. adown

    adown Member

    Joined:
    Jun 16, 2006
    Messages:
    61
    Likes:
    0
    Anyone??
     
  10. moty22

    moty22 Member

    Joined:
    Jan 31, 2015
    Messages:
    171
    Likes:
    18
    I don't have any experience with Arduino. If you are using one input then maybe taking the input setup out of the loop will help, it could be confusing the compiler.

    void setup() {
    // use a for loop to initialize each pin as an output:
    for (int thisPin = 10; thisPin < 13; thisPin++) {
    pinMode(thisPin, OUTPUT);

    }
    pinMode(brakesPin,INPUT);
    }
     
    Last edited: Feb 15, 2015
  11. cnicholas63

    cnicholas63 New Member

    Joined:
    Feb 16, 2015
    Messages:
    1
    Likes:
    0
    Hi adown,

    Did you sort this? Ian is right in his answer:

    But if you are not happy with the buffer solution, you could just re-test the state of the breaks pin inside the loop and break from the loop if the pin is low.

    void loop(){// read the state of the pushbutton value:
    brakesState = digitalRead(brakesPin);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if(brakesState == HIGH)
    // loop from the lowest pin to the highest:
    for(int thisPin =10; thisPin <13; thisPin++){
    if(digitalRead(brakesPin) == LOW)
    break;
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    }
    else// turn the pin off:
    for(int thisPin =10; thisPin <13; thisPin++)
    {
    digitalWrite(thisPin, LOW);
    }
    }

    This should break from the loop as soon as you release the button.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    I have been looking at this... The BrakeState and Brakesonbuffer are of int types... When using boolean values they may not correspond....

    I need to try this on real hardware, but I haven't got time.... Maybe someone with a simulator can check it's operation..
     

Share This Page