LED Police chaser code

Hi All,

I found a fun Police Chasers Light project to add to my robot car. The thing is that i want to add a ON/OFF switch for these LEDS. One of the mandatory requirements is to not use delay function and thats why i used Millis. But i am not sure how to add a ON/OFF switch in this code, with the delay i can work with if HIGH else LOW etc. Could someone push me in the right direction? I want to use a push button.
afbeelding

  • How to connect the push button?
  • What to add where in code below?
// English for which LED to Strobe
#define RED 0x0
#define BLUE 0x1
 
// Variable to track which LED is on
byte whichLED = RED;
 
// Where are the LEDs connected?
const int LED_Red = 7;
const int LED_Blue = 11;
 
// State variables for the LEDs
byte Red_State = LOW;
byte Blue_State = LOW;
 
// Some delay values to change flashing behavior
unsigned long switchDelay = 250;
unsigned long strobeDelay = 50;
 
// Seed the initial wait for the strobe effect
unsigned long strobeWait = strobeDelay;
 
// Variable to see when we should swtich LEDs
unsigned long waitUntilSwitch = switchDelay;  // seed initial wait
 
void setup() {
   pinMode(LED_Red, OUTPUT);
   pinMode(LED_Blue, OUTPUT);
}
 
void loop() {
    digitalWrite(LED_Red, Red_State);     // each iteration of loop() will set the IO pins,
    digitalWrite(LED_Blue, Blue_State);    // even if they don't change, that's okay
 
    // Toggle back and forth between the two LEDs
    if ((millis() - waitUntilSwitch) >= 0) {
        // time is up!
        Red_State = LOW;
        Blue_State = LOW;
        whichLED = !whichLED;  // toggle LED to strobe
        waitUntilSwitch += switchDelay;
    }
 
    // Create the stobing effect
    if ((millis() - strobeWait) >=0 ) {
        if (whichLED == RED)
            Red_State = !Red_State;
        if (whichLED == BLUE)
            Blue_State = !Blue_State;
        strobeWait += strobeDelay;
    }
}

How it must work?
1 push for start and 1 for stop cycle?
Or while is pushed the cycle run?

Yeah when pushed it turns on and if pushed again, it turns off. Nothing exciting.

consider

#define MyHW
#ifdef MyHW
const int LED_Red  = 13;
const int LED_Blue = 12;

const int pinBut   = A1;

#else
const int LED_Red = 7;
const int LED_Blue = 11;
#endif

#define Period   250
unsigned long msecLst;

byte          butState;
bool          enable = true;

// -----------------------------------------------------------------------------
void setup () {
    pinMode (pinBut,   INPUT_PULLUP);

    pinMode (LED_Red,  OUTPUT);
    pinMode (LED_Blue, OUTPUT);

    digitalWrite (LED_Red,  HIGH);
    digitalWrite (LED_Blue, LOW);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if (enable && (msec - msecLst) > Period)  {
        msecLst = msec;

        digitalWrite (LED_Red,  ! digitalRead (LED_Red));
        digitalWrite (LED_Blue, ! digitalRead (LED_Blue));
    }

    byte but = digitalRead (pinBut);
    if (butState != but)  {
        butState = but;
        delay (10);         // debounce

        if (LOW == but)
            enable = ! enable;
    }
}

Is there a possibility to replace the delay(10) with millis? And also how would you recommend the connection of the button?
afbeelding

yes, but it makes the code unnecessarily complicated unless the short 10 msec delay affects some real-time performance requirement.

when you add a pushbutton you keep them high. When you push the button it's connected to ground and get low. Then you change the stat and put the lights on. when you push again the lights will go off when the state has changed.

did you notice?

Could you put the example in the full code? I am kind off newbish to the whole programming.

This works, but when i turn them off, always one of them stays on. And how can i add the button part in the strobing effect which i posted?

because it simply stops the flashing. why don't you add a sub-function to turn both LEDs off that is called when enable is false

not sure what your strobing effect is doing

Hi gcjr,

Its this project Arduino Example: Police Lights with millis() - Bald Engineer

