<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Debounce in callbacks</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/83636/debounce-in-callbacks</link><description>Hello! I currently have an external button and LED connected to the Icarus IoT dev board. For the time being, I just want to turn on the LED in the callback function when I press the button. However, the button (and the board) is highly sensitive and</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 04 Feb 2022 01:13:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/83636/debounce-in-callbacks" /><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/351219?ContentTypeID=1</link><pubDate>Fri, 04 Feb 2022 01:13:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f63bbe5-074a-4244-aef5-a0670c0ad7b5</guid><dc:creator>mike_actinius</dc:creator><description>&lt;p&gt;Hello Baowz,&lt;/p&gt;
&lt;p&gt;A device tree declaration will not initialize a pull-up resistor, you have to do it in the gpio_pin_configure(). If you want to use a similar way with what we do in the &amp;quot;leds &amp;amp; button&amp;quot; sample, then you should declare an extra button in your overlay (you can copy paste the button declaration from the icarus board files and change the gpio to pin 0 where you seem to have your external button connected) and just initialize the gpio the same way&amp;nbsp;it is done for the on-board button in the sample, i.e. using the device tree flags in gpio_pin_configure().&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/350711?ContentTypeID=1</link><pubDate>Tue, 01 Feb 2022 14:31:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89e6dda8-72f8-46cf-9046-e4760ecb6548</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;Hello again, Mike!&lt;br /&gt;&lt;br /&gt;The code works perfectly, thank you so much for the help!&lt;br /&gt;&lt;br /&gt;Just a quick question when it comes to enabling the resistor in the GPIO. Should I do it the same way as I did with the button? I used the sample that is shown from your website found &lt;a href="https://docs.actinius.com/icarus/samples/leds-and-button"&gt;here&lt;/a&gt;. And the way I tried to&amp;nbsp;initialize the resistor is as follows:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;...   
    resistors {
        TV_resistor: resistor0 {
            gpios = &amp;lt;&amp;amp;gpio0 0 GPIO_PULL_UP&amp;gt;;
            label = &amp;quot;Video resistor&amp;quot;;
        };
    };
	aliases {
	   ...
	   resistor0 = &amp;amp;TV_resistor;
    };
...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/350540?ContentTypeID=1</link><pubDate>Mon, 31 Jan 2022 22:29:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a5a2d4e-176f-4f04-9cf8-b70d2cfb9acb</guid><dc:creator>mike_actinius</dc:creator><description>&lt;p&gt;Hi Baowz,&lt;/p&gt;
&lt;p&gt;You should declare the `last_button_press_time` variable outside the function, I edited my answer above to show you how the `last_button_press_time` and `BUTTON_DEBOUNCE_TIME` can be declared.&lt;/p&gt;
&lt;p&gt;The `newPressTime` is just used so that the &amp;quot;entry&amp;quot; time is used as a last_button_press_time, you can even skip it, and put the uptime directly in the `last_button_press_time` if you prefer.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/350461?ContentTypeID=1</link><pubDate>Mon, 31 Jan 2022 14:31:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5afec984-b462-4174-904d-c0c119d077d5</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;This seems to be a really good solution, thank you! However, I&amp;#39;m a little bit confused by the code. You say that you want to store it in a variable called &amp;quot;last_button_press_time&amp;quot;. But it ends up being stored in &amp;quot;newPressTime&amp;quot; and &amp;quot;last_button_press_time&amp;quot; is never declared. I feel like I&amp;#39;m missing something from your&amp;nbsp;explanation, I&amp;#39;m sorry about that. But can you elaborate a little bit more for me?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/350294?ContentTypeID=1</link><pubDate>Fri, 28 Jan 2022 23:52:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57547071-5fd6-4ec9-9491-4744b1e97bc7</guid><dc:creator>mike_actinius</dc:creator><description>&lt;p&gt;Hi Baowz,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;For debouncing a button, you can simply keep the uptime value at the moment of the button callback (eg&amp;nbsp;k_uptime_get_32()) in a variable, let&amp;#39;s call it&amp;nbsp;last_button_press_time, and in your callback do something like the following:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define BUTTON_DEBOUNCE_TIME 50
uint32_t last_button_press_time;

