int const downSwitch = 2; // Define Pins
int const upSwitch = 3;
int const heaterControl = 4;
int const autoRepeat = 200; // Auto-Repeat delay in mS
int Power = 30; // Define heater power variable and set to 30 (18.75% duty cycle)
int loopCount = 160; // 16 heat levels (plus off) x 1/10th sec per iteration
int onTime = 30; // Initial on time (in 1/10ths of a second)
int offTime = 130; // Initial off time (in 1/10ths of a second)
void setup()
{
pinMode(downSwitch, INPUT); // Set down switch pin as input
pinMode(upSwitch, INPUT); // Set up switch pin as input
pinMode(heaterControl, OUTPUT); // Set heater relay control pin as output
for (uint16_t modeTimer = 9600; modeTimer > 0; modeTimer--) // Count down 960 seconds
{
digitalWrite(heaterControl, LOW); // - turn heater on
if (digitalRead(upSwitch) == 1) // - check for up switch press
{
modeTimer = 1; // - exit auto mode if pressed (1 not 0 because it still gets decremented at the end of the loop)
}
if (digitalRead(downSwitch) == 1) // - check for down switch press
{
modeTimer = 1; // - exit auto mode if pressed (1 not 0 because it still gets decremented at the end of the loop)
}
delay (100); // - wait 100 milliseconds
} // And go around the loop until timer hits zero or a key is pressed
}
void loop() // Main code starts here (runs repeatedly)
{
onTime = Power; // Set on_time & off_time counters (in 1/10ths of a second)
offTime = 160 - Power; //
for (loopCount = 160; loopCount > 0; loopCount--) // Process 10x 100mS states (ie "1 second") 16 times (one for each of the possible heat levels)
{
if (digitalRead(upSwitch) == 1) // Check for "up switch"
{
if (Power < 160) // Only do if not already at max power level
{
Power += 10; // Increase heater power if up switch pressed
loopCount = 1; // Reset the timing loop
delay(autoRepeat); // Slow it down for auto repeat
}
}
if (digitalRead(downSwitch) == 1) // Check for "down switch"
{
if (Power > 0) // Only do if not already at min power level
{
Power -= 10; // Decrease heater power if down switch pressed
loopCount = 1; // Reset the timing loop
delay(autoRepeat); // Slow it down for auto repeat
}
}
if (onTime > 0) // If time on required then ...
{ //
digitalWrite(heaterControl, LOW); // ... turn on the relay
onTime--; // ... and decrement the counter by 1/10th of a second
} //
else // If time off required then ...
{ //
if (offTime > 0) // for as long at time off hasn't timed out ...
{ //
digitalWrite(heaterControl, HIGH); // ... turn off the relay
offTime--; // ... and decrement the counter by 1/10th of a second
} //
} //
delay(100); // Wait 1/10th of a second - this sets the overall speed of the timing loop in normal operation
} // End of heat cycle loop
loopCount = 160; // 160 iterations done ... so reset the counter and get back into the loop
} // End of Main Code Loop