Menu

debounce button with millis

Alex
2020-02-25
2020-05-11
1 2 > >> (Page 1 of 2)
  • Alex

    Alex - 2020-02-25

    hello everybody, i am new in GCB (good software ) i would like to use debounce button with millis (30 ms debounce) , could you give some example or idea, thanks in advance

     

    Last edit: Alex 2020-02-25
  • Anobium

    Anobium - 2020-02-25

    Hello Alex,

    Are you coming from the Arduino usage to Great Cow BASIC? This is to give some context to your question.

     
  • Alex

    Alex - 2020-02-25

    Hello Anobin, actually yes, i have used arduino with millis for debounce also ldmicro, i am not good at using interrupts or ticks, maybe some example with them i could give it a try

     
  • Anobium

    Anobium - 2020-02-25

    I thought so.

    You do not need to use millis() but you can if you really want to.

    Use something like, assumming you have LED(via a suitable resistor) and a Switch.

    #define InputSwitch porta.1
    #define LED porta.2
    
    dir InputSwitch in
    dir LED out
    
    LED = OFF
    do forever
            if InputSwitch = On then
                'debounce using WAIT
                 wait 10 ms
                  if InputSwitch = ON then
                                LED = ON
                 end if
              else
                                 LED = OFF
            end if
    loop
    
     
  • Alex

    Alex - 2020-02-25

    thanks for your reply, i have read in documentation that "wait" "cause the program to wait for either a specified amount of time..." i think it is like "delay" in arduino, but the reason i started to use millis is that i couldn´t make a multitask program using delay , i think i will not be able to make a multitask program using "wait"

     
  • Anobium

    Anobium - 2020-02-25

    OK. Wait is none blocking. Interupts will still fire etc.

    So, I get a better idea - what is the multitask program doing in the meanwhile.

    Do you want to detect the switch is down (in some on or off state) for a specific period of time?

     
  • Alex

    Alex - 2020-02-25

    the mulstitask is for a conveyor that counts every object that touches a limit switch, conveyor doesn´t have sensor just switch, when the limit switch counts an object also activates a servo motor arm that pulls the object out of the conveyor into a box also when the servo is working a green light is blinking every 1 second, i was thinking working with the limit switch counter first .

     
  • stan cartwright

    stan cartwright - 2020-02-25

    I have written a few games using gcb but never used debounce code for the button switches.
    They are just tied high via 10K resistors and when pressed drag the port low and I check if the port is off.
    Is that unusual? It has not caused a problem so far.
    edit, in the above application ie checking switches, the switches will be on and an object touching will set the switch low. One would only be testing for it to go low once then the next line , is it low then do something. By the time the program had looped to testing the switch again, it would be hi again.
    edit again- yes , I can see how when the program checks the switch it could be bouncing and be hi when it was being pressed. Hmmmm, something I must consider in future.
    An on interrupt by the switch would involve some sort of code or there would be lots of interrupts.

     

    Last edit: stan cartwright 2020-02-25
  • Anobium

    Anobium - 2020-02-25

    @Alex. No really a debounce.

    Do you need know the switch is down for a specific time? Then, store the millis() and compare when millis()+your_delay has elapsed and make sure the switch is still down. Is this what you need?

     
  • Alex

    Alex - 2020-02-25

    thanks for your reply, yes, i need to make sure that the button(limit switch) has been pressed (set ON)for 50 ms (time enoguh to avoid debounce) after that my microcontroller counts 1 object and repeat again(comes another object button is pressed, after 50ms another object is counted)

     
  • Anobium

    Anobium - 2020-02-25

    OK. You need to save the millis() at the first event, and, then do other stuff, and then check the first event+10ms has happened then check again that is switch is still down.

     
  • Alex

    Alex - 2020-02-25

    i think i can store 50ms like this:

    #chip mega328p, 16
    #include <millis.h>
    #define button portc.0
    #define interval 50 ;50ms debounce
    
    
    #define usart_baud_rate 9600
    #define usart_tx_blocking
    
    dim currentmillis as Word
    dim lastmillis as Word
    
    dim num as byte
    num=0
    
    dir button in
    
    do
    currentmillis=millis()
    
    if currentmillis - lastmillis > interval  then
        lastmillis=currentmillis
    end if
    loop
    
     

    Last edit: Alex 2020-02-25
  • Anobium

    Anobium - 2020-02-26

    Should work but you code is not checking the switch. :-)

     
    • Alex

      Alex - 2020-02-26

      thanks for your reply, that is where i get stuck

       
  • stan cartwright

    stan cartwright - 2020-02-26

    Could the 328p use on interrupt to set a var if the switch was pressed?
    Then you would only need to test the var and reset it if true, not debounce.

     
    • Alex

      Alex - 2020-02-26

      thanks for your reply, could you give an example please for a counter?

       
  • Anobium

    Anobium - 2020-02-26

    What are you counting? Time? Number of times the switch is low (or high).

    I recommend you flesh out the process else this will be coding by guessing (by us)

     
    • Alex

      Alex - 2020-02-27

      i am counting every objet that a limit switch detects, i have read that mechanical switches bounce everytime they are pressed, for a small period of time, every bounce could be read by the microcontrollers, so, to avoid that you can use a software debounce,

       
  • stan cartwright

    stan cartwright - 2020-02-26

    I hope I'm not upsetting anyone by pokeing my nose in.
    If the program is testing various switches sequencionally then there's a chance of testing a switch at the time it was activated but bouncing so giving a false result.
    pressed =0:unpressed=0
    do
    if switch=0 then pressed++
    else
    unpressed++
    end if
    until pressed>unpressed

    I am responding to millis alternative as in a previous post. Things one takes for granted like debounce.

    I don't like my programs wasting time so a fast result is needed....but there's time to waste on some programs.

    I just trying to make debounce routine and lots of syntax errors like
    do
    if var=x then
    var2++
    exit do
    end if
    loop

    is that ok?

     

    Last edit: stan cartwright 2020-02-26
  • David Stephenson

    Just as an alternate view. I always debounce in hardware that is I have a resistor (in series) and a capacitor (in parallel). If there is a problem with the long reset I set input pin to ouput and discharge the capacitor in the interrupt routine.

     

    Last edit: David Stephenson 2020-02-27
    • Alex

      Alex - 2020-02-27

      great idea i could test that, thanks a lot

       
    • stan cartwright

      stan cartwright - 2020-02-27

      Like this?
      Does it stress the cap or switch?

       
  • Alex

    Alex - 2020-02-27

    thanks, all for your reply i´ll try

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.