//GLOBAL VARIABLE-----------------------------------------------------
SemaphoreHandle_t xBS; //binary semaphore handler
#define LED 4
#define ADC_pin 13
float Vin, Vthreshold = 1.95;
//TASKS---------------------------------------------------------------
void TaskA(void *pt) {
while (1) {
xSemaphoreTake( xBS, portMAX_DELAY );
Vin = analogReadMilliVolts(ADC_pin)/1000.0; //Convert mV to V
xSemaphoreGive( xBS );
delay(500);
}
}
void TaskB(void *pt) {
Serial.begin(250000); //baud rate: 250kbps
while (1) {
xSemaphoreTake( xBS, portMAX_DELAY );
Serial.println(Vin);
xSemaphoreGive( xBS );
delay(500);
}
}
void TaskC(void *pt) {
pinMode(LED, OUTPUT);
while (1) {
xSemaphoreTake( xBS, portMAX_DELAY );
Vin>Vthreshold? digitalWrite(LED, LOW) : digitalWrite(LED, HIGH); //Check condition, can use If...else
xSemaphoreGive( xBS );
delay(500);
}
}
//SETUP----------------------------------------------------------------
void setup() {
xBS = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(TaskA, "TASK ADC", 2048, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore(TaskB, "TASK UART", 2048, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(TaskC, "TASK LED", 1024, NULL, 1, NULL, 1);
}
void loop() { }