Hi, this is my first post and I have been learning to work with this Arduino Duo for exactly 5 days. I have a decent amount of coding experience over my 40+ years of life but am brand spanking new to programming microcontrollers.
My project begins fairly straightforward. I have two fog machines that I want to alternately fire for 6 seconds each with a 2 second cooldown in between them when both fog machines are online (detected by a "preheat" pin going high from the fog machine's thermostat). If only one machine is found to be online I have a different timing scheme of 8 seconds on and 3 seconds off.
My long term vision is to incorporate a LCD screen and rotary encoder with some buttons to allow on the fly manipulation of the timing intervals but for now the code is limited to just demonstrating that the timing works with the default values.
Currently the only hardware connected to the Arduino board are a couple of LEDs to stand in for the relays. The LEDS are current limited to ground with 10k ohm resistors. The preheat pins are manually connected to Vcc to emulate the fog machine(s) coming online but are otherwise pulled down to Ground using 10k ohm resistors. The relay signal pins are likewise pulled down to Ground using 10k ohm resistors just to keep everything at a known state. As the code has evolved the hardware has always performed exactly as expected.
My code is currently littered with several Serial.print lines to aid in debugging and has comments along the way to aid in readability. The code as posted actually works. My issue is more that I don't really understand why. To be more specific, I don't understand why the BWod concept works in the fog() function but not in the same way in the noFog() function.
In fog() the loop remains in the function until if (onLoopStart - loopElapsed >= fogOn && fogState == true) becomes true. This makes sense to me because at this point the program has no means of exiting the function.
Looking at noFog() when if (offLoopStart - loopElapsed >= fogOff) evaluates as false the program will instantly return to fog() without the else statement to call the noFog() function for a repeat pass. Why does the program not remain in this function; what makes this one different from fog()?
The Serial output for this loop reads:
...fogger 1
noFog
running offLoopStart: 183227 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183309 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183390 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183471 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183552 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183632 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183714 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183795 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183876 running loopElapsed: 183217
try again
noFog
running offLoopStart: 183957 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184039 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184120 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184201 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184282 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184363 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184444 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184525 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184606 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184687 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184769 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184850 running loopElapsed: 183217
try again
noFog
running offLoopStart: 184931 running loopElapsed: 183217
try again
noFog
running offLoopStart: 185012 running loopElapsed: 183217
try again
noFog
running offLoopStart: 185093 running loopElapsed: 183217
try again
noFog
running offLoopStart: 185175 running loopElapsed: 183217
try again
noFog
running offLoopStart: 185255 running loopElapsed: 183217
finished offLoopStart: 185255 finished loopElapsed: 185255
fogger 2...
Here's the code:
//assign I/O pin names
const int Relay1 = 11;
const int Relay2 = 12;
const int Preheat1 = 9;
const int Preheat2 = 10;
//create relay on/off durations
long unsigned dualFogOn = 6000;
long unsigned dualFogOff = 2000;
long unsigned singleFogOn = 8000;
long unsigned singleFogOff = 3000;
//create clock watch variables
long unsigned loopElapsed = 0;
bool foggerToggle = true;
bool fogState = true;
//fog state functions
void noFog(long unsigned fogOff)
{
unsigned long offLoopStart = millis();
if (fogState == false)
{
//turn off the fogger(s)
Serial.println("noFog");
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
Serial.print("running offLoopStart: ");
Serial.print(offLoopStart);
Serial.print(" running loopElapsed: ");
Serial.print(loopElapsed);
Serial.println();
}
if (offLoopStart - loopElapsed >= fogOff)
{
loopElapsed = offLoopStart;
Serial.print("finished offLoopStart: ");
Serial.print(offLoopStart);
Serial.print(" finished loopElapsed: ");
Serial.print(loopElapsed);
Serial.println();
//prepare to fire the alternate fogger
foggerToggle = !foggerToggle;
//enable fogger mode functions
fogState = true;
}
else
{
Serial.println("try again");
noFog(fogOff);
}
};
void fog(int fogger, long unsigned fogOn, long unsigned fogOff)
{
unsigned long onLoopStart = millis();
//determine which fogger is to be fired
if (fogger == 1 && fogState == true)
{
Serial.print("fogger ");
Serial.println(fogger);
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
}
else if (fogger == 2 && fogState == true)
{
Serial.print("fogger ");
Serial.println(fogger);
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, HIGH);
};
//continue fogging for the specified amount of time
if (onLoopStart - loopElapsed >= fogOn && fogState == true)
{
loopElapsed = onLoopStart;
fogState = false;
noFog(fogOff);
}
};
void setup()
{
//assign pin modes
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Preheat1, INPUT);
pinMode(Preheat2, INPUT);
//open comms for debug
Serial.begin(9600);
}
void loop()
{
//preheat mode
if (digitalRead(Preheat1) == LOW && digitalRead(Preheat2) == LOW)
{
fogState = false;
noFog(5000);
};
//alternating fogger mode 1
if (digitalRead(Preheat1) == HIGH && digitalRead(Preheat2) == HIGH && (foggerToggle == true))
{
fogState = true;
fog(1, dualFogOn, dualFogOff);
};
//alternating fogger mode 2
if (digitalRead(Preheat1) == HIGH && digitalRead(Preheat2) == HIGH && (foggerToggle == false))
{
fogState = true;
fog(2, dualFogOn, dualFogOff);
};
//fogger1 only mode
if (digitalRead(Preheat1) == HIGH && digitalRead(Preheat2) == LOW)
{
fogState = true;
fog (1, singleFogOn, singleFogOff);
};
//fogger2 only mode
if (digitalRead(Preheat1) == LOW && digitalRead(Preheat2) == HIGH)
{
fogState = true;
fog (2, singleFogOn, singleFogOff);
};
}