I basically want to add a push button to the above mentioned project! Only i dont know how to add the pushbutton codes in the program to turn on/off

Alright, I am able to turn them on by pressing the switchbutton, but i need to hold it pressed in, otherwise the LEDs turn off. How can i change this current code to press once to turn on and press again to turn off?

#define RED 0x0 // Which LED to Strobe
#define BLUE 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 = 11; // Where are the LEDs connected?
const int LED_Blue = 8;
const int strobeButton = A1; 

byte Red_State = LOW; // State variables for the LEDs
byte Blue_State = LOW;
byte strobeButtonState = 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[] = {8, 11};
int count = 0;
int timer = 50;

void setup() {
pinMode(LED_Red, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(strobeButton, INPUT_PULLUP);

    digitalWrite (LED_Red,  HIGH);
    digitalWrite (LED_Blue, LOW);
    
for (count = 0; count < 6; count++) {
pinMode(pinArray[count], OUTPUT);
}
}

void loop() {

digitalWrite(LED_Red, Red_State); // each iteration of loop() sets the IO pins
digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay


strobeButtonState = digitalRead(A1);
  if (strobeButtonState == LOW)
  {
digitalWrite(LED_Red, Red_State); // each iteration of loop() sets the IO pins
digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay
 
     if ((long)(millis() - waitUntilSwitch)>=0)
     {
      Red_State = LOW;
      Blue_State = LOW;
      whichLED = !whichLED;
      waitUntilSwitch += switchDelay;
     }
 
     if ((long)(millis() - strobeWait)>=0)
     {
      if (whichLED == RED)
          Red_State = ! Red_State;
      if (whichLED == BLUE)
          Blue_State = ! Blue_State;
      strobeWait += strobeDelay;
}
  }
    }


in the code i posted, i showed how to check for a button press (change of state from HIGH to LOW) and toggle a flag (i.e. enable) used to control flashing.

do you understand that code?

I find it hard to understand. How can I integrate your part code into mine?

Your code:

    byte but = digitalRead (pinBut);
    if (butState != but)  {
        butState = but;
        delay (10);         // debounce

        if (LOW == but)
            enable = ! enable;

My code:

#define RED 0x0 // Which LED to Strobe
#define BLUE 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 = 11; // Where are the LEDs connected?
const int LED_Blue = 8;
const int strobeButton = A1; 

byte Red_State = LOW; // State variables for the LEDs
byte Blue_State = LOW;
byte strobeButtonState = LOW;

bool    enable = true;

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[] = {8, 11};
int count = 0;
int timer = 50;

void setup() {
pinMode(LED_Red, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(strobeButton, INPUT_PULLUP);

    digitalWrite (LED_Red,  HIGH);
    digitalWrite (LED_Blue, LOW);
    
for (count = 0; count < 6; count++) {
pinMode(pinArray[count], OUTPUT);
}
}

void loop() {
strobeButtonState = digitalRead(A1);
  if (strobeButtonState == LOW)
  {
digitalWrite(LED_Red, Red_State); // each iteration of loop() sets the IO pins
digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay
 
     if ((long)(millis() - waitUntilSwitch)>=0)
     {
      Red_State = LOW;
      Blue_State = LOW;
      whichLED = !whichLED;
      waitUntilSwitch += switchDelay;
     }
 
     if ((long)(millis() - strobeWait)>=0)
     {
      if (whichLED == RED)
          Red_State = ! Red_State;
      if (whichLED == BLUE)
          Blue_State = ! Blue_State;
          
      strobeWait += strobeDelay;
}
  }
    }


please study it.
if you have a questions -- about the code -- ask

Just cant get it to work... :frowning:

Try to added Serial.println("YOURSTRING"OR TAKE VARIABLE) after things you want to know if the are succided... and upload again.

this simply execute the flashing code while the button is being pressed.
it doesn't use a flag to execute the flashing code.
it doesn't check for a change in button state
it doesn't check when the button goes from HIGH to LOW
it doesn't toggle a flag