#define buttonHigher 2 // Increase cycle time
#define buttonLower 3 // Decrease cycle time
#define start 4 // Start cycle
#define stop 5 // Start cycle
#define motor 13 // Motor port
unsigned long runCycle = 0; // Cycle time
unsigned long onTime = 60000UL * 10UL; // 10 minutes
unsigned long oneMinute = 60000UL; // 1minute
bool startFlag = false; // Cycle control
//-------------------------------------------------------------------
void setup() {
Serial.begin(115200); // init Serial;
pinMode(buttonHigher, INPUT_PULLUP); // Port INPUT with pullup resistor
pinMode(buttonLower, INPUT_PULLUP); // Port INPUT with pullup resistor
pinMode(start, INPUT_PULLUP); // Port INPUT with pullup resistor
pinMode(stop, INPUT_PULLUP); // Port INPUT with pullup resistor
pinMode(motor, OUTPUT); // Port OUTPUT
}
//-------------------------------------------------------------------
void loop() {
if (digitalRead(buttonHigher) == LOW) // Verify if is to increase
{
delay(40); // Debouncing
if (digitalRead(buttonHigher) == LOW) // Confirme increase
{
onTime += oneMinute; // Increase cycle time
Serial.println(onTime / 60000UL); // Show cycle time in minutes
}
while (digitalRead(buttonHigher) == LOW) {} // Wait release button
}
if (digitalRead(buttonLower) == LOW) // Verify if is to decrease
{
delay(40); // Debouncing
if (digitalRead(buttonLower) == LOW) // Confirme decrease
{
onTime -= oneMinute; // Dencrease cycle time
if (onTime < 1) // If cycle time 0
onTime = 60000UL; // cycle time = 60000 (1 minute)
Serial.println(onTime / 60000UL); // Show cycle time in minutes
}
while (digitalRead(buttonLower) == LOW) {} // Wait release button
}
if (digitalRead(start) == LOW) // Start cycle
{
delay(40); // Debouncing
if (digitalRead(start) == LOW) // Confirm Start cycle
{
digitalWrite(motor, HIGH); // Turn on motor
startFlag = true; // Indicate started
runCycle = millis(); // Charge time control
}
}
while ( startFlag == true) // While in time
{
if ((millis() - runCycle) % 1000 == 0) // Print at each second
Serial.println((millis() - runCycle)/1000);
if (millis() - runCycle >= onTime) // Verifiy if time is over
{
digitalWrite(motor, LOW); // Turn off motor
startFlag = false; // Indicate stoped
}
if (digitalRead(stop) == LOW) // Stop cycle
{
delay(40); // Debouncing
if (digitalRead(stop) == LOW) // Confirm stop
{
digitalWrite(motor, LOW); // Turn off motor
startFlag = false; // Indicate stoped
}
}
}
}