R/C police car LED programming help please

Hello all,

Please forgive any newbie mistakes i make as i am a first time poster and relatively new to the whole arduino coding world.

My problem is that I am trying to create a realistic flash pattern for an R/C police car and I can get the lights to flash perfect each on their own routine, but i cannot combine 2 routines run simultaneously.

I am trying to make the usual blue and red lights strobe back and forth WHILE also having a traffic arrow of 8 LEDs move to the left. I cannot figure out the code to make this work. I understand i cannot use DELAY and that i should be using MILLIS() but i am having trouble grasping how to code a “4 quick blink” type of strobe light with millis(). And same thing with the traffic arrow. The code below works perfectly but all it will do is show you what i want to achieve, i am perplexed on how to write this same code in a way that i can have the strobes flashing while the arrow moves across… :astonished:

Physical setup looks like this:
B lue LED
R ed LED
Y ellow LED

R R R Y Y Y Y Y Y Y Y B B B

i left out some of the other code required to make this work, but i did get it to work ok, the code itself is not the problem, its how to rewrite it that is making me nuts. Thanks so much for any insight ! :grin:


```
// R/C police car rear yellow traffic arrow, moving
// from right to left with a double blink on the final led

void ta_left(){       

    int delayTime = 150;
  for(int i = 7; i >= 0; i--){
    changeLED(i,OFF); 
  }
  for(int i = 7; i >= 0; i--){
    changeLED(i,ON);            
    delay(delayTime);
  }

  delay(delayTime);
  changeLED(0,OFF);
  delay(delayTime);
  changeLED(0,ON);
  delay(delayTime);
  changeLED(0,OFF);
  delay(delayTime);
  changeLED(0,ON);
  delay(500);
}

// R/C police car strobe blue and red lights, 4 quick blinks one side,
//then 4 quick blinks the other side (back and forth)
void strobes (){

for(int x=0;x<4; x++){

digitalWrite(6, HIGH) ;
   digitalWrite(7, LOW) ;
   delay(25);
   digitalWrite(6, LOW) ;
   digitalWrite(7, LOW) ;
   delay(60);
 }

for(int y=0;y<4; y++){

digitalWrite(6, LOW) ;
   digitalWrite(7, HIGH) ;
   delay (25);
   digitalWrite(6, LOW) ;
   digitalWrite(7, LOW) ;
   delay(60);

}
}

Have you looked at the blink without delay example in the IDE? Does that make things clearer?

Please use code tags when posting code.

Thanks for reply,

I have looked at the tutorials like you have mentioned, but they instruct on 1 led blinking at a constant rate. I don't have enough experience to be able to adapt that code to my needs, it would be most helpful if someone could show me an example for an LED to blink quickly 4 times, then stop while the other side blinks, without using DELAY, so that after i get it working i can have my traffic arrow going simultaneously. Unfortunately I don't have a background in coding other than few examples i have done with the arduino.

Thanks!

but they instruct on 1 led blinking at a constant rate

With a very simple modification, you can change that to provide a brief flash. Try doing that, say, one 100 millisecond long flash every second. Hint: it involves the variable called "interval".

Post again if you get stuck.

Hi max_lumens, here is my suggestion for you.

When using delay () to time your flashes, the other thing you are doing, perhaps without realising, is using the current command in the program to keep track of where you are in the led sequence. To have several sequences running together, you not only have to get rid of delay () but also use another method of keeping track of where you are in each sequence.

This is what I suggest: your shortest delay is 25. Make all other delays into multiples of that, so 50, 75, 100, 250, 1000 etc whatever you need. Have a single delay of 25 at the start of your loop () function. Also make a new int variable called sequenceStep or something. Add one to this variable after the delay. Then, using only "if" statements (no for or while loops allowed) check for the step numbers where each led must be switched on or off. At the end of the sequence (5000 or whatever) one more "if" will be needed to set sequenceStep back to zero.

It will help to write each of your separate sequences out on paper or a spreadsheet. Remember all the "off" steps to go with each "on" step. Then go down each sequence and write the step number against each one, remembering each step is equivalent to a delay (25). Once you have them all worked out, each one bevomes a simple "if" statement switching the required led on or off.

Paul

Thanks so much! I will try all of your advice and report back :grin:

In my repository there is code for a sketch called MultiBlink that allows multiple effects with LEDs. The patterns are specified as data and generic code runs the lights. This may be useful for you application if you incorporate it into your sketch.