Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
Dual input Blinking LED
Hello, I am new to mbed programming. I tried to make a program to make the LED blink for infinite period of time until the button is pressed. Input is 0s and 1s generated from digital wave generator as well as a button to make LED on and off initially. Here is a snippet of the code that I wrote.
#include "mbed.h"
DigitalOut myled(LED1);
DigitalIn input(p17);
// p17 is pin17 where the pulse generator is connected //
int states;
bool button;
int main(){
while (button ==1){
switch(states){
case 0:
input == 1;
myled = 1;
break;
case 1:
input == 0;
myled = 0;
break;
}
}
}
I'm unable to test this until few weeks later so I'd love any suggestions, errors or criticism on my way of thinking (i feel like not including button ==0 state conditions and usage of switch here is not good but can't point out exactly what). Thank You for looking through this.
1 Answer
9 years, 1 month ago.
See a number of issues with the posted code.
1) You wish to use an external push button ? Mechanical push buttons have contacts that will bounce many times before the contacts settle. Each such bounce will cause the code to trigger. That is, unless treated as a special case of hardware, the PB will cause numerous triggers
Review this page on how to use external push buttons with mbed:
https://developer.mbed.org/users/4180_1/notebook/pushbuttons/
With the proper mbed library call, you can convert the PB mechanical action to be a single press = single '1' or '0', and vice versa.
2) What is 'button' ? What is the initial value of 'button' ? You must insert a starting state if this variable is internal to the code. Otherwise, if bonded to an external pushbutton then review the above URL on how to map this pushbutton. As show in the posted code, 'button' is not defined in your code.
3) Same issue with 'states'.
4) 'input' is an input connected to a GPIO pin. OK. But then the code alters this read input variable ? You should be ok to map to a GPIO pin but leave this pin definition as-is. If you need to alter this value then read or copy this read value to another local variable for your state machine.
5) you will need to insert some form of a delay to allow the LED to be visible. Read up on the blink example from mbed. That is, when your trigger occurs -> light up an LED -> wait so we can view the LED ENABLE state -> then move to the next state. The delay part is missing from your code.
Your idea of creating a state machine is sound but the related variables require some tweaks to operate correctly as required.
Recommend that you wait till you can test your code to refine and debug your code against the hardware.