The code works except for testing when a jumper is grounded or pulled from ground. Or if a button is pressed or not.
The jumper is used to switch code modes between delay and millis mode, the sketch shows how they behave differently. It uses Serial Monitor and sending Enter key works to pause and unpause printing so that data can be stopped and examined.
It's a demo and at present I lack a jumper to finish testing.
IMO it's at least half decently commented and should not be beyond most beginners, but I know there's nits to pick.
// DualActionDelayMillis v1.1 by GoForSmoke 11/18/24 -- made for Uno R3
// expect: ground pin 7 to run delay mode, not grounded runs millis mode
// expect: enter key in Serial Monitor to pause action, unpause action
// note that Arduino millis is +/-1 and that printing takes time as well!
const byte blinkPin = 13; // Uno board LED pin13
const byte jumperPin = 7; // a jumper from pin 7 to GND or pin 7 unterminated
byte jumperStateNow, jumperStatePrev; // to compare what is to what was
const word debounce = 20; // ms delay while the contacts settle, dirty signal
byte blinkState; // led13 0=OFF, not-0=ON
const unsigned long interval1 = 5000;
unsigned long start1;
byte started1 = 0;
const unsigned long interval2 = 1000;
unsigned long start2;
byte started2 = 0;
void usage()
{
Serial.println( F( "\n Dual Action Delay Millis \n" ));
Serial.println( F( " ground pin 7 to run delay mode, not grounded runs millis mode" ));
Serial.println( F( " Send enter key in Serial Monitor to pause action, unpause action \n\n" ));
}
void setup()
{
Serial.begin( 115200 ); // run serial fast to clear the output buffer fast
// set Serial Monitor to match
pinMode( blinkPin, OUTPUT ); // LOW by default
// blinkState is 0 by default
pinMode( jumperPin, INPUT_PULLUP );
jumperStateNow = jumperStatePrev = digitalRead( jumperPin );
}
void loop()
{
// ============================ change mode with jumper =======
jumperStateNow = digitalRead( jumperPin ); // check for mode change
if ( jumperStateNow != jumperStatePrev ) // if jumperPin changes state, stop and debounce then re-init
{
while ( jumperStateNow != jumperStatePrev )
{
jumperStatePrev = jumperStateNow;
delay( debounce );
jumperStateNow = digitalRead( jumperPin );
} // finished debounce
started1 = started2 = 0; // init for millis mode
}
// ============================ end change mode with jumper =======
// ============================ millis mode ===================
if ( jumperStateNow > 0 ) // run millis mode
{
if ( started1 == 0 )
{
started1 = 1;
Serial.print( F( "Millis Wait " ));
Serial.print( interval1 );
Serial.print( F( " time " ));
Serial.println( millis());
start1 = millis();
}
if ( started2 == 0 )
{
started2 = 1;
Serial.print( F( "Millis Wait " ));
Serial.print( interval2 );
Serial.print( F( " time " ));
Serial.println( millis());
start2 = millis();
}
if ( millis() - start1 >= interval1 )
{
started1 = 0;
Serial.print( F( "Finished Millis Wait " ));
Serial.print( interval1 );
Serial.print( F( " time " ));
Serial.println( millis());
}
if ( millis() - start2 >= interval2 )
{
started2 = 0;
Serial.print( F( "Finished Millis Wait " ));
Serial.print( interval2 );
Serial.print( F( " time " ));
Serial.println( millis());
}
}
// ============================ end millis mode ===================
// ============================ delay mode ===================
else // run delay mode
{
Serial.print( F( "Delay " ));
Serial.print( interval1 );
Serial.print( F( " time " ));
Serial.println( millis());
delay( interval1 );
Serial.print( F( "Finished Delay " ));
Serial.print( interval1 );
Serial.print( F( " time " ));
Serial.println( millis());
Serial.print( F( "Delay " ));
Serial.print( interval2 );
Serial.print( F( " time " ));
Serial.println( millis());
delay( interval2 );
Serial.print( F( "Finished Delay " ));
Serial.print( interval2 );
Serial.print( F( " time " ));
Serial.println( millis());
}
// ============================ end delay mode ===================
// ============================ pause from serial monitor=====
if ( Serial.available()) // enter serial to pause
{
usage();
while ( Serial.available())
{
Serial.read(); // empty the buffer
}
while ( !Serial.available()); // wait for unpause
while ( Serial.available())
{
Serial.read(); // empty the buffer
}
started1 = started2 = 0; // re-init millis mode
}
// ============================ end pause from serial monitor=====
}