/* Hello Wokwi! */

#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#define RED_LED 13
#define GREEN_LED 12
#define BLUE_LED 11
#define BLUE_BUTTON 3
const byte GREEN_BUTTON = 2;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  Serial.begin(9600);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(GREEN_BUTTON, INPUT_PULLUP);
  pinMode(BLUE_LED, OUTPUT);
  pinMode(BLUE_BUTTON, INPUT_PULLUP);
  //Timer1.initialize(1000000); 
  //Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
  lcd.init();
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Hello, WORLD!");
}

int ledState = LOW;
//volatile unsigned long blinkCount = 0; // use volatile for shared variables

void blinkLED(void)
{
  
  if (ledState == LOW) {
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  if (digitalRead(GREEN_BUTTON) == 0){
    digitalWrite(GREEN_LED, ledState);
    Serial.print("1- ");
    Serial.println(millis() / 1000.0,3);
  }
  if (digitalRead(BLUE_BUTTON) == 0){
    digitalWrite(BLUE_LED, ledState);
    Serial.print("2- ");
    Serial.println(millis() / 1000.0,3);
  }
}


void loop() {

  //unsigned long blinkCopy;  // holds a copy of the blinkCount

  // to read a variable which the interrupt code writes, we
  // must temporarily disable interrupts, to be sure it will
  // not change while we are reading.  To minimize the time
  // with interrupts off, just quickly make a copy, and then
  // use the copy while allowing the interrupt to keep working.

  //noInterrupts();
  //blinkCopy = blinkCount;
  //interrupts();

  if (digitalRead(GREEN_BUTTON) == 0){
    //Timer1.attachInterrupt(blinkLED);
    attachInterrupt(0, blinkLED,RISING);
  }
  if (digitalRead(BLUE_BUTTON) == 0){
    //Timer1.attachInterrupt(blinkLED);
    attachInterrupt(1, blinkLED,RISING);
  }
  //else Timer1.detachInterrupt();
  //attachInterrupt(1, blinkLED,RISING);

  //Serial.println(blinkCopy); 
  digitalWrite(RED_LED, !digitalRead(RED_LED));
  lcd.setCursor(7, 1);
  lcd.print(millis());
  //delay(1000);
}