Running Dual Loops

Hi, I have put together a double loop meshing the knight rider sequence and the Police light pattern. I have both of them working however, my police lights are slow in transition and I can’t figure out what I did wrong. Adjusting speed has no effect. Can you look and let me know where? Thanks, Leo

‘’’ // KNIGHT RIDER and POLICE LIGHTS

#define RED 0x0 // Which LED to Strobe
#define RED1 0x0
#define RED2 0x0
#define BLUE 0x1
#define BLUE1 0x1
#define BLUE2 0x1

byte whichLED = RED; // Variable to track which LED is on

// constants won’t change. Used here to set a pin number:
const int LED_Red = 3; // Where are the LEDs connected?
const int LED_Red1 = 4;
const int LED_Red2 = 5;
const int LED_Blue = 6;
const int LED_Blue1 = 7;
const int LED_Blue2 = 8;

byte Red_State = LOW; // State variables for the LEDs
byte Red1_State = LOW;
byte Red2_State = LOW;
byte Blue_State = LOW;
byte Blue1_State = LOW;
byte Blue2_State = LOW;

unsigned long switchDelay = 250; // delay values changing flashing behavior
unsigned long strobeDelay = 50;
unsigned long strobeWait = strobeDelay; // Seed initial wait for strobe effect
unsigned long waitUntilSwitch = switchDelay; // seed initial wait, switch LEDs
unsigned long previousMillis = 0; // will store last time LED was updated

// Variables will change:
int pinArray = {9, 10, 11, 12, 13};
int count = 0;
int timer = 50;

void setup() {
pinMode(LED_Red, OUTPUT);
pinMode(LED_Red1, OUTPUT);
pinMode(LED_Red2, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(LED_Blue1, OUTPUT);
pinMode(LED_Blue2, OUTPUT);

for (count = 0; count < 6; count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() { // KNIGHT RIDER
policeLights();
for (count = 0; count < 4; count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer * 2);
}

for (count = 4; count > 0; count–) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer * 2);
}
}

void policeLights() {

//unsigned long switchDelay = 550; // delay values changing flashing behavior
//unsigned long strobeDelay = 50;
//unsigned long strobeWait = strobeDelay; // Seed initial wait for strobe effect
//unsigned long waitUntilSwitch = switchDelay; // seed initial wait, switch LEDs

digitalWrite(LED_Red, Red_State); // each iteration of loop() sets the IO pins
digitalWrite(LED_Red1, Red1_State);
digitalWrite(LED_Red2, Red2_State);
digitalWrite(LED_Blue, Blue_State); // even if they don’t change, that’s okay
digitalWrite(LED_Blue1, Blue1_State);
digitalWrite(LED_Blue2, Blue2_State);

// Toggle back and forth between the two LEDs
if ((long)(millis() - waitUntilSwitch) >= 0) {
// time is up!
Red_State = LOW;
Red1_State = LOW;
Red2_State = LOW;
Blue_State = LOW;
Blue1_State = LOW;
Blue2_State = LOW;
whichLED = !whichLED; // toggle LED to strobe
waitUntilSwitch += switchDelay;
}

// Create the stobing effect
if ((long)(millis() - strobeWait) >= 0) {
if (whichLED == RED)
Red_State = !Red_State;
if (whichLED == RED1)
Red1_State = !Red1_State;
if (whichLED == RED2)
Red2_State = !Red2_State;
if (whichLED == BLUE)
Blue_State = !Blue_State;
if (whichLED == BLUE1)
Blue1_State = !Blue1_State;
if (whichLED == BLUE2)
Blue2_State = !Blue2_State;

strobeWait += strobeDelay;
}
}’’’

Can you post your code in code blocks? Thanks. dkw.

Hi, I am a bit new at this forum bit...can you describe what you mean by that..I am just a home tinkerer. Thanks.

Have a read through this. Pay particular attention to point no. 6.


#define RED 0x0 // Which LED to Strobe
#define RED1 0x0
#define RED2 0x0
#define BLUE 0x1
#define BLUE1 0x1
#define BLUE2 0x1

