Convert the following psuedo code to an arduino program: Program Structure : dec
ID: 2248378 • Letter: C
Question
Convert the following psuedo code to an arduino program:
Program Structure : declare ButtonStates II Global variable holding status of button unsigned long Time; I Holds time to debounce Switch int ButtonNextState input) II 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, Time = millis; I/Record time of high to low transition. set Button State to wait / Move to wait state. Turn on LED (Pin 13 high) Wait: IButton has gone low and we are waiting for it to remain low for 5 milliseconds if input is high, IIIf button has gone high, set ButtonState to Idle H Reset back to Idle. else if (millis(0-time >= 5)// if 5 milliseconds has passed. set Button State to Low I Move to low state. Turn off LED (Pin 13 low. returm Il indicating that button has been pressed. Low: I/Button is low and has been so for 5 milliseconds. if input is high / Once button released. set Button State 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. I Check Schematic in Lab 4 for pin number. Set Button State 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 Lab Assignment: Prelab: Write the program described in the program section. You are required to use the function call approach described above. You will need to read the button in later labs.Explanation / Answer
//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