<?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>Blink LED on button press on nRF9160dk</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/54146/blink-led-on-button-press-on-nrf9160dk</link><description>Hello, 
 I want to blink the LED by pressing button on nRF9160dk. Is there any sample available to start with? 
 I started with button demo but on button press there is no output. I saw examples for nRF51/52. But I couldn&amp;#39;t find for nRF9160dk. 
 
 Thank</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 08 Nov 2019 12:43:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/54146/blink-led-on-button-press-on-nrf9160dk" /><item><title>RE: Blink LED on button press on nRF9160dk</title><link>https://devzone.nordicsemi.com/thread/219287?ContentTypeID=1</link><pubDate>Fri, 08 Nov 2019 12:43:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4676ffa1-2235-4112-b266-73e790293b95</guid><dc:creator>Jagruti</dc:creator><description>&lt;p&gt;Thank you so much. It is working.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Blink LED on button press on nRF9160dk</title><link>https://devzone.nordicsemi.com/thread/219276?ContentTypeID=1</link><pubDate>Fri, 08 Nov 2019 12:24:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:627ee410-fe0b-4cb1-84bc-2ea14f01ab8a</guid><dc:creator>MJD093</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;I reviewed your code and found a couple of issues.&lt;/p&gt;
&lt;p&gt;Firstly, read the pin at the ISR point and not constantly in main, it&amp;#39;s a quick pin level check and can be performed very fast. You only need to spend time reading the pin when something changes or set a boolean flag in the ISR when it is triggered that can activate code in main to read the button state and set the LED.&lt;/p&gt;
&lt;p&gt;Secondly, the pin options used were not correct. To trigger an ISR the rising &lt;span style="text-decoration:underline;"&gt;&lt;strong&gt;and&lt;/strong&gt;&lt;/span&gt; falling edge of a pin, you need to use the option &lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/919640cdd332d64a0694fc686027d1e919f30ce1/include/dt-bindings/gpio/gpio.h#L52"&gt;GPIO_INT_DOUBLE_EDGE&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thirdly, your ISR wasn&amp;#39;t actually reading the pin state, your if statement was set to if (BUTTON_PIN) not if (BUTTON_STATE). You were always going to enter the &amp;#39;true&amp;#39; state of the if statement. I have added in the pin reading elements that are running in main to your ISR.&lt;/p&gt;
&lt;p&gt;A few additional points,&lt;/p&gt;
&lt;p&gt;Consider placing your LED and Button init codes in a seperate function and calling them at the start of main.&lt;/p&gt;
&lt;p&gt;The Struct Device for the LEDs is being called every time you run the ISR, remove it from here and only init the struct once and make it callable by placing the define for the LED device struct outside of main.&lt;/p&gt;
&lt;p&gt;Consider using the option GPIO_INT_DEBOUNCE on buttons to counter the bouncing effect of buttons.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;gpio.h&amp;gt;
#include &amp;lt;misc/util.h&amp;gt;
#include &amp;lt;misc/printk.h&amp;gt;

#define LED_PORT LED1_GPIO_CONTROLLER
#define LED	LED1_GPIO_PIN

/* change this to use another GPIO port */
#ifndef SW1_GPIO_CONTROLLER
#ifdef SW1_GPIO_NAME
#define SW1_GPIO_CONTROLLER SW1_GPIO_NAME
#else
#error SW1_GPIO_NAME or SW1_GPIO_CONTROLLER needs to be set in board.h
#endif
#endif
#define PORT	SW1_GPIO_CONTROLLER

/* change this to use another GPIO pin */
#ifdef SW1_GPIO_PIN
#define PIN     SW1_GPIO_PIN
#else
#error SW1_GPIO_PIN needs to be set in board.h
#endif

/* change to use another GPIO pin interrupt config */
#ifdef SW1_GPIO_FLAGS
#define EDGE    (SW1_GPIO_FLAGS | GPIO_INT_EDGE)
#else
/*
 * If SW0_GPIO_FLAGS not defined used default EDGE value.
 * Change this to use a different interrupt trigger
 */
#define EDGE    (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)
#endif

/* change this to enable pull-up/pull-down */
#ifndef SW1_GPIO_FLAGS
#ifdef SW1_GPIO_PIN_PUD
#define SW1_GPIO_FLAGS SW1_GPIO_PIN_PUD
#else
#define SW1_GPIO_FLAGS 0
#endif
#endif
#define PULL_UP SW1_GPIO_FLAGS

/* Sleep time */
#define SLEEP_TIME	500


void button_pressed(struct device *gpiob, struct gpio_callback *cb,
		    u32_t pins)
{
        printk(&amp;quot;Button pressed\n&amp;quot;);
        struct device *dev;

	dev = device_get_binding(LED_PORT);
        gpio_pin_configure(dev, LED, GPIO_DIR_OUT);

				u32_t val = 0U;

				gpio_pin_read(gpiob, PIN, &amp;amp;val);

        if(val == 0)
        {
            gpio_pin_write(dev, LED, 1);
        }
        else if (val == 1)
        {
            gpio_pin_write(dev, LED, 0);
        }
}

static struct gpio_callback gpio_cb;

