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