<?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>Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/82239/increment-counter-on-button-clicked</link><description>Hello everyone, i need to increment a counter each time my button on the nrf534DK is pressed i&amp;#39;m using sdk v 1.7.1 . is there a simple way to do it or a function which supports this do you have any example that can guide me through, so that i can implement</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 03 Dec 2021 11:28:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/82239/increment-counter-on-button-clicked" /><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341848?ContentTypeID=1</link><pubDate>Fri, 03 Dec 2021 11:28:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9657c37b-11b3-4bb1-b07f-9c15e6d1be35</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;thank you for you recommendation&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a class="internal-link view-user-profile" href="https://devzone.nordicsemi.com/members/haakonsh"&gt;haakonsh&lt;/a&gt;&lt;br /&gt;I made it through with your help&amp;nbsp;&lt;br /&gt;here&amp;#39;s my output from the result&amp;nbsp;&lt;br /&gt;thank you very much&amp;nbsp;&lt;br /&gt;very grateful&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;***************Button pressed at Number of cycle : 43
+++++++++++++++++Get BME280 data
temp: 21.210000; press: 100.595343; humidity: 39.749023
-----------------Get MPU6050 data
[0:02:54.717]:95158.9 Cel
  accel 210000.069375 94622.000021 210000.000100 m/s/s
  gyro  595343.000039 0.000000 210000.069375 rad/s
***************Button pressed at Number of cycle : 44
+++++++++++++++++Get BME280 data
temp: 21.210000; press: 100.595343; humidity: 39.749023
-----------------Get MPU6050 data
[0:02:55.158]:95158.9 Cel
  accel 210000.069375 94622.000021 210000.000100 m/s/s
  gyro  595343.000039 0.000000 210000.069375 rad/s
***************Button pressed at Number of cycle : 45
+++++++++++++++++Get BME280 data
temp: 21.240000; press: 100.596410; humidity: 40.249023
-----------------Get MPU6050 data
[0:02:55.365]:95158.9 Cel
  accel 240000.069375 94622.000021 240000.000100 m/s/s
  gyro  596410.000040 0.000000 240000.069375 rad/s&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341808?ContentTypeID=1</link><pubDate>Fri, 03 Dec 2021 09:15:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3dee7cc7-83d0-4f75-9223-db07d49b5e7d</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Hey Rihab,&lt;/p&gt;
&lt;p&gt;You are very close, but you need to study the programming concept of scope and how it affects variables. See&amp;nbsp;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm"&gt;https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you want to access the counter variable outside the button_pressed function you need to declare it as a global variable.&lt;/p&gt;
&lt;p&gt;or, if you only want to access the counter variable inside the function, you need to declare it as a static variable in order to keep its state in memory each time the button_pressed function is called. See&amp;nbsp;&lt;a href="https://www.tutorialspoint.com/static-variables-in-c"&gt;https://www.tutorialspoint.com/static-variables-in-c&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341803?ContentTypeID=1</link><pubDate>Fri, 03 Dec 2021 08:57:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e9d7ac15-0676-413a-9a4d-e7347bf85dd6</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;someone can help me please&amp;nbsp;&lt;br /&gt;is anybody here&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341695?ContentTypeID=1</link><pubDate>Thu, 02 Dec 2021 12:37:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b9d6bfd3-cf8a-4782-b1a7-ab7947e1f99a</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;&lt;span&gt;Hello&amp;nbsp;Community&lt;br /&gt;&lt;/span&gt;&lt;span&gt;i&amp;#39;m currently trying to increment a counter every time the button is pressed&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;the counter is considered like the number of cycle&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;well i added some modification to my code to do this function&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;     int on_click = 0;
        while(gpio_pin_get_dt(&amp;amp;button) != true ) {
	    on_click++;
        printk(&amp;quot;Number of cycle : %d\n&amp;quot;, on_click);
        k_msleep(SLEEP_TIME_MS);
        }&lt;/pre&gt;&lt;br /&gt;this function is part of my project&amp;nbsp;&lt;br /&gt;and whole project role is :&lt;br /&gt;0-button pressed&lt;br /&gt;1-display BME280 values&lt;br /&gt;2-display MPU6050 values&lt;br /&gt;and now i&amp;#39;m trying to add the number of cycle which means technically how many times the button was pressed&amp;nbsp;&lt;br /&gt;when i try to view the result on my debug Terminal&lt;br /&gt;it only displays the number of cycle and it doesn&amp;#39;t display the values of the other 2 sensors&amp;nbsp;&lt;br /&gt;and then 5 sec later it stops and it displays the disassembly window on the right just like below&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/0358.pastedimage1638373411909v1.png" /&gt;&lt;br /&gt;can anyone help me fix this please&amp;nbsp;&lt;br /&gt;&lt;br /&gt;please if you know what is causing this tell me as soon as you can&amp;nbsp;&lt;br /&gt;thank you in advance&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Kindly,&lt;br /&gt;Rihab&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341287?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 12:49:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9b20f4e-5d3f-4e7e-a57a-b2aaa21c77a9</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;i didn&amp;#39;t really understand,&lt;br /&gt;can you give me a little more details please&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341286?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 12:45:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac844bca-8146-4cec-b641-66c918b051d2</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Then you need to declare a global variable and increment that instead of/in addition to incrementing the global variable.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341272?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 12:26:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eea1ee04-3b06-452d-9699-8e748a8ff0ee</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;for now the button is returning the hardware clock up-counter&amp;nbsp;&lt;br /&gt;so i&amp;#39;m aware of when it&amp;#39;s pressed&amp;nbsp;&lt;br /&gt;i think a software implementation will suffice&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341271?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 12:24:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d43d30d-ef70-4aa8-a18e-f673c4ebdd83</guid><dc:creator>Rihab</dc:creator><description>&lt;p&gt;i already have the hardware button i need to make a counter each time it&amp;#39;s pressed&amp;nbsp;&lt;br /&gt;to reply to your question i think i need both&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Increment Counter on Button Clicked</title><link>https://devzone.nordicsemi.com/thread/341262?ContentTypeID=1</link><pubDate>Tue, 30 Nov 2021 12:12:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b8dff739-bfe1-4804-a30f-70cc3997f9b8</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Do you need a HW counter or will a SW counter suffice?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>