const unsigned char buttonPin = 7;
void setup() {
Serial.begin(115200);
Serial.println("Hello\n");
pinMode(buttonPin, INPUT_PULLUP);
}
# define DEBOUNCE 50 // milliseconds to ignore switch
unsigned char buttonState, prevButtonState;
unsigned long lastButtonTime;
unsigned char state;
unsigned char printed;
void loop() {
unsigned long now = millis();
if (now - lastButtonTime > DEBOUNCE) { // switch still hot from last time? ignore it.
buttonState = digitalRead(buttonPin);
if (buttonState != prevButtonState) {
// button has been pressed or released
if (buttonState == LOW) {
// move to the next state
state++;
if (state == 4) state = 0; // reset
printed = false;
}
// remember the button state and time
prevButtonState = buttonState;
lastButtonTime = now;
}
}
if (!printed) {
Serial.println(state);
printed = true;
}
}