const int ledPin = 13; // LED pin
const int buttonPin = 2; // Button pin
const int cycleTime = 10000; // ON/OFF duration in milliseconds (10s ON, 10s OFF)
bool ledState = false; // LED state (ON/OFF)
bool systemActive = false; // Tracks whether the LED cycle has started
unsigned long previousMillis = 0; // Stores last toggle time
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
Serial.begin(9600); // Start Serial Monitor
}
void loop() {
// Check if the button is pressed (LOW due to INPUT_PULLUP)
if (digitalRead(buttonPin) == LOW && !systemActive) {
delay(50); // Debounce delay
if (digitalRead(buttonPin) == LOW) { // Confirm button press
systemActive = true; // Start the LED cycle
ledState = true; // LED turns ON first
previousMillis = millis(); // Reset timer
digitalWrite(ledPin, ledState);
Serial.println("LED is ON, cycle started");
}
}
// If system is active, follow the blinking cycle
if (systemActive) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= cycleTime) {
previousMillis = currentMillis; // Update last toggle time
ledState = !ledState; // Toggle LED state
digitalWrite(ledPin, ledState);
// Print LED state to Serial Monitor
if (ledState) {
Serial.println("LED is ON");
} else {
Serial.println("LED is OFF");
}
}
}
}