Making leds stay on while in a loop

I’m trying to make my brake lights flash on my car 3 times and then go solid and stay solid when the arduino detects a higher voltage through a voltage sensor. I have all the hardware/wiring figured out. I am planning on using a TIP122 transistor to make the leds flash when the higher voltage is detected (testing right now with one 5mm led on a breadboard). I just can’t figure out how to make the led stay on after flashing until the measured voltage drops (since I’m using an “if” loop). It just wants to keep flashing. See code attached. I’m a beginner btw.

Blinking_Brake_Light.ino (643 Bytes)

Welcome.

Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images: https://forum.arduino.cc/index.php?topic=519037.0

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png

Thanks.. Tom... :slight_smile:

you can just add a counter to count times it went high

int space = 50;
int counter ;  //   creates a counter for use to count repetitions
float R1 = 30000;
float R2 = 7500;



void setup() {

counter = 0 ;

 
}

void loop() {

  int sensorValue = analogRead(A0);
  float voltage = (5.0*sensorValue*(R1+R2))/(1023.0*R2);
  Serial.println(voltage);


if (counter == 0 ){
  if (sensorValue > 6){
    digitalWrite(2, HIGH);
    delay(space);
    counter ++ // add's a 1 to the counter

    digitalWrite(2, LOW);
    delay(space);

    digitalWrite(2, HIGH);
    delay(space);
    counter ++ // add's a 1 to the counter
      
    digitalWrite(2, LOW);
    delay(space);


    digitalWrite(2, HIGH);
    delay(space);
    counter ++ // add's a 1 to the counter
    
    digitalWrite(2, LOW);
    delay(space);

    if (counter >= 3){
       digitalWrite(2, HIGH);
    } // end of second if counter
 
}   // end of first if counter
}  // end of if sensorvalue
       
  else{ 
    if (sensorValue < 6)
    digitalWrite(2, LOW);
    counter = 0 ;  //   re-sets the counter
  }  //  end of else if


}  // end of loop

Since you are just starting, you can see how this counter would offer a way to count the number of times that you had sent your pulse.

As a note, the sketch has a potential flaw in that delay() is a blocking function and nothing else can be done while it is timing.

As your needs change and your knowledge grows, read about blink-without-delay
It uses the internal timer to say, what time is it now… and has 1 second passed yet… so you can do thousands of things while waiting.

The other thing that is more closely related to your exact need is a for-loop

A simple way to run a thing x times

This is what you are really looking for on this.
Run your for loop and it will blink the three times
Then once that for() loop is done, you can then stay on with something like the if() shown.

But since you are learning, try it this way first and see if that does what you want.

The added if() is nested inside of your other if()

btw, THANK YOU for posing your code in such a way as to not just dump it in your post.
as you can see by my post, I embedded it into the post with a thing we call code tags.
read how to use this forum, it is a sticky post at the top of every forum and item #7 shows how to use code tags.

this is your same program, but since counter will count the number of times, you do not need separate pulses

[pre][code]

int space = 50;
int counter ;  //   creates a counter for use to count repetitions
float R1 = 30000;
float R2 = 7500;



void setup() {

counter = 0 ;

 
}

void loop() {

  int sensorValue = analogRead(A0);
  float voltage = (5.0*sensorValue*(R1+R2))/(1023.0*R2);
  Serial.println(voltage);


if (counter <= 2 ){  // only passes if the counter is less than 3
  if (sensorValue > 6){   // only passed if the value if over 6
    digitalWrite(2, HIGH);
    delay(space);
    counter ++ // add's a 1 to the counter

    digitalWrite(2, LOW);
    delay(space);

    if (counter >= 3){
       digitalWrite(2, HIGH);
     } // end of second if counter
  }   // end of first if counter
}  // end of if sensorvalue
       
  else{ 
    if (sensorValue < 6)
    digitalWrite(2, LOW);
    counter = 0 ;  //   re-sets the counter
  }  //  end of else if

}  // end of loop

[/pre]

[/code]

This is a nice idea, but it's going to annoy the hell out of the poor guy behind you in stop-and-go traffic. :slight_smile:

The fix would be to add an accelerometer and flash the brake lights when the deceleration rate exceeds some threshold. Now THAT would be cool.

johnwasser:
This is a nice idea, but it's going to annoy the hell out of the poor guy behind you in stop-and-go traffic. :slight_smile:

The 3 flash thing is not too-too bad.
What is annoying is the motorcycle break lights the only flash.

I think the 3 flashed in less than a second is going to get some attention.
as for stop and go. add a timer so they will not flash unless the brake has not been touched for more than a minute.

ehat06, you have some other things that are not working in your sketch.
you have nothing in set-up to call for serial.

also, you have taken the analog and turned it into a float, but for a simple on/off switch, there is no need for analog.

another way is that your 3 pulse if can be done without the counter I showed in the first section.

since you sent out the 3 pulses, at the end of that if() you could add LiteON=1;
and put

if (LiteON==0) {

before your if() so that only if the LiteON is 0 will it look for the switch

of course, in your else, you would make LiteON = 0

that way, you get your 3 pulses, then the LiteON flag changes and you do not try to blink again until your signal has gone to less than 6.

johnwasser:
The fix would be to add an accelerometer and flash the brake lights when the deceleration rate exceeds some threshold. Now THAT would be cool.

And illegal. At least here.