I built an asphalt heater that consists of a Nano, a spark generator and a solenoid valve to cycle the flow of propane. The project is wired correctly and my code did as intended, but I've since added a rocker switch between D2 and GND as INPUT_PULLUP, and doing my best to arrange some basic code for that, I seem to be missing my intended mark and I believe my "if - else if" usage is not right, or perhaps something else in that area and was hoping someone can set me straight. The switch is to bypass the cycling of the solenoid valve and just open it until switch is toggled to the opposite position.
#include "Simpletimer.h"
Simpletimer timer1;
#define TURN_ON HIGH // TURN_ON and TURN_OFF are defined to account for Active LOW relays
#define TURN_OFF LOW // Used to switch relay states for on/off of 120VAC~ devices
#define gasPin 9 //Digitial Pin
#define sparkPin 10 //Digitial Pin
#define switchPin 2 //Digitial Pin
//-----
boolean gasTime = true;
long startGas;
long oldTime;
long burnDuration = 30000; //on for 30 seconds
long extinguishedDuration = 20000; //off for 20 seconds
//-----
boolean sparkTime = true;
long sparkOn;
long lastSpark;
long sparkOnTime = 500; //on for 1/2 second
long sparkOffTime = 5000; //off for 5 seconds
void sparkRoutine() {
if (sparkTime == true && millis() - lastSpark >= sparkOffTime)
{
sparkOn = millis();
digitalWrite(sparkPin, TURN_ON);
sparkTime = false;
}
if (millis() - sparkOn >= sparkOnTime && sparkTime == false)
{
digitalWrite(sparkPin, TURN_OFF);
sparkTime = true;
lastSpark = millis();
}
}
void gasRoutine() {
if (gasTime == true && millis() - oldTime >= extinguishedDuration)
{
startGas = millis();
digitalWrite(gasPin, TURN_ON);
digitalWrite(LED_BUILTIN, TURN_ON);
gasTime = false;
}
if (millis() - startGas >= burnDuration && gasTime == false)
{
digitalWrite(gasPin, TURN_OFF);
digitalWrite(LED_BUILTIN, TURN_OFF);
gasTime = true;
oldTime = millis();
}
}
void setup()
{
Serial.begin(9600);
delay(100);
pinMode(switchPin, INPUT_PULLUP); //the switch pin is an input pulled HIGH by an internal resistor
pinMode(gasPin, OUTPUT);
pinMode(sparkPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
byte switchState = digitalRead(switchPin); //read the state of the input pin (HIGH or LOW)
if (switchState == HIGH) //if it is LOW then the switch is closed
if (timer1.timer(1000)){
gasRoutine();
Serial.println("Cycling Valve per routine"); //print a message
}
else if (switchState == LOW)
{
digitalWrite(gasPin, TURN_ON);
Serial.println("Valve is always OPEN"); //print a message
}
if (timer1.timer(1000)){
sparkRoutine();
}
}