Push button latch

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear all,
Anyone to suggest the best possible way I can make a pushbutton latch on atmel studio?

Any help will be appreciated!

Oyekunle Quadri

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (button_is_pressed) {
    button_state = pressed;
}

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or, did you mean you also want the ability to unlatch it as well?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes to unlatch as well

Oyekunle Quadri

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Will I set the bit as flipped?
Or how do I set the bit?

Oyekunle Quadri

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Push buttons are surprisingly difficult to write code for.  They "bounce" on the milli-Second level when pressed or released. The CPU is fast enough to treat each of these on/off bounces as individual button presses. 

 

Buttons are pressed at the fastest rate at about ten times a second.  Generally, buttons are one per Port input/output pin.  They use a pull-up resistor to Vcc to keep their unpressed state at logic high.   The main code checks the button about 20 times a second.  If the new reading is different than the previous reading,  the main code waits for about 20 milliSeconds and checks the button In/Out pin again.  This is the bouncing period; after 20 mS the bottom level is stable.  At this time, the switch has been "debounced" after being pressed or released.

 

  Buttons are usually handled by libraries in Arduino.  Each library author tries to make his library more "complete" or useful than the other libraries for push switches.  So they end up with really complex data structures with things like call-back function pointers and complex timer configurations that make this code difficult for beginners to understand and use.  However, popular Arduino libraries for push switches can deal with double-clicks, cycle-when-button-held, run-time change of button function, short vs. long presses, and anything else that they can think of.  You can learn a lot from examining Arduino libraries, but sometimes it is like carving concrete with a spoon.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah... I read about the denounce of a push button and have been trying to comprehend fully the danni's code on that.
But I was just hoping a method of latching will be possible with it just like the normal flip-flop latch Design.

Regards

Oyekunle Quadri

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'll add my own version in block form:

 

static bool button_state = false;
....
if (button_is_newly_pressed())
{
    button_state = !button_state;
}

This toggles the button state every time the button is newly pressed.  The devil is in the details of button_is_newly_pressed().

 

Issue 1: Buttons bounce, both on MAKE and on BREAK.  They can bounce for many milliseconds, and you can't have your code respond to each bounce as a new button press.  So you have to incorporate some form of delay when your code detects a change of button state (button is newly pressed or newly released).  This delay can be in the form of a timer tick that scans the buttons every e.g. 10-25 ms, or it can be a delay after detecting a change of button state, or it can even be a loop that counts N consecutive button states and resets if the button changes again before N counts (here the delay is in the loop execution itself).  Since delays where no other CPU work can be done are generally bad, the timer tick approach is usually preferred.

 

Issue 2: Once the button is pressed and being held down, your code can't come back and keep detecting new button presses.  Your code must only look for transitions from NOT_PRESSED to PRESSED, so you will need to keep track of previous and new button state every time you look at the button:

 

NOT_PRESSED -> NOT_PRESSED   ;ignore

NOT_PRESSED -> PRESSED  ;debounce this and record a button press

PRESSED -> PRESSED  ;ignore

PRESSED -> NOT_PRESSED  ;ignore but debounce

Last Edited: Sun. Apr 12, 2020 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Debouncing in your case is easier than normal debouncing...you want to press and activate, then press again to deactivate.  So you only need to detect pressing & thus you don't need to debounce in the normal sense.

As soon as you see an edge (or multiple edges), you can take action...you just need a blanking interval of  maybe 100ms, where it no longer looks.  The blanking can be a timer tick (sample the switch), so the program doesn't have to sit and wait for the duration.

 

loop:

if pin high:

    if the_state==OFF

        the_state= ON

    else

        the_state=OFF

    delay(100ms)   ...use a timer sampler to call the routine rather than this simple halt, if holding/waiting not desirable

 

Note holding the button down will give an auto repeat toggling...might be useful (or not).

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!