# define buttonPin 7
# define ledPin 8
typedef bool digitalReading;
const unsigned long debounce = 50;
# define PRESST LOW
void setup() {
Serial.begin(115200);
Serial.println("\nwell do you?\n");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
unsigned long now;
unsigned long timer;
digitalReading previousButtonReading; // readings are true when button is pressed
void loop()
{
now = millis();
bool switchAction = false;
if (now - timer > debounce) {
// ok, it's been a minute. read the switch
digitalReading buttonReading = digitalRead(buttonPin) == PRESST;
// if it's different...
if (buttonReading != previousButtonReading) {
Serial.print("I see that button ");
// ...it must have gotten pressed or released since
if (buttonReading) {
Serial.println("pressed.");
// flag for action on switch gets pressed
switchAction = true;
}
else Serial.println("released.");
// time stamp and remember for next pass
previousButtonReading = buttonReading;
timer = now;
}
}
if (switchAction)
digitalWrite(ledPin, digitalRead(ledPin) == HIGH ? LOW : HIGH);
}
/*
Serial.print("buttonReading "); Serial.print(buttonReading);
Serial.print(" previousButtonReading "); Serial.print(previousButtonReading);
Serial.println();
*/
/*
hey...
# define clock 9
pinMode(clock, OUTPUT);
unsigned char lastButtonRead; // #1 - bool, byte false button was not pressed at power up. deal with it
unsigned char currentButtonState;
void loop0() {
unsigned char currentButtonRead = digitalRead(buttonPin) == LOW; // #1 - bool, byte low is pressed is true
if (currentButtonRead != lastButtonRead) { // #2, #3 - bool, bool
timer = millis(); // #4 - unsigned long
lastButtonRead = currentButtonRead; // #5 - bool, bool
}
if ((millis() - timer) > debounce) {
// yes at loop speed Serial.println("see these transitions?");
// #6 - unsigned long, unsigned long, unsigned long
// Serial.print("currentButtonState "); Serial.print(currentButtonState);
// Serial.print("lastButtonRead "); Serial.print(lastButtonRead);
// Serial.println();
if (currentButtonState == PRESST && lastButtonRead != PRESST) { // #7, #8 - bool, bool
Serial.println(" and ");
// change LED states // #9
digitalWrite(ledPin, digitalRead(ledPin) == HIGH ? LOW : HIGH);
}
currentButtonState = lastButtonRead; // #10 - bool, bool
}
// delay(777);
}
unsigned long counter;
void loop1()
{
now = millis();
counter++;
// digitalWrite(clock, (now >> 8) & 0x1 ? LOW : HIGH);
static unsigned long reportTime;
if (now - reportTime > 10000) {
reportTime = now;
Serial.print(now); Serial.print(" ");
Serial.println(counter);
}
if (now - timer > debounce) {
// ok, it's been a minute. read the switch
bool buttonReading = digitalRead(buttonPin) == LOW;
if (buttonReading != lastButtonRead) {
lastButtonRead = buttonReading;
timer = now;
Serial.print("I see that button ");
if (buttonReading == PRESST) {
Serial.print("pressed.");
digitalWrite(ledPin, digitalRead(ledPin) == HIGH ? LOW : HIGH);
}
else Serial.print("released.");
}
}
}
*/