I’m working with Arduino Mega (ATmega2560) and FreeRTOS. My question is: “How to start a timer, stop it and again start this timer ?”
The idea of project is to create a timer, start it, and while CallBack function is executing many times a temperature sensor store data in a buffer. When buffer is full I want to stop timer, then pass data to serial interface, and then start timer again. I try to start timer again but it doesn’t work right. How to do this ?
#include <Arduino_FreeRTOS.h>
#include <timers.h>
#define mainAUTO_RELOAD_TIMER_PERIOD pdMS_TO_TICKS( 1000 )
TimerHandle_t xAutoReloadTimer;
BaseType_t xTimerStarted, xTimerStopped;
void setup() {
Serial.begin(9600);
Serial.println("Create a Timer");
xAutoReloadTimer = xTimerCreate(
"AutoReload", // name of the timer
mainAUTO_RELOAD_TIMER_PERIOD, // period of timer (timer after which timer's callback function will be called)
pdTRUE, // set timer to pdTRUE to create an auto-reload timer
0, // timer unique ID
prvAutoReloadTimerCallback); // pointer to software timer's callback function
Serial.println("Timer Created");
if( xAutoReloadTimer != NULL ) {
startTimer();
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void prvAutoReloadTimerCallback( TimerHandle_t xTimer ) {
TickType_t xTimeNow;
xTimeNow = xTaskGetTickCount();
Serial.print("Auto-reload timer callback executing ");
Serial.println( xTimeNow/62 );
if (xTimeNow/62 == 10) {
stopTimer();
vTaskEndScheduler();
/* pass data to serial interface and start timer again */
/* Here I just print some on serial interface */
for (int i = 0; i < 10; i++){
Serial.println(i);
}
//startTimer(); // is not working good
}
}
void startTimer() {
xTimerStarted = xTimerStart( xAutoReloadTimer, 0 );
if( xTimerStarted == pdPASS )
{
Serial.println("Timer started");
Serial.println("start scheduler");
vTaskStartScheduler();
}
}
void stopTimer() {
xTimerStopped = xTimerStop(xAutoReloadTimer, 0);
if (xTimerStopped == pdPASS){
Serial.println("Timer Stoped");
xTimerStarted = pdFAIL;
}
}