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

I want to know how to make LED waves using app_timer.

Hello

I'm working on sdk v17.
I want the LED to be printed out like a wave when I receive certain data from the app. (5 Times)

It is simple to use 'delay' and 'for' as the code below. But I can't do anything else during the delay. (I must be able to output sensor values, send and receive apps and data, etc.)

static void Left_move()
{
  printf("Left MOVE!\n");

  for(int i=0; i<5; i++) //5Times
  {
    nrf_gpio_pin_clear(BAT_LED_1); //LED off
    nrf_gpio_pin_clear(BAT_LED_2);
    nrf_gpio_pin_clear(BAT_LED_3);

    for(int pin=21; pin<25; pin++) //BAT_LED_1,2,3 (pin 22(?),23,24)
    {
      nrf_gpio_pin_set(pin); //led on
      nrf_delay_ms(500);
    }
}


static void nus_data_handler(ble_nus_evt_t * p_evt) //send to app
{
    uint8_t string_buffer[BLE_NUS_MAX_DATA_LEN+1];

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        memcpy(string_buffer, p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
        string_buffer[p_evt->params.rx_data.length] = 0;
 
        //Test Left/Right LED move
        if(strcmp("1", string_buffer) == 0)  //Left
        {
           //NRF_LOG_INFO("Left LED Moning");
           Left_move();
        } 
    }
}

Like this code, how do I use the app_timer to make the LEDs work in sequence at half a second intervals?

Is there anything to refer to?

Thank you in advance.

Parents
  • Hi,

    This should be a good starting point on how to use the application timer for your use case:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    
    #define MAX_LEDS 2
    APP_TIMER_DEF(m_repeated_timer_id);
    
    int counter = 0;
    
    
    static void m_repeated_timer_id_handler(void * p_context)
    {
    
        bsp_board_led_invert(counter);
    
        if(counter == MAX_LEDS) 
    
    
        {
            counter = 0;    
            app_timer_stop(m_repeated_timer_id);    
        }
    
        else
        {
            counter++;
        }
    }
    
    
    
    static void  lfclk_request(void)
    
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    
    }
    
    
    static void create_timers()
    {
        ret_code_t err_code;
    
        err_code = app_timer_create(&m_repeated_timer_id, APP_TIMER_MODE_REPEATED, m_repeated_timer_id_handler);
        APP_ERROR_CHECK(err_code);
    
    }
    
    static void wave(void)
    {
        
        ret_code_t err_code;
        err_code = app_timer_start(m_repeated_timer_id, APP_TIMER_TICKS(500), NULL);
        APP_ERROR_CHECK(err_code);
    }
    
    
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
    
    
        /* Configure board. */
        bsp_board_init(BSP_INIT_LEDS);
    
        lfclk_request();
        app_timer_init();
        create_timers();
    
        wave();
        
        while(true)
        {
            //do Nothing
        }
    }

Reply
  • Hi,

    This should be a good starting point on how to use the application timer for your use case:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    
    #define MAX_LEDS 2
    APP_TIMER_DEF(m_repeated_timer_id);
    
    int counter = 0;
    
    
    static void m_repeated_timer_id_handler(void * p_context)
    {
    
        bsp_board_led_invert(counter);
    
        if(counter == MAX_LEDS) 
    
    
        {
            counter = 0;    
            app_timer_stop(m_repeated_timer_id);    
        }
    
        else
        {
            counter++;
        }
    }
    
    
    
    static void  lfclk_request(void)
    
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    
    }
    
    
    static void create_timers()
    {
        ret_code_t err_code;
    
        err_code = app_timer_create(&m_repeated_timer_id, APP_TIMER_MODE_REPEATED, m_repeated_timer_id_handler);
        APP_ERROR_CHECK(err_code);
    
    }
    
    static void wave(void)
    {
        
        ret_code_t err_code;
        err_code = app_timer_start(m_repeated_timer_id, APP_TIMER_TICKS(500), NULL);
        APP_ERROR_CHECK(err_code);
    }
    
    
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
    
    
        /* Configure board. */
        bsp_board_init(BSP_INIT_LEDS);
    
        lfclk_request();
        app_timer_init();
        create_timers();
    
        wave();
        
        while(true)
        {
            //do Nothing
        }
    }

Children
Related