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

Add a supplemetnary button and led on Blinky exemple

Hello, we have a project on: Periperal: experimental ble_app_blinky / and Central: experimental ble_app_blinky c We have add suplementary:
LEDBUTTON_LED4 ( BSP_BOARD_LED_3) LEDBUTTON_BUTTON2_PIN" (BSP_BUTTON_1) For the moment when we push "button2" or "button1" on the peripheral, everytime the same "led 3" is toggle on the central. How and where we must do the assignments that when we push "button 2" on peripheral the led 4 is toggle on central, and when we push "button1" on peripheral the led 3 is toggel on central? (And where we must do the assigment reverse from central to peripheral)

We are not so good in programming. Can you please send us the code. Thank you very much. Best regards MC

Ist maybe her to ad a code?

case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:
    {
        NRF_LOG_INFO("Button state changed on peer to 0x%x.\r\n", p_lbs_c_evt->params.button.button_state);
        if (p_lbs_c_evt->params.button.button_state)
        {
						bsp_board_led_on(LEDBUTTON_LED);
						
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED);
        }
    } break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION
  • Hello mc

    There are a few ways you can achieve this. You could create a new characteristic for the second button, or you could use the one already there. I threw together a quick solution using the characteristic already present. Do note this solution may not be the optimal way of doing it, it's just for guidance.

    The way the blinky example works at the peripheral is that GPIOTE generates an interrupt, this identifies it as a port event, then calls the button handler in the app_buttons module, which creates a variable called transition, which is passed on to the button_event_handler in main.c. The transition variable is set to 1 if the button transition led to the pins "active state", and zero otherwise. The active state is defined when calling app_buttons_init, in the buttons_init function in main. In the example the active state attribute is set to "false" which corresponds to 0.

    The button_event_handler then calls the ble_lbs_on_button_change function which sends the transition value to the client.

    The changes I made here defines a new button, LEDBUTTON_BUTTON_PIN2, and registers this with the app_button module in buttons_init. The button config now looks like this

    static app_button_cfg_t buttons[] =
    {
        {LEDBUTTON_BUTTON_PIN, false, BUTTON_PULL, button_event_handler},
        {LEDBUTTON_BUTTON_PIN2, false, BUTTON_PULL, button_event_handler}
    };
    

    I then altered the switch in button_event_handler

    switch (pin_no)
    {
        case LEDBUTTON_BUTTON_PIN:
            NRF_LOG_INFO("Send button1 state change.\r\n");            
            err_code = button_action ? ble_lbs_on_button_change(&m_lbs, 1) : ble_lbs_on_button_change(&m_lbs, 0);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
    					
    	case LEDBUTTON_BUTTON_PIN2:
    		NRF_LOG_INFO("Send button2 state change.\r\n");            
    		err_code = button_action ? ble_lbs_on_button_change(&m_lbs, 3) : ble_lbs_on_button_change(&m_lbs, 2);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
    				break;
    
        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
    

    This sends a 0 or 1 if the first button is released/pressed, and a 2 or 3 if the second button is released/pressed.

    On the client side you define a second LED, and then alter the following case in lbs_c_evt_handler in main

    case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:
            {
                NRF_LOG_INFO("Button state changed on peer to 0x%x.\r\n", p_lbs_c_evt->params.button.button_state);
                if (p_lbs_c_evt->params.button.button_state==0x1)
                {
                    bsp_board_led_on(LEDBUTTON_LED);
                }
                else if(p_lbs_c_evt->params.button.button_state==0x0)
                {
                    bsp_board_led_off(LEDBUTTON_LED);
                }
                else if(p_lbs_c_evt->params.button.button_state==0x3)
                {
                    bsp_board_led_on(LEDBUTTON_LED2);
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED2);
                }						
            } break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION
    

    This toggles the correct led depending on what value was received from the server.

    You will need to add similar changes (but reverse roles) for an extra button at the central.

    Best regards

    Jørn Frøysa

  • Hello Jorn Froysa

    Thank you very much!!! Now we can push the button on the peripheral and the correct leds toggel on the central!

    To do it from central to the peripheral we have a problem: We not see the "case BLE_LBS_C_EVT_BUTTON_NOTIFICATION" in the peripheral. We see only the "led write handler" What we have to change there?

    /**@brief Function for handling write events to the LED characteristic.
    
    • @param[in] p_lbs Instance of LED Button Service to which the write applies.

    • @param[in] led_state Written/desired state of the LED. */ static void led_write_handler(ble_lbs_t * p_lbs, uint8_t led_state) { if (led_state)

      {

       bsp_board_led_on(LEDBUTTON_LED_PIN);
       	  NRF_LOG_INFO("Received LED ON!\r\n");
       	
      

      } else { bsp_board_led_off(LEDBUTTON_LED_PIN); NRF_LOG_INFO("Received LED OFF!\r\n"); }

    }

    Can you plase send us the modification? We would very apppreciate! Best regards MC

  • Hi Jorn

    After making above changes, If i press button_1 led_1 will glow and if i press button_2 then both leds led1 and led2 will glow

    i am not getting why led1 is glowing after pressing button2. please help

    board used :- nrf52_pca10040

    sdk :- 11.0

Related