enum { Off = HIGH, On = LOW };
#define Interval 3000L
const byte pinBut = A1;
const byte pinLed = 10;
unsigned long msecLst = 0;
unsigned long msec;
// -----------------------------------------------------------------------------
void
setOutput ()
{
digitalWrite (pinLed, On);
msecLst = millis ();
}
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin (9600);
digitalWrite (pinLed, Off);
pinMode (pinLed, OUTPUT);
pinMode (pinBut, INPUT_PULLUP);
}
// -----------------------------------------------------------------------------
void loop()
{
msec = millis ();
// check for button change and press
static byte butLst = Off;
byte but = digitalRead (pinBut);
if (butLst != but) {
butLst = but;
if (On == but)
setOutput ();
}
// turn off LED after Interval
if ( (0 < msecLst) && ((msec - msecLst) > Interval)) {
msecLst = 0;
digitalWrite (pinLed, Off);
}
}
1 Like