<?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>Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/105387/read-and-count-only-when-button-is-pressed</link><description>Hello, 
 
 Trying to use a code from the DevAcademy on buttons below but having a bit of trouble with modifying it. 
 My aim is a counter that reads how many buttons pressed I made so far but it seems to count indefinitely. 
 May I ask assistance with</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Nov 2023 10:40:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/105387/read-and-count-only-when-button-is-pressed" /><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/455093?ContentTypeID=1</link><pubDate>Fri, 10 Nov 2023 10:40:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b7e1b76-49fa-4656-ba4d-e95d6854a8bf</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;See &lt;a href="https://www.zephyrproject.org/how-to-debounce-button-inputs-in-a-rtos/"&gt;this &lt;/a&gt;article and the following &lt;a href="https://github.com/ubieda/zephyr_button_debouncing"&gt;sample&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454984?ContentTypeID=1</link><pubDate>Thu, 09 Nov 2023 16:10:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5a9d0cc3-73ad-46c1-96da-e9a520f019f1</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;Can you provide a lead on how to do that?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454976?ContentTypeID=1</link><pubDate>Thu, 09 Nov 2023 15:48:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7d1d7bfc-d6f2-4233-a864-7ce53afa389f</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;I think that is because of debounce on the button. You can start a timer that prevents any incrementation for short while after a button has been pressed.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454968?ContentTypeID=1</link><pubDate>Thu, 09 Nov 2023 15:32:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd8d3878-8a6f-4b7f-bb86-88f8c579e336</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;I think one thing I noticed is that it &amp;quot;slips&amp;quot; like sometimes the count does twice or thrice at times.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Is there a way to prevent that?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454863?ContentTypeID=1</link><pubDate>Thu, 09 Nov 2023 10:05:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9926449d-faa2-4636-a9b6-bee9142fa5f6</guid><dc:creator>Jared</dc:creator><description>[quote user="al_ns"]Wait, that works![/quote]
&lt;p&gt;Good!&lt;/p&gt;
[quote user="al_ns"]but what does &lt;span&gt;GPIO_INT_EDGE_TO_INACTIVE&lt;/span&gt; in a nutshell?[/quote]
&lt;p&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/hardware/peripherals/gpio.html#c.GPIO_INT_EDGE_TO_INACTIVE"&gt;Configures GPIO interrupt to be triggered on pin state change to logical level 0 and enables it.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454764?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 17:51:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dbacab50-b589-471d-83f3-ed0c9eef14ed</guid><dc:creator>al_ns</dc:creator><description>&lt;p&gt;Wait, that works!&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 * Note: 
 * Tested on nRF Connect SDK Version : 2.0
 */

#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/devicetree.h&amp;gt;
#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;

/* STEP 9 - Increase the sleep time from 100ms to 10 minutes  */
// #define SLEEP_TIME_MS   10*60*1000
#define SLEEP_TIME_MS   1

/* SW0_NODE is the devicetree node identifier for the node with alias &amp;quot;sw0&amp;quot; */
#define SW0_NODE	DT_ALIAS(sw0) 
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);

/* LED0_NODE is the devicetree node identifier for the node with alias &amp;quot;led0&amp;quot;. */
#define LED0_NODE	DT_ALIAS(led0)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

int count = 0;


/* STEP 4 - Define the callback function */
int button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    // gpio_pin_toggle_dt(&amp;amp;led);
	// if (gpio_pin_get_dt(&amp;amp;led)) {
	// 	return 1;
	// }
	// return 0;
	count++;
	printk(&amp;quot;%d\n&amp;quot;, count);
	return 0;
}
/* STEP 5 - Define a variable of type static struct gpio_callback */
static struct gpio_callback button_cb_data;

void main(void)
{
	int ret;
	

	if (!device_is_ready(led.port)) {
		return;
	}

	if (!device_is_ready(button.port)) {
		return;
	}

	ret = gpio_pin_configure_dt(&amp;amp;led, GPIO_OUTPUT_ACTIVE);
	if (ret &amp;lt; 0) {
		return;
	}

	ret = gpio_pin_configure_dt(&amp;amp;button, GPIO_INPUT);
	if (ret &amp;lt; 0) {
		return;
	}
	/* STEP 3 - Configure the interrupt on the button&amp;#39;s pin */
	ret = gpio_pin_interrupt_configure_dt(&amp;amp;button, GPIO_INT_EDGE_TO_INACTIVE );

	/* STEP 6 - Initialize the static struct gpio_callback variable   */
    gpio_init_callback(&amp;amp;button_cb_data, button_pressed, BIT(button.pin)); 	
	
	/* STEP 7 - Add the callback function by calling gpio_add_callback()   */
	 gpio_add_callback(button.port, &amp;amp;button_cb_data);
	

	if (led.port) {
		while (1) {
			if (gpio_pin_get_dt(&amp;amp;button)==1) {
				
			}
			k_msleep(SLEEP_TIME_MS);
		}
	}
	// while (1) {
	// 	/* STEP 8 - Remove the polling code */

    //     k_msleep(SLEEP_TIME_MS);
	// }

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;but what does &lt;span&gt;GPIO_INT_EDGE_TO_INACTIVE&lt;/span&gt; in a nutshell?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read and count only when button is pressed</title><link>https://devzone.nordicsemi.com/thread/454149?ContentTypeID=1</link><pubDate>Mon, 06 Nov 2023 10:17:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a6a746ad-2ef8-4cf6-abcc-ec1110493e08</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;As the code is written now, count will be incremented as long the button is pressed. What you want instead, is that count to be incremented only once per button press.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You have already configured a GPIO event to be t&lt;span&gt;riggered on pin state change to logical level 1. This means that every time the GPIO pin is changed to a logical 1 the callback handler&amp;nbsp;will be called. You should therefore move the count ++ line to the&amp;nbsp;button_pressed callback handler. If you&amp;#39;re using the button on the devkit, then the pin will be at logical 1 when you release the button, if you want the callback handler to be called once you push the button and not release, you have to change&amp;nbsp;GPIO_INT_EDGE_TO_ACTIVE to&amp;nbsp;GPIO_INT_EDGE_TO_INACTIVE.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Jared&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>