byte whichLED = RED; // Variable to track which LED is on

// constants won't change. Used here to set a pin number:
const int LED_Red = 3; // Where are the LEDs connected?
const int LED_Red1 = 4;
const int LED_Red2 = 5;
const int LED_Blue = 6;
const int LED_Blue1 = 7;
const int LED_Blue2 = 8;

byte Red_State = LOW; // State variables for the LEDs
byte Red1_State = LOW;
byte Red2_State = LOW;
byte Blue_State = LOW;
byte Blue1_State = LOW;
byte Blue2_State = LOW;

unsigned long switchDelay = 250; // delay values changing flashing behavior
unsigned long strobeDelay = 50;
unsigned long strobeWait = strobeDelay; // Seed initial wait for strobe effect
unsigned long waitUntilSwitch = switchDelay; // seed initial wait, switch LEDs
unsigned long previousMillis = 0; // will store last time LED was updated

// Variables will change:
int pinArray[] = {9, 10, 11, 12, 13};
int count = 0;
int timer = 50;

void setup() {
pinMode(LED_Red, OUTPUT);
pinMode(LED_Red1, OUTPUT);
pinMode(LED_Red2, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(LED_Blue1, OUTPUT);
pinMode(LED_Blue2, OUTPUT);

for (count = 0; count < 6; count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() { // KNIGHT RIDER
policeLights();
for (count = 0; count < 4; count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer * 2);
}

for (count = 4; count > 0; count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer * 2);
}
}

void policeLights() {

//unsigned long switchDelay = 550; // delay values changing flashing behavior
//unsigned long strobeDelay = 50;
//unsigned long strobeWait = strobeDelay; // Seed initial wait for strobe effect
//unsigned long waitUntilSwitch = switchDelay; // seed initial wait, switch LEDs

digitalWrite(LED_Red, Red_State); // each iteration of loop() sets the IO pins
digitalWrite(LED_Red1, Red1_State);
digitalWrite(LED_Red2, Red2_State);
digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay
digitalWrite(LED_Blue1, Blue1_State);
digitalWrite(LED_Blue2, Blue2_State);

// Toggle back and forth between the two LEDs
if ((long)(millis() - waitUntilSwitch) >= 0) {
// time is up!
Red_State = LOW;
Red1_State = LOW;
Red2_State = LOW;
Blue_State = LOW;
Blue1_State = LOW;
Blue2_State = LOW;
whichLED = !whichLED; // toggle LED to strobe
waitUntilSwitch += switchDelay;
}

// Create the stobing effect
if ((long)(millis() - strobeWait) >= 0) {
if (whichLED == RED)
Red_State = !Red_State;
if (whichLED == RED1)
Red1_State = !Red1_State;
if (whichLED == RED2)
Red2_State = !Red2_State;
if (whichLED == BLUE)
Blue_State = !Blue_State;
if (whichLED == BLUE1)
Blue1_State = !Blue1_State;
if (whichLED == BLUE2)
Blue2_State = !Blue2_State;

strobeWait += strobeDelay;
}
}````

Hopefully this is better...a little online searching.......thanks.

Looks like it is time for you to learn "Blink Without Delay" and get all the delay calls out of your code. You've made it beyond the point where delay is going to work for you.

Hi, this is a combination of the "Blink without Delay" code, "Knight Rider" and "Police Lights" I have found online. So, are you saying that the delay(timer) will not work? It should be replaced with the "millis"..thanks much

I am saying you need to forget you ever heard of delay. They've all got to go. Do it all the BWoD way.

Hmmm...what does Bandwidth on Demand got to do with it? At home learner, remember? Nevermind, I will figure it out especially after reading other forum posts. I didn't want u to write it for me but u pointed me to the moon, so off I go. Fits in with the other posts.

It's Blink Without Delay. I figured you would pick that up from the previous post. Sorry if I overestimated you.

At home learner, remember?

I didn't forget. Most folks here are. I just assumed a little bit of basic intelligence would lead to the ability to read that in context of the rest of the thread.

This is how to use code tags
Edit your post
before the code, type
** **[code]** **

after the code, type
** **[/code]** **

So it looks like

your code here

cowboybilliards:
Hi, I have put together a double loop meshing the knight rider sequence and the Police light pattern. I have both of them working however, my police lights are slow in transition and I can't figure out what I did wrong. Adjusting speed has no effect. Can you look and let me know where?

Looks like you have taken the code for the police lights from the web and added the knight rider (either from the web or wrote it yourself). I say this because your code is inconsistent (a mix of delay and a millis() based approach) and the conclusion is that you don't understand the code that you copied

You need to get rid of the delay as indicated above. In an attempt to help, an explanation of how your police lights work.

Let's first leave the knight rider effect out of the loop(). The loop() function is called (executed) repeatedly; it's for now not relevant how that is achieved. So without your knight rider effect, the only thing that loop() does is calling (executing) policeLights().

In the beginning of your code, you have

unsigned long switchDelay = 250;  // delay values changing flashing behavior
unsigned long strobeDelay = 50;
unsigned long strobeWait = strobeDelay; // Seed initial wait for strobe effect
unsigned long waitUntilSwitch = switchDelay;  // seed initial wait, switch LEDs

What that does is "remember that at time N, I have to do something" where N is either 250 for strobeWait and 50 for waitUntilSwitch. It's like having an appointment and thinking "remember, I have an appointment and have to leave the house at 10 o'clock". You have two appointments; in this case it might be easier to think in breaks at work; you have a coffee break every 50 minutes and a lunch break every 250 minutes.

The first thing policeLights() does is set the leds according to states stored in memory.
2)
Next it looks on the clock (millis()) and checks if it is already time (waitUntilSwitch) to switch the lights off (lunch break).
If yes, it indicates that the lights must be switched off and remembers the next time (waitUntilSwitch += switchDelay) for a lunch break.
Else it does nothing.
3)
Next it looks on the clock (millis()) and checks if it is already time (strobeWait) to toggle the lights (coffee break).
If yes, it sets the new state for the lights and remembers the next time (strobeWait += strobeDelay) that the lights must be toggled (coffee break).
Else it does nothing.

