Question: In this program will create one InterruptIn objects for the button and a Ticker object. The button interrupt will be used to detect the changes
In this program will create one InterruptIn objects for the button and a Ticker object.
The button interrupt will be used to detect the changes on the digital pin. So now, instead of having all your code in the main loop, it will be written in the ISRs, and your main loop will be empty.
Below is the pseudo-code of the program:
Button handler: Toggle a Boolean variable.
This variable will reflect if the LED has to be blinking or off.
Button2 ISR: If the Boolean variable is true, toggle the LED.
Otherwise turn LED off. Main function: Configure the button with pull-up resister.
Attach the button ISR to be called on the falling edge when button is pressed.
Attach the Ticker ISR to be called every 500ms Infinite loop where the program waits for interruptions to occur
DIGITAL IN/OUT FUNCTIONS
The DigitalIn interface is used to read the value of a digital input pin.
eg. to read the value of a pin: DigitalIn button_press(Input Pin);
int main(){ if (button_press) Led_out = !Led_out; //toggle the state of Led_out }
The DigitalOut interface is used to configure and control a digital output pin, for example DigitalOut Led_out(Output Pin);
int main() { Led_out = 1; //turn Led_out high. }
INTERRUPT IN FUNCTIONS The InterruptIn interface is used to trigger an event when a digital input pin changes.
eg: InterruptIn button_press(Input Pin); void button_ISR(){ Led_out = !Led_out; } int main(){ button_press.rise(&button_ISR); //Interrupt sub-routine button_ISR will be called //when a rising edge occurs. while(1); //waiting for interrupts }
TICKER FUNCTIONS The Ticker interface is used to setup a recurring interrupt to repeatedly call a function at a specified interval.
For example: Ticker blinky;
void blink(){ led1 = !led1; } int main(){ blinky.attach(&blink, 0.5); //Interrupt sub-routine blink() will be called every 0.5s while(1){ //do something else}}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
