11 years, 2 months ago.

m3pi reading button connected to mbed p21

I want to test the pushbutton on the m3pi board connected to the mbed pin 21.

Pololu's m3pi schematic indicates this button is wired to the mbed pin 21.

My goal is to press the button and light the Mbed LED4. I have looked through the documentation and cannot determine the correct mode to use with the DigitalIn function. I have tried it with no mode and am currently trying it with PullDown. LED4 is not lighting when I press the button.

I am writing the button state to a file. The button state is always 1 the first time through the loop, whether or not I press it.

My code follows:

#include "mbed.h"

LocalFileSystem local("local");               // Create the local filesystem under the name "local"
 
DigitalIn button(p21);
DigitalOut led(LED4);
 
int main() {
    int count = 0;
    int button_state = 0;
    
    FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
        button.mode(PullDown);
   
    
    while(count < 10) {
    
        button_state = button.read();
        //button_state = !button_state;
        
        fprintf(fp, "count: %d, button state: %d\n", count, button_state);
        led = button_state;
        
        ++count;
        wait(0.5);
    }
    
    fclose(fp);
}

a) What is the correct mode to have the button state be a 1 when the button is pressed? b) Do I have to set the mode once or every time through the loop?

Thank you in advance for your repsonses and help.

posted by James Allen 05 Apr 2013

button.mode(PullUp);

posted by James Allen 05 Apr 2013

1 Answer

11 years ago.

take a look at this program it may help you.

Import programtestpb_m3pi

Test the push button on m3pi connected to p21. when button pressed led 3 will light , otherwise led 1 will light