#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
QueueHandle_t xQueue1;
SemaphoreHandle_t xSemaphore;
// Task function
void StartTask1(void *pvParameters) {
uint32_t msg = 7;
while (1) {
xSemaphoreTake(xSemaphore, portMAX_DELAY);
Serial.println("This is Task 2");
xSemaphoreGive(xSemaphore);
digitalWrite(8, HIGH);
vTaskDelay(500);
digitalWrite(8, LOW);
vTaskDelay(500);
xQueueSend( xQueue1, &msg, 10 );
}
}
// Task function
void StartTask2(void *pvParameters) {
uint32_t data;
uint8_t state;
while (1) {
xSemaphoreTake(xSemaphore, portMAX_DELAY);
Serial.println("This is Task 1");
xSemaphoreGive(xSemaphore);
xQueueReceive( xQueue1, &data, 10 );
Serial.print("Message: ");
Serial.println(data);
//Read the potentiometer value (0 to 1023)
long potValue = analogRead(A0);
//Map the potentiometer value to the LED brightness range (0 to 255)
long brightness = map(potValue, 0, 1023, 0, 255);
Serial.print("Potentiometer value: ");
Serial.println(brightness);
state = digitalRead(2);
if(!state)
digitalWrite(4, HIGH);
else
digitalWrite(4, LOW);
vTaskDelay(1000);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(8, OUTPUT);
pinMode(4, OUTPUT);
pinMode(2, INPUT_PULLUP);
// Create a task with a priority of 1 (idle), 2 (belowNormal), 3(Normal)
xTaskCreate(StartTask1, "SimpleTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(StartTask2, "SimpleTask", configMINIMAL_STACK_SIZE, NULL, 2, NULL);
xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) );
/* Create a mutex type semaphore. */
xSemaphore = xSemaphoreCreateMutex();
// Start the scheduler
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}