I’m writing a program where I want to receive signals from an IR remote, using the IRremote library. Inside the task responsible for detecting any signals from the remote I use IrReceiver.decode()
in an if statement. The problem I face is that this if statement seemingly prevents all the tasks from ever running. If the if statement were to be, for example, if(5 < 10)
, all tasks will run perfectly fine, but for whatever reason IrReceiver.decode()
completely prevents anything from running.
Here is the code:
#include <Arduino_FreeRTOS.h>
#include <queue.h>
#include <IRremote.h>
int outputPins[] = {11, 10, 2, 3, 4, 5, 12};
int numOfPins = sizeof(outputPins) / sizeof(outputPins[0]);
QueueHandle_t speedQueue, dirQueue;
void taskIR(){
String hexString;
BaseType_t queueStatus;
for(;;)
{
Serial.println("Entering taskIR");
if(IrReceiver.decode())
{
Serial.println("IR signal received");
queueStatus = xQueueSendToBack(speedQueue, &hexString, 0);
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void setSpeed(){
BaseType_t queueStatus;
String command;
for(;;)
{
Serial.println("Entering setSpeed");
queueStatus = xQueueReceive(speedQueue, &command, portMAX_DELAY);
if(queueStatus == pdPASS){
Serial.println("Read from speedQueue");
if(command == "44"){ // 30% speed
analogWrite(11, 255*0.1);
analogWrite(10, 255*0.1);
}
else if(command == "40"){ // 60% speed
analogWrite(11, 255*0.6);
analogWrite(10, 255*0.6);
}
else if(command == "43"){ // 100% speed
analogWrite(11, 255);
analogWrite(10, 255);
}
else if(command == "18"){ // stop
analogWrite(11, 0);
analogWrite(10, 0);
}
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void setDirection()
{
BaseType_t queueStatus;
for(;;)
{
Serial.println("Read from setDirection");
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
IrReceiver.begin(7, 0);
for(int i = 0; i < numOfPins; i++)
{
pinMode(outputPins[i], OUTPUT);
}
speedQueue = xQueueCreate(5, sizeof(String));
dirQueue = xQueueCreate(5, sizeof(String));
xTaskCreate(taskIR, "IR", 128, NULL, 1, NULL);
xTaskCreate(setSpeed, "setSpeed", 128, NULL, 5, NULL);
xTaskCreate(setDirection, "setDirection", 128, NULL, 5, NULL);
Serial.println("setup done");
}
void loop() {
// put your main code here, to run repeatedly:
}
In its current state, the code doesn’t really achieve anything meaningful since any further development of the program has haulted due to this issue.
When writing the code I first started off with the first task (taskIR) together with the IR remote and everything worked fine. However, as soon as I included any other task, none of the tasks would run.
With all the tasks included, everything works fine whenever I remove IrReceiver.decode()
from the if condition.
I feel like there’s something fundamental I’m missing but I can’t for the life of me figure it out.