Write following code section to C Program Structure: declare ButtonState; // Glo
ID: 3754836 • Letter: W
Question
Write following code section to C
Program Structure: declare ButtonState; // Global variable holding status of button unsigned long ButtonTime; 1/ Holds time to debounce Switch int ButtonNextState( input) // function that is to be called in loop to service the switch switch based on ButtonState Idle: /7 State where nothing has been happening. if input is low, ButtonTime-millis I/ Record time of high to low transition set ButtonState to Wait // Move to Wait state. Turn on LED // Pin 13 high used for debugging Wait: /7 Button has gone low and we are waiting for it to remain low for 5 milliseconds if input is high, 1/ If button has gone high, set ButtonState to Idle // Reset back to Idle. else if (millis () -time 5 // if 5 milliseconds has passed set ButtonState to Low // Move to low state. Turn off LED // Pin 13 low also used for debugging). 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 /1 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 f( ButtonNextState ( digitalRead( input ) ) // switch is connected to pin 4. end of if Send serial message indicating button press / Indicate button has been pressed. 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
Hi, Here comment and output is not nessesory so I put only code in c.
#include <stdio.h>
#define Idle 0
#define Wait 1
#define Low 2
#define HIGH 1
#define LOW 0
int ButtonState ;
unsigned long ButtonTime;
int Led = 0 ;
int ButtonNextState(int input)
{
switch(ButtonState)
{
case Idel:
if(input == LOW)
{
ButtonTime = millis();
ButtonState = Wait;
Led = 1;
}
break;
case Wait:
if(input == HIGH)
ButtonState = Idle;
else if(millis()-time >= 5)
{
ButtonState = Low;
Led = 0;
return 1;
}
break;
case Low:
if( input == HIGH )
ButtonState = Idle;
break;
}
return 0;
}
int main()
{
int pin = 1; // assuming 1 as a input and 0 as a output
ButtonState = Idle;
while(1)
{
if(ButtonNextState(digitalRead(input)))
printf("Button pressed ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.