Program Structure: declare ButtonState: Global variable holding status of button unsigned long Button Time: Holds time to debounce Switch int Button NextState( input) function that is to be called in loop to service the switch switch based on ButtonState Idle: State where nothing has been happening. if input is low, Button Time millis0: Record time of high to low transition. set ButtonState to Wait Move to Wait state. Wait: Button has gone low and we are waiting for it to remain low for 5 milliseconds if input is high. l f button has gone high. set ButtonState to Idle Reset back to Idle else if (millis0- Button Time 5 if 5 milliseconds has passed. set ButtonState to Low Move to low state. return 1 indicating that button has been pressed Low Button is low and has been so for 5 milliseconds. if input is high Once button released. set ButtonState to Idle: end of switch return 0 By default return 0 indicating nothing is happening. setup: Function run at the start of the program. Set pin as input Check Schematic in Lab 4 for pin number. Set ButtonState to Idle Initialize state loop Function continuously called Check status of button. if ButtonNextState( digitalRead( input)) Send serial message indicating button press Indicate button has been pressed end of if Solution //initialize and declare variables const int ledPin = 13; //led attached to this pin const int buttonPin = 2; //push button attached to this pin int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { //set the mode of the pins... pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); }//close void setup void loop() { //sample the state of the button - is it pressed or not? buttonState = digitalRead(buttonPin); //filter out any noise by setting a time buffer if ( (millis() - lastDebounceTime) > debounceDelay) { //if the button has been pressed, lets toggle the LED from \"off to on\" or \"on to off\" if ( (buttonState == HIGH) && (ledState < 0) ) { digitalWrite(ledPin, HIGH); //turn LED on ledState = -ledState; //now the LED is on, we need to change the state lastDebounceTime = millis(); //set the current time } else if ( (buttonState == HIGH) && (ledState > 0) ) { digitalWrite(ledPin, LOW); //turn LED off ledState = -ledState; //now the LED is off, we need to change the state lastDebounceTime = millis(); //set the current time }//close if/else }//close if(time buffer) }//close void loop.