Im busy with another challenge of the YT course.
im trying to make code so that the user can enter a number which is put on a queue after the enter.
A second task schould read the queue and chancing the rate in which the builtin led is blinking
But I ran into 2 problems.
First the led is not blinking at all.
Second if I try to change the rate the second time , it seems that the change is not picked up with the second task at all.
You need to have a separate task which blinks the LED and you need to yield in the enterBlinkRate task to ensure that it does not hog the CPU. Here is a working solution:
#if CONGIG_FREETOS_UNICORE
static const BaseType_t app_cpu = 0 ;
#else
static const BaseType_t app_cpu = 1;
#endif
static const uint8_t msg_queue_len = 5;
static portMUX_TYPE my_spinlock = portMUX_INITIALIZER_UNLOCKED;
static QueueHandle_t msg_queue;
static const int led_pin = 2;
int blinkRate = 500;
void enterBlinkRate(void * parameter) {
char ch;
char buff[20];
int idx = 0;
int enteredDelayTime;
while (1) {
//clear the buffer
if (Serial.available() > 0) {
ch = Serial.read();
if (ch == '\n') {
enteredDelayTime = atoi(buff);
memset(buff, 0, 20);
idx = 0;
if (xQueueSend(msg_queue, (void *) &enteredDelayTime, 10) != pdTRUE) {
Serial.println("Queue full");
}
} else {
if (idx < 20) {
buff[idx] = ch;
idx++;
}
}
}
vTaskDelay(10 / portTICK_PERIOD_MS );
}
}
void changeBlinkRate(void * parameters) {
int enteredDelayTime;
while(1) {
if (xQueueReceive(msg_queue, (void *)&enteredDelayTime, portMAX_DELAY) == pdTRUE) {
/* Ensure serialization while updating the global variable. */
taskENTER_CRITICAL(&my_spinlock);
blinkRate = enteredDelayTime;
taskEXIT_CRITICAL(&my_spinlock);
}
}
}
void ledBlinkTask(void * parameters) {
while(1)
{
digitalWrite(led_pin, HIGH);
vTaskDelay(blinkRate / portTICK_PERIOD_MS );
digitalWrite(led_pin, LOW);
vTaskDelay(blinkRate / portTICK_PERIOD_MS);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(led_pin, OUTPUT);
// Wait a moment to start (so we don;t miss Serial output)
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println();
Serial.println("----FreeRTOS QUEUE Challenge ----");
msg_queue = xQueueCreate(msg_queue_len, sizeof(int));
xTaskCreatePinnedToCore(enterBlinkRate,
"Enter blink rate",
1024,
NULL,
1,
NULL,
app_cpu
);
xTaskCreatePinnedToCore(changeBlinkRate,
"Change blink rate",
1024,
NULL,
1,
NULL,
app_cpu
);
xTaskCreatePinnedToCore(ledBlinkTask,
"LED blink task",
1024,
NULL,
1,
NULL,
app_cpu
);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}