// https://wokwi.com/projects/353587272457307137
// https://wokwi.com/projects/389347775708978177
// https://forum.arduino.cc/t/timer-function-for-staggered-switching/1076239
// https://forum.arduino.cc/t/photo-resistor-vs-diode-for-measuring-product-charge-length/1218810
# define mmPin 6
# define mmLED 7 // mm status lamp
# define mmGAP 1000 // charge finished gap
void setup() {
Serial.begin(115200);
Serial.println("mm timer demo\n");
pinMode(mmPin, INPUT_PULLUP);
pinMode(mmLED, OUTPUT);
}
unsigned long now; // time for all non-blocked functions
void loop()
{
now = millis();
// maybe initiaite/refresh a mm period
if (!digitalRead(mmPin))
anotherMM();
// check if it is time to unMM
checkMMTimer();
}
bool mmIsOn; // timing a charge
unsigned long mmTimer;
unsigned long firstMM;
unsigned long lastMM;
void anotherMM()
{
mmTimer = now;
lastMM = now;
if (mmIsOn == false) {
mmIsOn = true;
firstMM = now;
Serial.println("MM charge ON!");
digitalWrite(mmLED, HIGH); // and do relay or whatever needs doing
}
}
void checkMMTimer()
{
if (now - mmTimer < mmGAP) // time to unmm?
return;
if (mmIsOn == true) {
mmIsOn = false;
Serial.print("MM charge finished. Length ");
Serial.println(lastMM - firstMM);
Serial.println();
digitalWrite(mmLED, LOW); // and do relay or whatever needs doing
}
}
MM
CHARGE!