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