How do i get my if to repeat untill another if value is entered.

I am a nob for starters, i have got pretty far with just youtube and google. But ive hit a hard stop. For starters my project is an amber strobe light bar for a tow truck. Im 100% sure of legality. Ive used 10w amber led cobs and 510 irf mosfets to control them from the arduino. I am also using a 6 button resistive dropper on the analog input 0 all my lights are going to be on digital 2-13 outs. I have got the lights bling in the patterns i am happy with but my change later. I have also got the push buttons to work swapping between each pattern. However i have to hold the button down to get pattern to repeat. How do the patter to repeat after one press of each corresponding button. Thank you all in advanced.

amber_strobes2.0.ino (10.2 KB)


Thank you for the quick response. I have tryied using the interupts its requisite is to use a digital pin pre determined. All of my digital pins are being used. Might you have another option. Thanks again.

  int val = analogRead(sensePin);

  if (val == 538)  {

Analogue values wobble notoriously; don't use equality comparisons like this.

The rest of your code is too long to read, and get rid of the delays. (Google: arrays)

  else(val == 0); What is that?


This, and the others like it, doesn't make sense because it does nothing:

else(val == 0);


I strongly suspect that you are faced with a huge re-write of your program to remove all the delay()s as they are almost certainly the reason why your buttons don’t react immediately - you have to hold the button until the current sequence ends and the code gets back to line 9.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

However, it should not be necessary to write your code in such a long-winded fashion if you learn about using arrays to hold the pin numbers for the LEDs.

I also think you will need to separate out the detection of the switches from the code that causes the flashes. In other words when a switch is pressed it causes a variable to take a value. If that value changes then a different sequence should start. And to keep the code short, when a new sequence is selected by a switch there will need to be some initial setting of values to make the sequence work. With suitable values the real work of flashing the LEDs in the first sequence could be dealt with something like these few lines

       for (byte n = 0; n < 3; n++) {
            digitalWrite(ledPin[n + ledPinOffset], ledState);

My idea with the variable ledPinOffset is to be able to select the first three LEDs or the subsequent three.

You will need a variable to count the flashes and change the value of ledPinOffset at the appropriate time.

Finally (for now) it is probably not a good idea to expect an analog input to give an exact value such as 538. A test such as

if (val >= 538)  {

would be much safer. (of course I may have the logic wrong, but you should get the idea)


Thank you all so much for the suggestions. I see what you are saying about the delays but that is not really the issue. I want the pattern to continue even after releasing the button. I dont mind waiting for the pattern to end to switch. And yes the else bits dont make sense i was just testing out and forgot to delete them they do absolutely nothing. Also please keep in mind a am a noob... some of these things yall are saying dont make any sense to me. The reason my code is so long is thats the only way i know to do it. It seems to work fine. If i cannot figure it out in code i will just put a switch instead of the button. Also referring to the person sayng they dont like the ( == ) i haven't had any issues with that. my resistor dropper holds very well. Thanks again yall!

I see what you are saying about the delays but that is not really the issue

If you think that the delays are not the issue, you really don't understand the problem.
You rely on one analogRead, but that only occurs when one cycle of whatever animation is current has completed.

:o You are very much correct. Im not looking at the code as whole. Like i said i am a noob. Thank you you pointing that out. I am trying to understand arrays, and how millis() will help my situations.

Also referring to the person sayng they dont like the ( == ) i haven't had any issues with that. my resistor dropper holds very well

A non sequitur if ever there was one.
Look up the difference between = and ==


You reset "val" each time through loop() and you flash the lights based on that variable. Since you are not holding the button down val turns to zero (I guess) and of course the flashing code does not execute.

You need to separate which animation should be playing from the button press, so if there is no button press the animation does not change and keeps playing.

Here's some code to consider:

int animationNumber = 0;

void loop () {

  int val = analogRead(sensePin);

  // set animation based on button
  if ( val >= 500 )
    animationNumber = 1;

  } else if ( val >= 300 ) {

    animationNumber = 2;

  } else if ( val >= 250 ) {

    animationNumber = 3;

  } else if ( val >= 160 ) {

    animationNumber = 4;

  } else if ( val >= 140 ) {

    animationNumber = 5;

  } // else

  switch ( animationNumber )
    case 1:
      // move val == 538 code here

    case 2:
      // move if (val == 318) code here

    // add other cases here


  } // switch

I think this will act as you want.

Already good advice in this thread on proceeding further, mine is to separate each animation into it's own function, giving each a descriptive name. then call those functions in the switch statement. Pulling the code out of loop() will make it easier to read and change.

Also referring to the person sayng they dont like the ( == ) i haven’t had any issues with that. my resistor dropper holds very well. Thanks again yall!

Sure, sitting comfortably by your PC in your nice warm house. Outside, in varying temperatures, surrounded by and fed with an automotive power system (think EMI)? Maybe not so much. Take some good advice and test for a value in a range. Something like 530 <= analogVal <= 546. This will give the code some tolerance for component/voltage value shifts due to temperature.

Study this state machine tutorial by LarryD. It illustrates many techniques applicable to your project.

I feel like i have bit off more than i can chew. Im just going to use switches. Thank you all for your help. if some one wants to write to code for me id be glad to pay haha. just not too much.

If you want to pay someone to write a program for you please ask in the Gigs and Collaborations section of the Forum.