#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
const int ledPin1 = 2;
const int switchPin1 = 4;
const int ledPin2 = 5;
const int switchPin2 = 18;
const int ledPin3 = 19;
const int switchPin3 = 13;
const int ledPin4 = 27;
const int switchPin4 = 15;
int currentLed = 1; // Keep track of the currently lit LED
int count = 0;
void ledControlTask(void *pvParameters) {
pinMode(ledPin1, OUTPUT);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(ledPin2, OUTPUT);
pinMode(switchPin2, INPUT_PULLUP);
pinMode(ledPin3, OUTPUT);
pinMode(switchPin3, INPUT_PULLUP);
pinMode(ledPin4, OUTPUT);
pinMode(switchPin4, INPUT_PULLUP);
unsigned long startTime = millis(); // Record the start time
while (true) {
// Update LED states based on currentLed
digitalWrite(ledPin1, currentLed == 1);
digitalWrite(ledPin2, currentLed == 2);
digitalWrite(ledPin3, currentLed == 3);
digitalWrite(ledPin4, currentLed == 4);
bool switchState1 = digitalRead(switchPin1);
bool switchState2 = digitalRead(switchPin2);
bool switchState3 = digitalRead(switchPin3);
bool switchState4 = digitalRead(switchPin4);
if (switchState1 == LOW && currentLed == 1) {
count++;
currentLed = 2;
} else if (switchState2 == LOW && currentLed == 2) {
count++;
currentLed = 3;
} else if (switchState3 == LOW && currentLed == 3) {
count++;
currentLed = 4;
} else if (switchState4 == LOW && currentLed == 4) {
count++;
currentLed = 1;
}
vTaskDelay(pdMS_TO_TICKS(20));
// Check if 2 minutes have passed and stop the process
if (millis() - startTime >= 1* 60 * 1000) {
vTaskDelete(NULL); // Stop the task
}
}
}
void setup() {
Serial.begin(115200);
xTaskCreate(ledControlTask, "LED_Control_Task", 2048, NULL, 1, NULL);
}
void loop() {
// Print the button press count from within loop
Serial.print("Button Press Count: ");
Serial.println(count);
// Check if 2 minutes have passed and turn off all LEDs
if (millis() >= 1 * 60 * 1000) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
while (true) {
// Loop indefinitely, all LEDs are turned off, and the process has stopped
}
}
delay(1000);
}