//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) {
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() {
Serial.begin(115200); //baud rate
xBS = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(TaskA, "ADC", 2048, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore(TaskB, "UART", 2048, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(TaskC, "LED", 1024, NULL, 1, NULL, 1);
xSemaphoreGive(xBS); // Release semaphore
Serial.println("Begin operation...");
}
void loop() { }