So I'm working on a program to incorporate a 555 functionality that is hooked to a toggle switch. The output pin pulses a set of 4015 shift registers to stack a series of LEDs in sequence. The toggle turns the blink function on, but I want to be able to depower the system with the ability to have the switch still set to high. When the system is repowered, I don't want the arduino to commence pulsing the 4015s. The user will need to toggle the switch to low, and then once it is toggled back to high, the sequence will start again.
My code so far looks like this
const int ledPin = 11;
const int switchPin = 2;
boolean switchPinCheck = true;
int led1State = LOW;
int switchPinState = 0;
long previous1Millis = 0;
long interval = 50;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
switchPinState = digitalRead(switchPin);
if(switchPinState == HIGH)
switchPinCheck = false;
}
void blink(long mod, int led, int *state, long *previous){
unsigned long currentMillis = millis();
if(currentMillis - *previous > interval/mod){
*previous = currentMillis;
if (*state == LOW)
*state = HIGH;
else
*state = LOW;
digitalWrite(led, *state);
}
}
void loop() {
switchPinState = digitalRead(switchPin);
if(switchPinCheck == true){
if(switchPinState == HIGH){
blink(1, ledPin, &led1State, &previous1Millis);
}
}
if(switchPinState == LOW)
switchPinCheck = true;
}
The idea is to use the boolean to decouple the blink function from the switch state until the user has cycled the switch.
Perhaps I should put the boolean true initialization within the setup routine as an if/else setup instead of only having the switch read set it to false?