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

I think it's a simple Timer Algorithm, but it's not working.

Hello

Using the timer, I am making code that turns on LED1 to 3 sequentially.  I thought it was going to be very simple, but it's not going as well as I thought.

I'm making code for LEDs to work in this order.

LED1 -> LED2 -> LED3 -> LED all turn off -> LED1 -> LED2 .......        (loop)

static void timer_handler(void * p_context)
{
    nrf_gpio_pin_toggle(BSP_LED_3);  //Timer check, 0.5 sec toggle
}

static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_create(&m_timer_id,
                                APP_TIMER_MODE_SINGLE_SHOT, //SINGLE_SHOT or REPEATED 
                                timer_handler);   
    APP_ERROR_CHECK(err_code); 
}


static void timer_start()
{
    ret_code_t err_code;

    // Start application timers.
    err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(500), NULL); //0.5sec delay
    APP_ERROR_CHECK(err_code);
}



int main(void)
{ 
    NRF_CLOCK-> TASKS_LFCLKSTART = 1;

    timers_init();
    gpio_init(); 

    nrf_gpio_pin_set(BSP_LED_0); //led off
    nrf_gpio_pin_set(BSP_LED_1);
    nrf_gpio_pin_set(BSP_LED_2);

    //move();

    for (;;)
    {
        power_manage();
        //move();
        //timer_start2();

        nrf_gpio_pin_set(BSP_LED_0); // all led off
        nrf_gpio_pin_set(BSP_LED_1);
        nrf_gpio_pin_set(BSP_LED_2);
        //nrf_delay_ms(500);
        
        timer_start(); //0.5sec
        nrf_gpio_pin_toggle(BSP_LED_0);
        timer_start();
        nrf_gpio_pin_clear(BSP_LED_1);
        timer_start();
        nrf_gpio_pin_clear(BSP_LED_2);
        timer_start();
        //nrf_delay_ms(500);
    }
}

When this code is executed, all LEDs 1,2,3 will be on at once and will not be off thereafter.

Timer works fine.  (The timer handler has the LED4 toggled to make sure the timer is working.)

I think it's a simple problem, but it's not going as well as I thought.
Can I know the problem with this?

Thank you!

Parents Reply Children
Related