9 years, 10 months ago.

mbed 1768 Button input

Hello,

Could you teach me how to make a program for Button input which keep logic1. Normally Digital_In allow to read input status as hi or low, but this has no latch circuit, if off a Button (un-pushed) mbed IO status logic 0(zero). How to make keep the last button status?

Please advise. Yujen

2 Answers

8 years, 6 months ago.

hi andy, need your favor.. i want to change it to count the number of times the button was pressed.where there got two push buttons one for increasing and another one will decrease the value.. thank you in advance..

That's a fairly simple change:

volatile int count = 0;

void onUpButtonPush() {
  if (debounceTimer.read_us() > debounceTimeUS) {
    if (upButton == 1) {  // very basic sanity check that this wasn't a glitch when the button was released.
      count ++;
    }
  }
 debounceTimer.reset(); // reset the timer to 0
 debounceTimer.start(); //  probably not needed but just in case a reset stops the timer...
}

void onDownButtonPush() {
  if (debounceTimer.read_us() > debounceTimeUS) {
    if (downButton == 1) {  // very basic sanity check that this wasn't a glitch when the button was released.
      count--;
    }
  }
 debounceTimer.reset(); // reset the timer to 0
 debounceTimer.start(); //  probably not needed but just in case a reset stops the timer...
}
posted by Andy A 12 May 2016
9 years, 10 months ago.

I'm not sure exactly what you want to do. The last button state latched at what time? Do you want a variable that becomes set when the button is pressed and remains set until you clear it?

The code below will detect the button press and keep that information until the program clears the indication. Alternatively you could change it to count the number of times the button was pressed.

InterruptIn myButton(p10);

// this variable is set to true when the button is pressed and remains true until cleared by the program.
// use the volatile keyword since its value is changed by an interrupt.
volatile bool buttonPressed;


// this function is called whenever the button is pressed.
// Note - due to glitches caused by the button contacts bouncing this could be called multiple times when button is pressed.
// It could also be called when button is released due to the contacts bouncing around a bit.
// Search for de-bouncing for details on how cope with this if it causes problems.
void onButtonPush() {
  if (myButton == 1) {  // very basic sanity check that this wasn't a glitch when the button was released.
    buttonPressed = true;
  }
}

// main loop
main () {

  // set the function onButtonPush to be called every time that pin goes from 0 to 1.
  myButton.rise(&onButtonPush);

  while (true) {

    if (buttonPressed) { // the button has been pressed.
      // do something
      buttonPressed = false; // clear the button detection.
    }
    wait_ms(10); 
  }

}

Hello Andy,

Thank you for advising.. I think that you understand what I asked.. I wanted: When you pushed the button LCD indicate 1, but LCD keep to indicate 1 if you leave it. And when you pushed again, LCD indicate 0 as low.

I tried to run this program with TextLCD.h library, but it didnt work... Could you advise with LCD indication?

Thanks, Yujen

posted by Yujen sai 21 Jan 2015

I'm assuming that by TextLCD.h you mean this: http://developer.mbed.org/users/simon/code/TextLCD/ (in the future please be as specific as possible as to what you are using or include a link, a file name on it's own is fairly meaningless)

This code should display a 1 or a 0 on the display depending on the state.

The code in the interrupt tracks the button state. The main loop then loops constantly looking for changes and updating the display if needed. It uses a timer to limit the maximum rate that the button can be pressed so that glitches on the switch don't get counted twice. This is currently set to 1000us which is on the low side, you may want to increase this if you see things getting counted twice.

#include "mbed.h"
#include "TextLCD.h"

InterruptIn myButton(p10);
Timer debounceTimer;
TextLCD lcd(p15, p16, p17, p18, p19, p20); // set pins for your setup.
 
// assume any two key presses within 1000us of each other are caused by mechanical issues with the switch.
// This value may need tweaking depending on the switch used.
const int debounceTimeUS = 1000; 

// this variable is set to true when a 1 should be displayed.
// use the volatile keyword since its value is changed by an interrupt.
volatile bool buttonPressed = false;
 
 
// this function is called whenever the button is pressed.
// Note - due to glitches caused by the button contacts bouncing this could be called multiple times when button is pressed.
// It could also be called when button is released due to the contacts bouncing around a bit.
// Search for de-bouncing for details on how cope with this if it causes problems.
void onButtonPush() {
  if (debounceTimer.read_us() > debounceTimeUS) {
    if (myButton == 1) {  // very basic sanity check that this wasn't a glitch when the button was released.
      buttonPressed = !buttonPressed; // change the state of the variable
    }
  }
 debounceTimer.reset(); // reset the timer to 0
 debounceTimer.start(); //  probably not needed but just in case a reset stops the timer...
}
 
// main loop
main () {

// start the timer. 
 debounceTimer.reset();
 debounceTimer.start();

  lcd.cls(); //clear the screen
  lcd.putc('0'); // display a 0
// set the function onButtonPush to be called every time that pin goes from 0 to 1.
  myButton.rise(&onButtonPush);

  bool previousState = false; 
  while (true) {
     if (buttonPressed != previousState) { // the button has been pressed.  
       previousState = buttonPressed;
       lcd.cls(); //clear the screen
       if (buttonPressed )
        lcd.putc('1');
       else
        lcd.putc('0');
    }
  }
 
}
posted by Andy A 21 Jan 2015