Read and count only when button is pressed

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 it?

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
* Note:
* Tested on nRF Connect SDK Version : 2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
/* 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 "sw0" */
#define SW0_NODE DT_ALIAS(sw0)
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Board: nrf52840dk

Parents
  • Hi,

    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. 

    You have already configured a GPIO event to be triggered 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 will be called. You should therefore move the count ++ line to the button_pressed callback handler. If you'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 GPIO_INT_EDGE_TO_ACTIVE to GPIO_INT_EDGE_TO_INACTIVE. 

    regards

    Jared 

  • Wait, that works!

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /*
    * Copyright (c) 2016 Intel Corporation
    *
    * SPDX-License-Identifier: Apache-2.0
    * Note:
    * Tested on nRF Connect SDK Version : 2.0
    */
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    /* 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 "sw0" */
    #define SW0_NODE DT_ALIAS(sw0)
    static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    but what does GPIO_INT_EDGE_TO_INACTIVE in a nutshell?

Reply
  • Wait, that works!

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    /*
    * Copyright (c) 2016 Intel Corporation
    *
    * SPDX-License-Identifier: Apache-2.0
    * Note:
    * Tested on nRF Connect SDK Version : 2.0
    */
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    /* 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 "sw0" */
    #define SW0_NODE DT_ALIAS(sw0)
    static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    but what does GPIO_INT_EDGE_TO_INACTIVE in a nutshell?

Children