void button_pressed_callback(...) {
    uint32_t newPressTime = k_uptime_get_32();

    // debounce
    if (k_uptime_get_32() - last_button_press_time &amp;lt; BUTTON_DEBOUNCE_TIME) {
        return;
    }

    is_button_press_pending = true;
    // use (and re-set) the `is_button_press_pending` flag in your main loop
    // or submit work to queue at this point

    last_button_press_time = newPressTime;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Also, it sounds like you may not have the internal pull-up resistor active on your input, making your pin floating and triggering &amp;quot;too easily&amp;quot;. Make sure to turn it on when you initialize your GPIO.&lt;/p&gt;
&lt;p&gt;Let me know in case of any questions!&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348681?ContentTypeID=1</link><pubDate>Thu, 20 Jan 2022 11:37:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2bf9467-227d-43ac-be7e-79aaed4e9bc3</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&lt;/p&gt;
&lt;p&gt;Debounce need dealy, it is not wise to introduce significant dealy on a call back in the interrupt handler.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Maybe you can also think about a hardware solution?&lt;/p&gt;
&lt;p&gt;I am not sure if you have seen similar complaints on the&amp;nbsp;&lt;span&gt;board you are using.&amp;nbsp;&lt;/span&gt;&lt;span style="font-family:inherit;"&gt;Do you have any chance to play with the Nordic nRF DK board? The combination of the HW+FW solution could be a good reference for your development.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348544?ContentTypeID=1</link><pubDate>Wed, 19 Jan 2022 14:28:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5aa807c-ad89-406f-8574-f3444652d40b</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;Hello again, Charlie!&lt;br /&gt;&lt;br /&gt;Before I made this post I was looking around for some suitable debounce code. However, I found none that was implemented directly into the callback, hence this post. Is there a reason why I can&amp;#39;t find any that suits my use?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348297?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 13:26:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d91a3407-2883-46b5-bac4-8cc87571d478</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This sample does not support&amp;nbsp;&lt;span&gt;actinius Icarus board and it also has big differences with our supported nRF DK boards, so it will take some effort to modify the sample to fit this board.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I suggest you either switch to nRF9160DK to continue the learning of CAF or just search &amp;quot;debounce code in c&amp;quot; to quickly catch some debouncing implementation for your application. The general theory is that&lt;/span&gt;&lt;span&gt; on edge detection wait for several ms and detect edge again and if the change is still present then it is a true change. The button.c from CAF&amp;nbsp;set debounce interval&amp;nbsp;&lt;/span&gt;CAF_BUTTONS_DEBOUNCE_INTERVAL 2ms as default.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Charlie&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348279?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 12:37:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a2bf569a-5b97-4d74-83ec-5b4c8e361273</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;I do not recall making any changes except for changing the pin numbers that fits the board. Have you tried to compile the code for actinius_Icarus_ns? Since that&amp;#39;s the board I&amp;#39;m using and not the nrf9160dk. Not sure if that makes any difference?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348229?ContentTypeID=1</link><pubDate>Tue, 18 Jan 2022 10:16:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:95f91599-e7cc-4365-abd4-1ca72d5a9e48</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&lt;/p&gt;
&lt;p&gt;I did not encounter this error after adding these files for the nrf9160dk_nrf9160_ns building with the original Common Application Framework preview sample.&lt;/p&gt;
&lt;p&gt;Did you make any changes?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348083?ContentTypeID=1</link><pubDate>Mon, 17 Jan 2022 14:26:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:751e189a-536f-47cd-a0ae-ef71740fb1e4</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;Hello again!&lt;br /&gt;&lt;br /&gt;Thank you for the response.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m currently using the Icarus IoT dev board with the nRF9160 chip. I have already implemented the buttons.c, buttons_def.h, and the overlay. I&amp;#39;m curious if the led_state_def.h is necessary? I have not implemented it and is that why I get the error in the &amp;quot;module_set_state&amp;quot; function? This function is declared in the &amp;quot;module_state_event.h&amp;quot; file.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/348038?ContentTypeID=1</link><pubDate>Mon, 17 Jan 2022 12:53:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6bad80ac-02a4-46b5-b9af-cd3edfac5e3a</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You need to add the following configuration files to add support for nrf9160dk_nrf9160_ns building:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;boards\nrf9160dk_nrf9160_ns.overlay

configuration\nrf9160dk_nrf9160_ns\led_state_def.h

configuration\nrf9160dk_nrf9160_ns\buttons_def.h&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;You can copy these files from nrf5340dk_nrf5340_cpuapp and refer to &lt;a href="https://infocenter.nordicsemi.com/topic/ug_nrf91_dk/UG/nrf91_DK/hw_buttons_leds.html"&gt;the nRF9160DK&amp;nbsp;Buttons, slide switches, and LEDs pins assignment&lt;/a&gt;&amp;nbsp;to modify them.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/347836?ContentTypeID=1</link><pubDate>Fri, 14 Jan 2022 17:27:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf535dfb-464f-4a0a-bd0e-171c05477c3a</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;It does not seem like the sample works for nRF9160 I keep getting an error in the &amp;quot;&amp;nbsp;module_set_state&amp;quot; function.&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;undefined reference to &amp;#39;__event_type_module_state_event&amp;#39;&lt;/pre&gt;&lt;br /&gt;So it seems I have to make one from scratch then?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/347767?ContentTypeID=1</link><pubDate>Fri, 14 Jan 2022 12:58:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0dd164c0-229e-43f5-9884-f21f3ae79a1e</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&lt;/p&gt;
&lt;p&gt;The sample may be not written and tested with nRF9160DK board, but I am pretty sure CAF can be used by nRF9160 SiP.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/347748?ContentTypeID=1</link><pubDate>Fri, 14 Jan 2022 11:47:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5d0114f-6e36-4c95-b99f-8bf3ebc3491f</guid><dc:creator>Baowz</dc:creator><description>&lt;p&gt;Hello, Charlie! thank you for your reply.&lt;br /&gt;&lt;br /&gt;I see in the CAF sample you sent me, in the &amp;quot;requirements&amp;quot; it only supports nRF52840 and&amp;nbsp;nRF5340. The board that I have is using nRF9160, do you think it will work anyway?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Debounce in callbacks</title><link>https://devzone.nordicsemi.com/thread/347723?ContentTypeID=1</link><pubDate>Fri, 14 Jan 2022 10:25:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3bca2c5-8bd2-4164-becd-74acf8d1a41a</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Christian,&lt;/p&gt;
&lt;p&gt;Thanks for checking with us.&amp;nbsp;Our &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.8.0/nrf/libraries/caf/buttons.html"&gt;Common Application Framework Buttons module&lt;/a&gt;&amp;nbsp;actually has button debounce support, you can find the implementation from&amp;nbsp;nrf\subsys\caf\modules\buttons.c file.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For your development, you can either implement your own debounce function by referring to the buttons.c source codes or just learn how to use CAF.&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.8.0/nrf/samples/caf/README.html"&gt;Common Application Framework preview&lt;/a&gt;&amp;nbsp;sample could be a good start point.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Charlie&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>