/* Button as switch Original David A. Mellis 2006 Modified Mauro Herrero - Fab Academy 2021 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH, or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit, ignoring noise */ //set constants, according to attiny pinout const int button = 3; const int blue = 4; //set variable - buttonstate int blueState = 0; int reading; int lastRead = 0; // The following variables are longs because the time is measured in milliseconds, // and will be a huge number long time = 0; // last time the output pin was changed long debounce = 100; // debounce time void setup() { // put your setup code here, to run once: pinMode(blue, OUTPUT); pinMode(button, INPUT); } void loop() { // put your main code here, to run repeatedly: reading = digitalRead(button); if (reading == HIGH && lastRead == LOW && millis()-time > debounce){ if (blueState == HIGH) blueState = LOW; else blueState = HIGH; time = millis(); } digitalWrite(blue, blueState); // lastRead = reading; }