#include <Arduino_FreeRTOS.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const int addButton = 2;
const int subtractButton = 3;
const int startButton = 4;
volatile int timerValue = 0;
volatile bool timerRunning = false;
volatile unsigned long lastDebounceTimeAdd = 0;
volatile unsigned long lastDebounceTimeSubtract = 0;
volatile unsigned long lastDebounceTimeStart = 0;
const unsigned long debounceDelay = 200;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Timer: ");
lcd.print(timerValue);
pinMode(changeTime, INPUT);
pinMode(startButton, INPUT);
attachInterrupt(digitalPinToInterrupt(changeTime), changeTime, CHANGE);
attachInterrupt(digitalPinToInterrupt(startButton), startTimer, CHANGE);
xTaskCreate(updateLCDTask, "Update LCD", 128, NULL, 1, NULL);
xTaskCreate(timerTask, "Timer Task", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
void loop() {
}
void addTime() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTimeAdd) > debounceDelay) {
if (!timerRunning) {
timerValue += 5;
}
lastDebounceTimeAdd = currentTime;
}
}
void subtractTime() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTimeSubtract) > debounceDelay) {
if (!timerRunning && timerValue >= 5) {
timerValue -= 5;
}
lastDebounceTimeSubtract = currentTime;
}
}
void startTimer() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTimeStart) > debounceDelay) {
if (!timerRunning && timerValue > 0) {
timerRunning = true;
}
lastDebounceTimeStart = currentTime;
}
}
void updateLCDTask(void *pvParameters) {
while (true) {
lcd.setCursor(7, 0);
lcd.print(timerValue);
lcd.print(" ");
delay(500);
}
}
void timerTask(void *pvParameters) {
while (true) {
if (timerRunning) {
if (timerValue > 0) {
vTaskDelay(1000 / portTICK_PERIOD_MS); // 1 second delay
timerValue--;
} else {
timerRunning = false;
lcd.setCursor(7, 0);
lcd.print("this is not working");
}
}
}
}