// Blink LEDs and serial print at these specific intervals
// for https://forum.arduino.cc/t/need-help-at-coding/1250376/249
// and https://forum.arduino.cc/t/need-help-at-coding/1250376/241
// Modified from https://wokwi.com/projects/366159375120487425
// into https://wokwi.com/projects/397422542317885441
// to eliminate the delay() and enable the 6th LED
// *** SEE welcome() FUNCTION FOR TIMING CHART
// Array variables managing a set of events
byte ledPin[] = {2, 4, 6, 8, 10, 11}; // each event's LED signal pins
unsigned long interval[] = {300, 700, 900, 2100, 3150, 6300}; // event intervals
unsigned long oldTime[] = {0, 0, 0, 0, 0, 0}; // each event's timestamp variable (start at zero time)
const int EVENTS = sizeof(ledPin)/sizeof(ledPin[0]); // measure the number of events by the size of the pin array
int counter;
void setup() {
Serial.begin(9600); // for the serial monitor
welcome(); // call the "welcome" screen with the timing diagram
}
void loop() {
for (int i = 0; i < EVENTS; i++) { // cycle through all events array
if (millis() - oldTime[i] >= interval[i]) { // compare intervals to time difference
oldTime[i] = millis(); // event interval 'i' has occurred, set new event i zero time
// The interval[] for each event (ledPin[]) has been tested
// now call the function you for that interval[]
placeHolderFunction (i); // blink the 'i' LED
if (counter > 79) {
counter = 0;
Serial.println();
Serial.print("interval[#] ---> ");
}
counter++;
// // Serial.print(ledPin[i]); // print the event pin number
Serial.print(i); // print the single event array number
}
}
placeHolderCleanupFunction();
}
void placeHolderFunction (int thisLED) { // this function lights a LED
digitalWrite(ledPin[thisLED], HIGH);
// instead of delay() it relies on placeHolderCleanupFunction() to
// turn off the ELD after an interval.
//delay(10);
//digitalWrite(ledPin[thisLED], LOW);
}
void placeHolderCleanupFunction(void) { // turn off LEDS if they've been on for a while
static uint32_t last = 0;
uint32_t now = millis();
if (now - last > 10 ) { //
last = now;
for ( int i = 0; i < EVENTS ; ++i) {
int myInterval = interval[i] *1/ 2; // 1/2 of the full interval
//int myInterval = 100; // 100ms after the start
if (digitalRead(ledPin[i]) == HIGH && now - oldTime[i] >= myInterval) {
digitalWrite(ledPin[i], LOW);
}
}
}
}
void welcome() {
Serial.println("Each character on this timing graph represents 100ms.");
Serial.println("interval[0] RED LED every 300ms |--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*");
Serial.println("interval[1] ORG LED every 700ms |------*------*------*------*------*------*------*------*------*");
Serial.println("interval[2] YEL LED every 900ms |--------*--------*--------*--------*--------*--------*--------*");
Serial.println("interval[3] GRN LED every 2100ms |--------------------*--------------------*--------------------*");
Serial.println("interval[4] BLU LED every 3150ms |-------------------------------*------------------------------*");
Serial.print("interval[#] ---> ");
}