//
//================================================^================================================
// B a s i c R a w S k e t c h
//================================================^================================================
//
// URL
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 25/06/23 Running code
//
//
//
//
// Notes:
//
//
//
//
//================================================
#define LEDon HIGH //PIN---[220R]---A[LED]K---GND
#define LEDoff LOW
#define OPENED HIGH
#define CLOSED LOW
#define STOPPED 0
#define ENABLED 1
#define LOCKOUT 2
#define EXECUTEcode true //the code in the "State" is executed
#define BYPASScode false //the code in the "State" is not executed
#define RELAYon HIGH //a HIGH turns on the NPN transistor, the relay closes the dry contact
#define RELAYoff LOW
//================================================
#define ONE_SECOND 1000ul //milliseconds
#define ONE_MINUTE (ONE_SECOND * 60ul) //60 seconds in one minute
#define ONE_HOUR (ONE_MINUTE * 60ul) //60 minutes in one hour
#define ONE_DAY (ONE_HOUR * 24ul) //24 hours in one day
// G P I O s A n d V a r i a b l e s
//================================================^================================================
//
//Analogs
//================================================
//
const byte potentiometer = A1;
//INPUTS
//================================================
//
const byte mySwitch1 = 2;
//OUTPUTS
//================================================
//
const byte relayCoil = 12;
const byte heartbeatLED = 13;
//VARIABLES
//================================================
//
byte lastMySwitch = OPENED;
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long relayOnTime;
unsigned long relayLockTime;
unsigned long relayOnInterval = 2 * ONE_SECOND; //2 second
unsigned long lockOutInterval = 30 * ONE_SECOND; //30seconds
byte relayTimerFlag = STOPPED;
// s e t u p ( )
//================================================^================================================
//
void setup()
{
//Serial.begin(115200);
digitalWrite(heartbeatLED, LEDoff);
pinMode(heartbeatLED, OUTPUT);
digitalWrite(relayCoil, RELAYoff);
pinMode(relayCoil, OUTPUT);
pinMode(mySwitch1, INPUT_PULLUP);
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//Is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 500)
{
//Restart this TIMER
heartbeatTime = millis();
//toggle the heartbeat LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
//======================================================================== T I M E R switches
//Is it time to check our switches ?
if (millis() - switchTime >= 50)
{
//Restart this TIMER
switchTime = millis();
checkSwitches();
}
//======================================================================== T I M E R relay On
//If enabled, is it time to turn OFF the relay ?
if (relayTimerFlag == ENABLED && millis() - relayOnTime >= relayOnInterval)
{
//Turn OFF the relay
digitalWrite(relayCoil, RELAYoff);
//Restart and enable TIMER lockout
relayLockTime = millis();
relayTimerFlag = LOCKOUT;
}
//======================================================================== T I M E R relay lockout
//If we are in lockout, is it time to allow normal relay operation again ?
if (relayTimerFlag == LOCKOUT && millis() - relayLockTime >= lockOutInterval)
{
relayTimerFlag = STOPPED;
}
//================================================
// 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 switchState = digitalRead(mySwitch1);
//======================================================================== mySwitch1
//Was there a change in state for this switch input ?
if (lastMySwitch != switchState)
{
//Update to this new state.
lastMySwitch = switchState;
//========================
//Is timing stopped and was this switch closed ?
if (relayTimerFlag == STOPPED && switchState == CLOSED)
{
//Enable and Restart TIMER.
relayTimerFlag = ENABLED;
relayOnTime = millis();
digitalWrite(relayCoil, RELAYon);
}
} //END of mySwitch1
} //END of checkSwitches()
//
//================================================^================================================
//