// Define a task handle and initialize it to NULL
TaskHandle_t outputBuzzLights_handle = NULL;
//Explains how to suspend/restart tasks as required:https://savjee.be/videos/programming-esp32-with-arduino/manage-freertos-tasks/
void setup() {
//Setup Pins
pinMode(5, OUTPUT); //Pager Motor
pinMode(18, OUTPUT); //LED2
pinMode(25, INPUT_PULLUP); //ACK Button
pinMode(26, INPUT_PULLUP); //TST Button
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//Create Output Buzzer Task
xTaskCreatePinnedToCore(outputBuzzLights,
"OutputBuzzLights",
2048,
NULL,
1,
&outputBuzzLights_handle,
1);
//Create Test Button Check Task
xTaskCreatePinnedToCore(testBttnCheck,
"testBttnCheck",
2048,
NULL,
1,
NULL,
1);
//Create Check Serial Task
xTaskCreatePinnedToCore(checkSerial,
"checkSerial",
2048,
NULL,
1,
NULL,
1);
//The output buzzer, pager motor and leds do not need to be active initially, so suspend this task for now
vTaskSuspend(outputBuzzLights_handle);
//Call vTaskResume(outputBuzzLights_handle); to start the output again
//delay(1000);
//vTaskResume(outputBuzzLights_handle);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void testBttnCheck(void* parameters){
while(1){
if (digitalRead(26) == LOW) {
Serial.println("TST");
//Pager Acknowledged. Suspend the output task
vTaskResume(outputBuzzLights_handle);
}
vTaskDelay( 500 / portTICK_PERIOD_MS ); // wait for half a second
}
}
void outputBuzzLights(void* parameters){
while(1){
Serial.println("OUTPUT ACTIVE");
digitalWrite(5, HIGH);
digitalWrite(18, HIGH);
tone(4, 1000, 250); // Plays 262Hz tone for 0.250 seconds
vTaskDelay( 300 / portTICK_PERIOD_MS ); // wait for half a second
digitalWrite(5, LOW);
digitalWrite(18, LOW);
vTaskDelay( 200 / portTICK_PERIOD_MS ); // wait for half a second
if (digitalRead(25) == LOW) {
Serial.println("ACK");
//Pager Acknowledged. Suspend the output task
vTaskSuspend(outputBuzzLights_handle);
}
}
}
void checkSerial(void* parameters){
while(1){
if (Serial.available() > 0) {
// read the incoming string:
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
Serial.println(incomingString);
}
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for half a second
}
}