int ledPin[] = {13, 12, 11,10}; // LED pins: MSB to LSB
int ledDelay[4]={8000, 4000,2000, 1000}; // Toggle intervals in milliseconds
// declare other required variables
unsigned long lastChangeTime[4]; // Stores last toggle time for each LED
bool ledState[4]; // Current state of each LED (HIGH or LOW)
unsigned long currTime; // Current time tracker
void setup() {
currTime = millis(); // Initialize current time
for(int i=0; i<4; i++){
lastChangeTime[i] = currTime; // initialize lastChangeTimes
pinMode(ledPin[i], OUTPUT); // Set LED pins as OUTPUT
ledState[i] = LOW; // Start with LED OFF
digitalWrite(ledPin[i], ledState[i]); // Apply initial state
}
}
void loop() {
currTime = millis(); // Update current time
for (int i = 0; i < 4; i++) {
if (currTime - lastChangeTime[i] >= ledDelay[i]) {
// Time to toggle this LED
ledState[i] = !ledState[i]; // Flip state
digitalWrite(ledPin[i], ledState[i]); // Apply new state
lastChangeTime[i] = currTime; // Update last change time
}
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6