// https://forum.arduino.cc/t/delay-or-millis-in-a-void-function-cant-figure-it-out/1061953
// https://wokwi.com/projects/350332948255343188
// Name.ino
// Web Link
//********************************************^************************************************
// Version YY/MM/DD Description
// 1.00 YY/MM/DD Running sketch
//
//
//********************************************^************************************************
#define CLOSED LOW
#define OPENED HIGH
#define PUSHED LOW
#define RELEASED HIGH
#define LEDon HIGH
#define LEDoff LOW
#define RELAYon LOW
#define RELAYoff HIGH
#define RUNNING true
#define notRUNNING false
#define ENABLED true
#define DISABLED false
//********************************************^************************************************
const byte buttonPin = 2; // +5V---[50k Pullup]---[Input Pin]---[Switch]---GND
const byte flashingLED = 3; // [Pin3]---[220R]---[A->|-K]---GND
const byte heartbeatLED = 13; // [Pin13]---[220R]---[A->|-K]---GND
byte lastButtonPinState;
byte flashingFlag = DISABLED;
//****************************
//timing stuff
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long flashingLedTime;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP); // +5V---[50k Pullup]---[Input Pin]---[Switch]---GND
pinMode(flashingLED, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************* heartbeat T I M E R
//to see if the sketch has blocking code,
//toggle the heartbeat LED (every 333ms)
if (millis() - heartbeatTime >= 333ul)
{
//restart the TIMER
heartbeatTime = millis();
//toggle the LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* switch T I M E R
//is it time to check the switches ? (every 50ms)
if (millis() - switchTime >= 50ul)
{
//restart the TIMER
switchTime = millis();
//go and check the switches
checkSwitches();
}
//************************************* flashingLED T I M E R
//if the flashingLED is allowed to flash,
//is it time to toggle the LED ?
if (flashingFlag == ENABLED && millis() - flashingLedTime >= 999UL)
{
//restart the TIMER
flashingLedTime = millis();
//toggle the LED
digitalWrite(flashingLED, !digitalRead(flashingLED));
}
//*********************************
//other non blocking code goes here
//*********************************
} //END of loop()
// c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
byte currentState;
//************************************* b u t t o n P i n
//buttonPin code
currentState = digitalRead(buttonPin);
//*****************************
//was there a change in state ?
if (lastButtonPinState != currentState)
{
//update to the new state
lastButtonPinState = currentState;
//*************
//was the switch closed ?
if (currentState == CLOSED)
{
//toggle the flashingFlag
flashingFlag = !flashingFlag;
if (flashingFlag == DISABLED)
digitalWrite(flashingLED, LEDoff);
//restart the TIMER
flashingLedTime = millis();
}
//*************
//switch was OPENED
else
{
}
} //END of this switch
//************************************* n e x t S w i t c h
} //END of checkSwitches()