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;
}
}’’’