// Define the pins for the LEDs
const int onboardLED = 13;
const int redLED = 12;
const int greenLED = 11;
// Variables to control timing
unsigned long previousMillis = 0;
const long interval = 1000; // 1 second interval between LED changes
int ledState = 0; // 0 for onboard, 1 for red, 2 for green
void setup() {
// Set the LED pins as outputs
pinMode(onboardLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to change the LED
if (currentMillis - previousMillis >= interval) {
// Save the last time we changed the LED
previousMillis = currentMillis;
// Turn off all LEDs
digitalWrite(onboardLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
// Turn on the next LED in the cycle
if (ledState == 0) {
digitalWrite(onboardLED, HIGH); // Turn on onboard LED
ledState = 1; // Next LED is red
} else if (ledState == 1) {
digitalWrite(redLED, HIGH); // Turn on red LED
ledState = 2; // Next LED is green
} else if (ledState == 2) {
digitalWrite(greenLED, HIGH); // Turn on green LED
ledState = 0; // Next LED is onboard
}
}
}