//*********************************************************************************
// Version YY/MM/DD Description
// ======= ======== ===========
// 1.00 21 11 28 Working sketch
//
//
//Prints the switch closed times to the serial monitor.
//*********************************************************************************
#define OPEN HIGH
#define CLOSED LOW
//*********************************************************************************
const byte heartbeatLED = 13;
const byte resetSwitch = 3;
const byte mySwitch = 2;
byte currentState;
byte resetLastState = OPEN;
byte mySwitchLastState = OPEN;
//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long totalTime;
unsigned long timeClosed;
//*********************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(heartbeatLED, OUTPUT);
pinMode(resetSwitch, INPUT_PULLUP);
pinMode(mySwitch, INPUT_PULLUP);
} //END of setup()
//*********************************************************************************
void loop()
{
//***************************************
//time to toggle the heartbeat LED ?
if (millis() - heartbeatMillis >= 500)
{
//restart the TIMER
heartbeatMillis = millis();
//toggle LED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//******************************************
//is it time to check the switches ?
if (millis() - switchMillis >= 20)
{
//restart the TIMER
switchMillis = millis();
checkSwitches();
}
//******************************************
//other none blocking code goes here
//******************************************
} //END of loop()
//*********************************************************************************
void checkSwitches()
{
//***********************************************
// m y S w i t c h
//check the switch for a change in state
currentState = digitalRead(mySwitch);
//******************************
//was there a change ?
if (mySwitchLastState != currentState)
{
//update to the new state
mySwitchLastState = currentState;
//******************
//was the switch closed ?
if (currentState == CLOSED)
{
timeClosed = millis();
}
//******************
//the switch was opened
else
{
totalTime = totalTime + millis() - timeClosed;
//print switch closed times
Serial.print("The switch was closed for ");
Serial.print(millis() - timeClosed);
Serial.println(" milliseconds.");
Serial.print("Accumulated switch closed time in ms = \t");
Serial.println(totalTime);
Serial.println();
}
} //END of mySwitch code
//***********************************************
// r e s e t S w i t c h
//check the switch for a change in state
currentState = digitalRead(resetSwitch);
//******************************
//was there a change ?
if (resetLastState != currentState)
{
//update to the new state
resetLastState = currentState;
//******************
//was the switch closed ?
if (currentState == CLOSED)
{
totalTime = 0;
//print the total time the switch has been closed
Serial.print("Accumulated switch closed time in ms = \t");
Serial.println(totalTime);
Serial.println();
}
} //END of resetSwitch code
//***********************************************
//other switch code goes here
} //END of checkSwitches()
//*********************************************************************************