Have had a look around to try & find a fix for this but can't - any help would be appreciated!
I'm trying to get this sketch to load up (Uno) with the LED on 13 set LOW regardless of the state the toggle (SPST) switch which switches on 4. The switch may be at HIGH or LOW when booted up, so I want it to sense a state change on the switch to switch it
Currently, the LED comes on at boot and I bloody well can't figure out why - it otherwise works as intended. What am I missing?!
All help appreciated,
S.
int LED = 13;
int toggleSwitchOne = 4;
int toggleSwitchStateOne = digitalRead(toggleSwitchOne);
int lastToggleSwitchStateOne = digitalRead(toggleSwitchOne);
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 25;
void setup() {
pinMode(toggleSwitchOne, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop() {
toggleSwitchStateOne = digitalRead(toggleSwitchOne);
if (toggleSwitchStateOne != lastToggleSwitchStateOne)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
digitalWrite(LED, !digitalRead(LED));
toggleSwitchStateOne = digitalRead(toggleSwitchOne);
lastToggleSwitchStateOne = !toggleSwitchStateOne;
}
}
If you plane on using more then a single switch, arrays! instead of numbering.
Removed debouce because you don't really need it here. But if you want to trigger something else rather than a LED you might want to add it again. But for that I would use a library,. way easier.