#include <Arduino.h>
#include <OneButton.h>
#define POWER_BUTTON 28
#define POWER_LED 27
static bool switched = false;
static bool lightup = false;
static bool turnoff = false;
static unsigned long currentTime;
static unsigned long previousTime = 0;
static unsigned long timeClicked;
const int timeInterval = 200;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(POWER_LED, OUTPUT);
pinMode(POWER_BUTTON, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
currentTime = millis(); // first priority!
if (digitalRead(POWER_BUTTON) != 1) {
timeClicked = millis();
switched = !switched;
delay(150);
while (currentTime - timeClicked <= 500) {
if (digitalRead(POWER_BUTTON) != 1) {
turnoff = !turnoff;
delay(350);
}
currentTime = millis();
}
}
if (currentTime - previousTime >= timeInterval) {
if (turnoff) {
digitalWrite(POWER_LED, LOW);
} else if (switched && lightup) {
lightup = false;
digitalWrite(POWER_LED, LOW);
previousTime = currentTime;
} else if (switched && !lightup) {
lightup = true;
digitalWrite(POWER_LED, HIGH);
previousTime = currentTime;
} else {
digitalWrite(POWER_LED, HIGH);
}
}
}