Continue to Site

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.

  • 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.

led sequencer without delay help

Status
Not open for further replies.

adown

Member
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
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:
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.
 
The reason they don't switch off as expected it the "switch on" is always executed... You need a "Brakes on buffer"

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
 
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
 
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...
 
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:
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);
 
  }
 
}
 
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:
Hi adown,

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

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...

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.
 
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..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top