Now policeLights has done its job and returns to loop(). Note that if the state of the lights has changed, that will not be 'actioned' so there is no effect when policeLights() is done.

Next loop() calls policeLights() again. It will first set the states (1 above); if the states were updated in the previous call, the lights will now reflect the updated states. And next it will do 2 and 3 again.

And so on and so on ...

Because no delays are used, policeLights() takes less than a few microseconds to run.

Now, when the other code in loop() is added, the delay() will interfere. policeLights() will now only be called every 400 millisecond (8x timer delay).

So you need to break the knight rider effect into small steps. In most Arduino code, for-loops and while-loops() are not needed; that's the case here as well; after all, loop() is called repeatedly and hence whatever you do in loop() is done repeatedly.

I suggest that you write a knightrider function and call that from loop(). Every time that you call that function, it will do one step (switch one light on or off if it's time to do so).

You will need a few extra variables in the beginning of the code; two for the timing and one to indicate which pin will be switched on or off when it's time. There is also one that determines which direction your effect goes (left to right or right to left).

unsigned long knightriderDelay = 50;
unsigned long knightriderWait = knightriderDelay;
char knighriderIndex = 0;
char knightriderDirection = 1;  // 1 = left to right, -1

I used char types for the latter two; they are just smaller integers that can hold values between -128..+127 which is sufficient.

Every time that it's time to do a light for the knightrider effect, you do that on the pin indicated by knigtriderIndex and next increment (for left to right) or decrement (for right to left) the knightriderIndex; the increment/decrement is achieved by adding direction to the index. Note that you need to check if the index reaches 0 or 5 and change the direction from 1 to -1 or from -1 to +1.
I leave the puzzle to implement the knightrider() function up to you for now; when stuck, ask.

Hi all,

All is working now as a charm. The speed for both are correct. I dropped the delay(timer) and went with the millis(). I thank all of you for your inputs.