#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#define RED 2
#define GREEN 0
#define BUTTON 14
int RandValve = 0;
volatile bool Printing = false; // Declare as volatile
bool buttonstate = false;
const int debounceDelay = 200; // Debounce delay for button
void Task1(void *pvParameters) {
while (1) {
if (Printing) {
RandValve = random(0, 501);
Serial.print("Random Value: ");
Serial.println(RandValve);
vTaskDelay(pdMS_TO_TICKS(2000));
} else {
vTaskDelay(pdMS_TO_TICKS(100));
}
}
}
// TASK2 - Green LED control
void Task2(void *pvParameters) {
while (1) {
if (Printing && RandValve > 25) {
digitalWrite(GREEN, HIGH);
vTaskDelay(pdMS_TO_TICKS(250));
digitalWrite(GREEN, LOW);
vTaskDelay(pdMS_TO_TICKS(250));
} else {
digitalWrite(GREEN, LOW);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
}
void Task3(void *pvParameters) {
while (1) {
digitalWrite(RED, HIGH);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(RED, LOW);
vTaskDelay(pdMS_TO_TICKS(4500));
}
}
void Task4(void *pvParameters) {
while (1) {
if (digitalRead(BUTTON) == LOW) {
if (!buttonstate) {
Printing = !Printing;
Serial.print("Printing state: ");
Serial.println(Printing ? "ON" : "OFF");
buttonstate = true;
vTaskDelay(pdMS_TO_TICKS(debounceDelay));
}
} else {
buttonstate = false;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void setup() {
Serial.begin(115200);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
xTaskCreate(Task1, "Rand value generating", 1000, NULL, 1, NULL);
xTaskCreate(Task2, "Green LED Control", 1000, NULL, 1, NULL);+
xTaskCreate(Task3, "Red LED Blink", 1000, NULL, 1, NULL);
xTaskCreate(Task4, "Button Control", 1000, NULL, 1, NULL);
}
void loop() {
}