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

app_timer_stop work once in two

Hello,

I am encountering a small problem with my timer, I followed the tutorial : devzone.nordicsemi.com/.../application-timer-tutorial

The purpose of my project is to rotate a stepper motor using Bluetooth, when I press a button on nrf Toolbox it turns on the right and when I press another button it turns on the left.

Actually, this works fine, I give you my code below.

The thing is I use the LED2 on the nrf51dk to see if my timer is runing or not, as shown in the tutorial, and when I first press the button to make the motor go on the right for example, the led turns on for the duration of the timer and when it stops the led also stops, but if I press the button a second time, the motor rotate correctly, the LED turns on while it rotates, but when it is finished the LED is still on, and I have to rotate the motor again to see the led turn off.

I am trying to consume the less power as possible and I guess that if my timer doesn't stop it will consume power.

here is my code : 

//navigate threw the different cases to rotate the motor
void setDirection(){
	
			step_number++;
			if(step_number > 1){
				step_number = 0;
			}
			
}
//rotate the motor in the good direction
void stepper(){
	uint32_t err_code;
	if(Direction == true){
		nrf_gpio_pin_set(dirPin);
	}else{
		nrf_gpio_pin_clear(dirPin);
	}
	
	switch(step_number){
		case 0:
			nrf_gpio_pin_set(stepPin);
		break;
		case 1:
			nrf_gpio_pin_clear(stepPin);
			err_code = app_timer_start(timer_step, APP_TIMER_TICKS(10, APP_TIMER_PRESCALER), NULL);

		break;
	}
	
	setDirection();

}

//get the data send from my phone to know if the motor has to rotate on the right or left
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
   char on[2] = "on";
		char off[3] = "off";
    for (uint32_t i = 0; i < length; i++)
    {
        while (app_uart_put(p_data[i]) != NRF_SUCCESS);
    }
    while (app_uart_put('\r') != NRF_SUCCESS);
    while (app_uart_put('\n') != NRF_SUCCESS);
			
		if(strstr((char*)(p_data), on)){
			nrf_gpio_pin_clear(LED3);
			
			steps_left = 1000;
			Direction=true;
			
		
		}
		if(strstr((char*)(p_data), off)){
			nrf_gpio_pin_set(LED3);
			
				steps_left = 1000;
				Direction=false;
			
		}
}

static void timer_a_handler(void * p_context)
{
  nrf_drv_gpiote_out_toggle(LED2);

}

// Create timers
static void create_timers()
{   
    uint32_t err_code;

    // Create timers
    err_code = app_timer_create(&timer_step,
                                APP_TIMER_MODE_SINGLE_SHOT,
                                timer_a_handler);
	
    APP_ERROR_CHECK(err_code);
}

/**@brief Application main function.
 */
int main(void)
{
     uint32_t err_code;
    bool erase_bonds;

    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();

    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
						
		
    printf("\r\nUART Start!\r\n");
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
		create_timers();
		nrf_gpio_pin_dir_set(stepPin, NRF_GPIO_PIN_DIR_OUTPUT);
		nrf_gpio_pin_dir_set(dirPin, NRF_GPIO_PIN_DIR_OUTPUT);


    // Enter main loop.
    for (;;)
    {
			if(steps_left != 0){
				stepper();
				steps_left--;
				if(steps_left == 0){

					err_code = app_timer_stop_all();
					//err_code = app_timer_stop(timer_step);
					//to know if this step really happens, I receive this notification everytime, so i guess it should work
					uint8_t step0[5] = "step0";
					ble_nus_string_send(&m_nus, step0, strlen((char*)step0));
				}
			}

				
        power_manage();
    }
}

If you have any idea that would really help me, I am new to this, so any tips is welcome.

Thanks.

Parents Reply Children
No Data
Related