This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Can't read a button state using: bsp_board_button_state_get()

Hi, i'm starting to develop on the nrf52840DK and trying to understand the Nordic SDK (using the lastest version at the time 15.3). Just edited the blinky example to read a button, if it's pressed a led goes on, my intention was to create the simplest program to achieve this but the led is always on and the button does nothing, my question is why?

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    /* Configure board. */
    bsp_board_init(BSP_INIT_LEDS || BSP_INIT_BUTTONS);
    bsp_board_led_off(3);

    while (true)
    {
        if (bsp_board_button_state_get(1)) {
              bsp_board_led_on(3);
        }     

    }
}

Parents Reply Children
  • Hi, i know that is a very inefficient program without the use of interrupts but i'm trying to use some basic and low level functions of the SDK an then go to interrupts, debounce, etc.

    I really can't understand what is wrong with the code above. The use of led functions work fine but buttons don't.

    Thanks for your answer.

  • Hello, 

    i'm trying to use some basic and low level functions of the SDK an then go to interrupts, debounce, etc.


    Ok, I see what you mean. I have a simple code, polling Button 1 for change, based on the blinky example and using the nrf_gpio library.

     * @defgroup blinky_example_main main.c
     * @{
     * @ingroup blinky_example
     * @brief Blinky Example Application main file.
     *
     * This file contains the source code for a sample application to blink LEDs.
     *
     */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "boards.h"
    #include "nrf_delay.h"
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        //Configure LED 3 as output
        nrf_gpio_cfg_output(LED_3);
        //Configure Button 2 as input, active low
        nrf_gpio_cfg_input(BUTTON_2, NRF_GPIO_PIN_PULLUP);
        while (1)
        {
            if (nrf_gpio_pin_read(BUTTON_2) == false)
            {
                nrf_gpio_pin_toggle(LED_3);
                //Debounce delay
                //nrf_delay_ms(100);
            }
        }
    }
    
    /**
     *@}
     **/

     

    David said:
    I really can't understand what is wrong with the code above

      The problem is the init function. When doing it like this, it works as intended and exactly the same as above:

    int main(void)
    {
        /* Configure board. */
        //bsp_board_init(BSP_INIT_LEDS && BSP_INIT_BUTTONS);
        bsp_board_init(BSP_INIT_LEDS);
        bsp_board_init(BSP_INIT_BUTTONS);
        bsp_board_led_off(3);
    
        while (true)
        {
            if (bsp_board_button_state_get(1)) {
                  bsp_board_led_invert(3);
            }     
    
        }
    }

  • The error in my original code was a C syntax error, i was using the logical operator "||" instead of the bitwise operator "|" for the init flags. A silly mistake i couldn't see.

    Thanks for your support, i like the DevZone.

Related