#include <EEPROM.h>
#define INPUT_PIN 10
#define TS1_LED 15
#define TS2_LED 17
#define TS3_LED 8
#define C_LED 9
#define RESET_BUTTON 4
volatile unsigned long totalSeconds = 0;
volatile unsigned long Seconds = 0;
volatile unsigned long TS1 = 5;
volatile unsigned long TS2 = 10;
volatile unsigned long TS3 = 15;
int address = 0;
hw_timer_t *timer = NULL;
void IRAM_ATTR onTimer() {
if(digitalRead(INPUT_PIN) == HIGH) { totalSeconds++;}
}
void IRAM_ATTR RC() {
totalSeconds = 0;
digitalWrite(TS1_LED,LOW);
digitalWrite(TS2_LED,LOW);
digitalWrite(TS3_LED,LOW);
EEPROM.write(address, totalSeconds);
EEPROM.commit();
}
void IRAM_ATTR DATA_SAVE() {
EEPROM.write(address, totalSeconds);
EEPROM.commit();
}
void setup() {
EEPROM.begin(127);
Serial.begin(115200);
pinMode(TS1_LED, OUTPUT);
pinMode(TS2_LED, OUTPUT);
pinMode(TS3_LED, OUTPUT);
pinMode(C_LED, OUTPUT);
pinMode(RESET_BUTTON, INPUT_PULLDOWN);
pinMode(INPUT_PIN, INPUT_PULLDOWN);
timer = timerBegin(1000000);
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, 1000000, true, 0);
attachInterrupt(RESET_BUTTON, RC, RISING);
attachInterrupt(INPUT_PIN, DATA_SAVE, FALLING);
totalSeconds = EEPROM.read(0);
if (totalSeconds <= 0) {totalSeconds==0;}
if (totalSeconds >= 20) {totalSeconds==20;}
}
void loop() {
digitalWrite(C_LED,digitalRead(INPUT_PIN));
if(totalSeconds >= 5) {digitalWrite(TS1_LED,HIGH); }
if(totalSeconds >= 10) {digitalWrite(TS2_LED,HIGH); }
if(totalSeconds >= 15) {digitalWrite(TS3_LED,HIGH); }
if(totalSeconds >= 20) {RC();}
Serial.println(totalSeconds);
}