#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int led1Pin = 2;
const int led2Pin = 3;
const int led3Pin = 4;
const int led4Pin = 5;
const int button1Pin = 14;
const int button2Pin = 15;
const int yellowbtn = 11;
unsigned long lastBlink1 = 0; // millis timestamp for LED1 blink
unsigned long lastDebounce1 = 0; // debounce timestamp for button1 hold
unsigned long lastBlink2 = 0; // millis timestamp for LED3 blink
unsigned long task3StartTime = 0; // timestamp for Task3 start
unsigned long interval_debouncebnt1 = 50;
unsigned long interval_debouncebnt2 = 50;
unsigned long interval_debounceyellowbtn = 50;
unsigned long interval_purpleLed = 500;
unsigned long prevTime_debouncebnt1 = 0;
unsigned long prevTime_debouncebnt2 = 0;
unsigned long prevTime_debounceyellowbtn = 0;
unsigned long prevTime_purpleLed = 0;
int counter = 10;
bool done_flag = false;
//flags
bool button1flag = false;
bool button1Held = false;
bool button2Pressed = false;
int buttonState;
bool lastButtonState = LOW; // the previous reading from the input pin
bool ledState = LOW;
bool last_ylwButtonState = LOW; // the previous reading from the input pin
bool ledgreenState = LOW;
bool sendPulse_flag = false;
void setup() {
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(yellowbtn, INPUT);
Serial.begin(9600);
lcd.init();
// turn on the backlight
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("hello");
}
void Task_1(unsigned long CT) {
buttonState = digitalRead(button1Pin);
if(CT - prevTime_debouncebnt1 > interval_debouncebnt1) {
prevTime_debouncebnt1 = CT;
if(buttonState == HIGH && button1flag == false){
digitalWrite(led2Pin, !digitalRead(led2Pin)); // Toggle LED state
button1flag = true;
}
if(buttonState == LOW){
button1flag = false;
}
// delay(50);
}
}
void loop() {
unsigned long currentTime = millis();
Task_1(currentTime);
if(currentTime - prevTime_debouncebnt2 > interval_debouncebnt2) {
bool currentButtonState = digitalRead(button2Pin);
prevTime_debouncebnt2 = currentTime;
// if state change detected
if (currentButtonState != lastButtonState) {
if (currentButtonState == HIGH) {
ledState = !ledState; // Toggle the LED state
}
// Delay a little bit to avoid bouncing
// delay(50);
}
lastButtonState = currentButtonState;
digitalWrite(led1Pin, ledState);
}
if(currentTime - prevTime_debounceyellowbtn > interval_debounceyellowbtn) {
bool currentButtonState = digitalRead(yellowbtn);
prevTime_debounceyellowbtn = currentTime;
// if state change detected
if (currentButtonState != last_ylwButtonState) {
if (currentButtonState == HIGH) {
ledgreenState = !ledgreenState; // Toggle the LED state
sendPulse_flag = !sendPulse_flag; //Toggle Flag
lcd.clear();
lcd.print(" SP flag: " + String(sendPulse_flag));
}
// Delay a little bit to avoid bouncing
// delay(50);
}
last_ylwButtonState = currentButtonState;
digitalWrite(led3Pin, ledgreenState);
}
if (sendPulse_flag && digitalRead(led2Pin) && ledState && !done_flag){
if (currentTime - prevTime_purpleLed > interval_purpleLed ){
prevTime_purpleLed = currentTime;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("sending pulse...");
digitalWrite(led4Pin, !digitalRead(led4Pin));
if(counter > 0) {
counter--;
lcd.setCursor(0,1);
lcd.print("counter: " + String(counter));
}
if(counter == 0 && !done_flag) {
done_flag = true;
lcd.clear();
lcd.print("DONE Sending pulse");
}
}
}
}