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.
8 years, 3 months ago.
Multi Function PUSH button
Hello,
I would like to ask a question regarding best way to implement a button function for multipurpose. I would like to develop an application such that when user push the button the writing of data should happen to SD card and when the user press the button one more time then the logger has to stop. As the button pin should be continuously monitored. Any ideas or and sample code would be really great. I am using Stm32l053 nucleo board.
First button Press : start the writing data in to SD card when second time the same Button Pressed : Stop writing data in to SD card. Thank you.
Best regards, Sandeep.
1 Answer
8 years, 3 months ago.
What you are asking for with the button is relatively simple, but implementation could depend on several things and there are ultimately several ways to do this. It would be important to understand everything that your program needs to do. For example if it is logging data, then presumably it is also reading in data from one or more sources. Also, it might be good to include an LED to indicate whether writing to the SD card is Enabled or Disabled. Are you going to use the rtos features of mbed os?
The trickiest part with buttons is usually debouncing them so you get clean on/off transitions. There are ways to do this in hardware and software. I would have to dig more into mbed InterruptIn to see how they are handling this.
Here is a quick program, I tested with my Nucleo_F401RE board and a micro SD Card and it was working. There is an SD File library in mbed os 5, but I don't know how to access it from the online compiler. So I used this SD File library here:
https://developer.mbed.org/users/neilt6/code/SDFileSystem/
include the mbed library with this snippet
#include "mbed.h" #include "SDFileSystem.h" /* LED to indicate SD State */ DigitalOut led(LED1); /* User Button Input as Interrupt */ InterruptIn button(USER_BUTTON); /* SD Card Access */ SDFileSystem sd(D11, D12, D13, D10, "sd"); /* Track State of Program */ volatile int program_state = 0; /* Called When Button is Pressed Down */ void button_pressed() { if (program_state == 1) { program_state = 0; led = 0; } else { program_state = 1; led = 1; } } /* Main */ int main() { /* use to simulate data */ float data = 0; /* Set button interrupt */ button.fall(&button_pressed); /* Main Loop */ while (1) { /* Do Nothing */ if (program_state == 0){ wait(1); // 1000 ms } /* Else write to SD Card */ else { /* Read Inputs */ data = data + 0.1; /* Do Calculations or Formating */ /* Write to SD - Mount/Unmount Each time to Free Card */ sd.mount(); /* File pointer, "a" for append */ FILE *file = fopen("/sd/datalog.txt", "a"); if(file == NULL) { printf("Could not open file for write\n\r"); } else{ fprintf(file,"%.2f\r\n",data); fclose(file); } /* Free Card */ sd.unmount(); wait(2); // 2000 ms } } }