#define GREEN_LED_PIN 17
#define RED_LED_PIN 18
#define BUTTON_PIN 19
unsigned long prevGreen = 0, prevRed = 0, lastDebounce = 0, lastPrint = 0, startTime = 0;
const unsigned long debounceDelay = 150;
const unsigned long normalGreenInterval = 600;
const unsigned long normalRedInterval = 300;
const unsigned long fastRedInterval = 50;
const unsigned long fastModeInterval = 200;
bool greenState = LOW, redState = LOW, fastMode = false;
int lastButtonState = HIGH, pressCount = 0;
unsigned long pressTimes[4] = {0};
int pressIndex = 0;
unsigned long greenOnStart = 0, redOnStart = 0;
unsigned long greenOnDuration = 0, redOnDuration = 0;
void setup() {
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
startTime = millis();
}
void loop() {
unsigned long now = millis();
int reading = digitalRead(BUTTON_PIN);
// Debounce handling
if (reading != lastButtonState) {
lastDebounce = now;
}
if (now - lastDebounce > debounceDelay) {
static int buttonState = HIGH;
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
pressCount++;
pressTimes[pressIndex] = now;
pressIndex = (pressIndex + 1) % 4;
Serial.print("Натискання: ");
Serial.print(pressCount);
Serial.print(", Час: ");
Serial.print(now / 1000.0, 2);
Serial.println(" с");
// Check for 4 presses within 5 seconds
if (pressCount >= 4) {
unsigned long earliestPress = pressTimes[pressIndex];
if (now - earliestPress <= 5000) {
fastMode = true;
Serial.println("Швидкий режим");
}
}
}
}
}
lastButtonState = reading;
if (fastMode) {
// Fast mode: both LEDs blink at 400ms (200ms on, 200ms off)
if (now - prevGreen >= fastModeInterval) {
prevGreen = now;
greenState = !greenState;
redState = !redState;
digitalWrite(GREEN_LED_PIN, greenState);
digitalWrite(RED_LED_PIN, redState);
if (greenState) greenOnStart = now;
else greenOnDuration += now - greenOnStart;
if (redState) redOnStart = now;
else redOnDuration += now - redOnStart;
}
} else {
// Normal mode
// Green LED: 1.2s period (600ms on, 600ms off) or off if button pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
if (now - prevGreen >= normalGreenInterval) {
prevGreen = now;
greenState = !greenState;
digitalWrite(GREEN_LED_PIN, greenState);
if (greenState) greenOnStart = now;
else greenOnDuration += now - greenOnStart;
}
} else {
if (greenState != LOW) {
greenState = LOW;
digitalWrite(GREEN_LED_PIN, LOW);
greenOnDuration += now - greenOnStart;
}
}
// Red LED: 600ms period (300ms on, 300ms off) or 100ms period (50ms on, 50ms off) if button pressed
long redInterval = (digitalRead(BUTTON_PIN) == LOW) ? fastRedInterval : normalRedInterval;
if (now - prevRed >= redInterval) {
prevRed = now;
redState = !redState;
digitalWrite(RED_LED_PIN, redState);
if (redState) redOnStart = now;
else redOnDuration += now - redOnStart;
}
}
// Print LED on durations and runtime
if (now - lastPrint >= 1000) {
lastPrint = now;
Serial.print("Green ON time: ");
Serial.print(greenOnDuration / 1000.0, 2);
Serial.print(" s, Red ON time: ");
Serial.print(redOnDuration / 1000.0, 2);
Serial.println(" s");
// Print total runtime every 10 seconds
if ((now - startTime) / 10000 > (lastPrint - startTime) / 10000) {
Serial.print("Загальний час роботи: ");
Serial.print((now - startTime) / 1000.0, 2);
Serial.println(" с");
}
}
}