CrossRoads:
That kind of mostly uncommented code does not help the newbies.
I am somewhat with Msquare and Steve Gibson, that a blizzard of comments isn't always helpful and that the code itself should be largely self-explanatory. My main purpose here is to provide functions (rather than libraries) that can be cut-and-pasted to obtain a result easily, yet are decipherable.
OK, OK, had a fiddle with that initially - now corrected to UL. I was not sure it mattered - as best I can figure it does not matter given the "correct" form of comparison until and unless you specify an interval beyond (half of) the critical four weeks or whatever. Am having significant fun getting to grips with "C"; more at home with assembler where you know what is going on. ![]()
Current iteration:
// Blink without "delay()" - multi!
const int led1Pin = 13; // LED pin number
const int led2Pin = 10;
const int led3Pin = 11;
int led1State = LOW; // initialise the LED
int led2State = LOW;
int led3State = LOW;
unsigned long count1 = 0; // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
char timeout(unsigned long *marker, unsigned long interval) {
if (millis() - *marker >= interval) {
*marker += interval; // move on ready for next interval
return true;
}
else return false;
}
void setup() {
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}
void loop() {
// Act if the latter time (ms) has now passed on this particular counter,
if (timeout(&count1, 500UL )) {
if (led1State == LOW) {
led1State = HIGH;
}
else {
led1State = LOW;
}
digitalWrite(led1Pin, led1State);
}
if (timeout(&count2, 300UL )) {
if (led2State == LOW) {
led2State = HIGH;
}
else {
led2State = LOW;
}
digitalWrite(led2Pin, led2State);
}
if (timeout(&count3, 77UL )) {
if (led3State == LOW) {
led3State = HIGH;
}
else {
led3State = LOW;
}
digitalWrite(led3Pin, led3State);
}
}