Hello I have a momentary toggle switch that turns on two LED's that is working correctly for left and right barrels. I also have a blinking function on a separate switch controlling the same LED's. The problem I am having is when I have the blink function operational the LED toggle if statement will only flash once and not turn on even though the Serial print shows the toggle switch working. At the end of the blink function if the switch is turned off the LED's are set to turn off and what I have found is I have to comment out the LED's digitalWrite(lBarrel, LOW) and digitalWrite(rBarrel, LOW) in order for the toggle switch to operate correctly but then when I turn off the blink switch the LED's will sometimes still be on which would be normal without the digitalWrite LOW. How can I do a work around or is it possible. Thanks RBixler.
const int LEDsw = 7; // Barrel LED switch
const int staticsw = 8; // Momentary switch to change LED from on to off
int LEDswState = 0; // set switch state and start at 0/LOW
int LEDstate = 0; // create and set the LED state to LOW to start
const long ledInterval = 50; // LED flashing time
unsigned long prevMillis = 0; // Declare previous time
int staticState = LOW; // Declare set static switch state and start at 0/LOW
int staticSwitchNew;
int staticSwitchOld = 1;
void setup() {
Serial.begin(9600); // start serial comunications
// Set pin modes for all input and output controls
pinMode(LEDsw, INPUT);
pinMode(staticsw, INPUT);
pinMode(lBarrel, OUTPUT);
pinMode(rBarrel, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis(); // Declare current time
LEDswState = digitalRead(LEDsw); // Read the LED switch
staticSwitchNew = digitalRead(staticsw);
if (staticSwitchOld == 0 && staticSwitchNew == 1){
if (staticState == 0) {
digitalWrite(lBarrel, HIGH);
digitalWrite(rBarrel, HIGH);
staticState = 1;
} else {
digitalWrite(lBarrel, LOW);
digitalWrite(rBarrel, LOW);
staticState = 0;
}
}
staticSwitchOld = staticSwitchNew;
Delay(50);
barrelLEDs();
} // End main loop
/********** Functions ***********/
void barrelLEDs() {
unsigned long currentMillis = millis();
// Check if the LED switch is on
if (LEDswState == HIGH) {
LEDswState = HIGH;
if (currentMillis - prevMillis >= ledInterval) {
//Save the last time you blinked the led
prevMillis = millis();
// if the led is off turn it on and vice-versa
if (LEDstate == LOW) {
LEDstate = HIGH;
} else {
LEDstate = LOW;
} // End millis() control
} // End LED state
digitalWrite(lBarrel, LEDstate);
digitalWrite(rBarrel, LEDstate);
} else {
LEDswState = LOW;
//digitalWrite(lBarrel, LOW);
//digitalWrite(rBarrel, LOW);
} // End LED switch state
} // End LED control