void main(void)
{
	struct device *gpiob;

	printk(&amp;quot;Press the user defined button on the board\n&amp;quot;);
	gpiob = device_get_binding(PORT);
	if (!gpiob) {
		printk(&amp;quot;error\n&amp;quot;);
		return;
	}


	gpio_pin_configure(gpiob, PIN,
			   GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE | GPIO_INT_DOUBLE_EDGE | GPIO_INT_ACTIVE_LOW | GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE);

	gpio_init_callback(&amp;amp;gpio_cb, button_pressed, BIT(PIN));

	gpio_add_callback(gpiob, &amp;amp;gpio_cb);
	gpio_pin_enable_callback(gpiob, PIN);

	while (1) {
		u32_t val = 0U;

		gpio_pin_read(gpiob, PIN, &amp;amp;val);
		k_sleep(SLEEP_TIME);
	}
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Blink LED on button press on nRF9160dk</title><link>https://devzone.nordicsemi.com/thread/219257?ContentTypeID=1</link><pubDate>Fri, 08 Nov 2019 11:14:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ab91d0a5-4a96-4061-a6bc-45c2fbcb7a34</guid><dc:creator>Jagruti</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thank you for the response, it is working.&lt;/p&gt;
&lt;p&gt;I am using push button 2 and LED 2. When I press the button, LED glows continuously. I want a function in which when I release the button, LED stops glowing. I have tried it, but it is not working. What should I do?&lt;/p&gt;
&lt;p&gt;I will paste the code.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;gpio.h&amp;gt;
#include &amp;lt;misc/util.h&amp;gt;
#include &amp;lt;misc/printk.h&amp;gt;

#define LED_PORT LED1_GPIO_CONTROLLER
#define LED	LED1_GPIO_PIN

/* change this to use another GPIO port */
#ifndef SW1_GPIO_CONTROLLER
#ifdef SW1_GPIO_NAME
#define SW1_GPIO_CONTROLLER SW1_GPIO_NAME
#else
#error SW1_GPIO_NAME or SW1_GPIO_CONTROLLER needs to be set in board.h
#endif
#endif
#define PORT	SW1_GPIO_CONTROLLER

/* change this to use another GPIO pin */
#ifdef SW1_GPIO_PIN
#define PIN     SW1_GPIO_PIN
#else
#error SW1_GPIO_PIN needs to be set in board.h
#endif

/* change to use another GPIO pin interrupt config */
#ifdef SW1_GPIO_FLAGS
#define EDGE    (SW1_GPIO_FLAGS | GPIO_INT_EDGE)
#else
/*
 * If SW0_GPIO_FLAGS not defined used default EDGE value.
 * Change this to use a different interrupt trigger
 */
#define EDGE    (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)
#endif

/* change this to enable pull-up/pull-down */
#ifndef SW1_GPIO_FLAGS
#ifdef SW1_GPIO_PIN_PUD
#define SW1_GPIO_FLAGS SW1_GPIO_PIN_PUD
#else
#define SW1_GPIO_FLAGS 0
#endif
#endif
#define PULL_UP SW1_GPIO_FLAGS

/* Sleep time */
#define SLEEP_TIME	500


void button_pressed(struct device *gpiob, struct gpio_callback *cb,
		    u32_t pins)
{
        printk(&amp;quot;Button pressed\n&amp;quot;);
        struct device *dev;

	dev = device_get_binding(LED_PORT);
        gpio_pin_configure(dev, LED, GPIO_DIR_OUT);
        if(PIN)
        {
            gpio_pin_write(dev, LED, 1);
        }
        else
        {
            gpio_pin_write(dev, LED, 0);
        }
}

static struct gpio_callback gpio_cb;

void main(void)
{
	struct device *gpiob;

	printk(&amp;quot;Press the user defined button on the board\n&amp;quot;);
	gpiob = device_get_binding(PORT);
	if (!gpiob) {
		printk(&amp;quot;error\n&amp;quot;);
		return;
	}


	gpio_pin_configure(gpiob, PIN,
			   GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE | GPIO_INT_ACTIVE_LOW | GPIO_INT_ACTIVE_HIGH);

	gpio_init_callback(&amp;amp;gpio_cb, button_pressed, BIT(PIN));

	gpio_add_callback(gpiob, &amp;amp;gpio_cb);
	gpio_pin_enable_callback(gpiob, PIN);

	while (1) {
		u32_t val = 0U;

		gpio_pin_read(gpiob, PIN, &amp;amp;val);
		k_sleep(SLEEP_TIME);
	}
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Blink LED on button press on nRF9160dk</title><link>https://devzone.nordicsemi.com/thread/219110?ContentTypeID=1</link><pubDate>Thu, 07 Nov 2019 15:00:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd1502b1-cc0f-4748-a2e7-9b85d819aa46</guid><dc:creator>MJD093</dc:creator><description>&lt;p&gt;In the Zephyr repository, (/path/to/ncs/zephyr) there are sample applications in the sample folder.&lt;/p&gt;
&lt;p&gt;If you enter the /basic/ folder in /samples/, you can find the blinky and button example applications. If you have a play with both of them individually, you can then combine them to create a callback function that when the button is pressed, it flips the output on the LED. (See the button example for how the interrupt callback works)&lt;/p&gt;
&lt;p&gt;Once you have that down, you can try setting the button GPIO options to detect both falling and rising edges and then you can create an application that can set the LED output based on the current state of the button.&lt;/p&gt;
&lt;p&gt;If you are unsure about any of the functions or what&amp;#39;s happening. The Zephyr documentation can help shed light on what everything is doing. &lt;a href="https://docs.zephyrproject.org/latest/"&gt;docs.zephyrproject.org/.../&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>