I am a few weeks into learning Arduino and have found I don't have the experience to spot how to correct my program. The part of the scenario in question requires a first pump to cut in once a high level is reached, after an interval, a second pump cuts in, following a further interval, an alarm would come in, and if low level is reached, all resets.
I've tried to logically problem solve this by stripping out lines of code and make the system work one step at a time but I now have a feeling this is beyond my current skill.
Any advice would be appreciated.
Kind Regards
Dan
Here is the particular section( calculations etc not quoted in this)
if (voltageLevelHigh >= 4.5 && voltageLevelLow >= 4.5 ) { // defines high level in tank.
digitalWrite(pumpFirst, HIGH); // first pump to start pumpFirstStartTime = millis (); // Records pump start time
digitalWrite(pumpSecond, HIGH); // second pump called ON
pumpStartTime = millis (); // Records pump start time
}
}```
```if (voltageLevelHigh >= 4.5 && voltageLevelLow >= 4.5 && millis () - (pumpSecondStartTime > pumpSecondOvertime)) { //time from second pump start to overtime interval
digitalWrite(alarm, HIGH); // alarm called to activate.
}
Serial.print("pumpFirst time ");
Serial.println(pumpStartTime);
else (voltageLevelLow <=1.5);{
(digitalWrite(pumpFirst, LOW)); // first pump to stop
(digitalWrite(pumpSecond, LOW)); // second pump to stop
digitalWrite(alarm, LOW); // alarm called to deactivate.
}
}```
For example, here, pumpSecond is set to a time, presumably taken from millis() in part of the code you didn't share. But in the next line, pumpSecond is used like it is a pin number.
Thanks for your responses.
Ctrl-t is a nice trick to learn.
So far I have built this as a conceptual project on a breadboard, using two pots for high and low levels and three LED's for the pumps and alarm.
int pump1 = 7; // defines 1st pump connection point
int pump2 = 5; // defines 2nd pump connection point
int levelHigh = A1; // defines HIGH level sensor connection point
int levelLow = A0; // defines LOW level sensor connection point
int analogLevelHigh;
int analogLevelLow;
float voltageLevelHigh;
float voltageLevelLow;
int alarm = 8; // defines alarm connection point
int pumpFirst; // defines first pump to start during sequence
int pumpSecond; // defines second pump to start during sequence
unsigned long pumpStartTime;
unsigned long pumpFirstStartTime = 0;
unsigned long pumpFirstOvertime = 30000;
unsigned long pumpSecondOvertime = 60000;
void setup() {
pinMode(pump1, OUTPUT); //defines pump 1 pin7 as an output
pinMode(pump2, OUTPUT); //defines pump 2 pin5 as an output
pinMode(levelHigh, INPUT); //defines HIGH level pinA1 as an input
pinMode(levelLow, INPUT); //defines LOW level pinA0 an an input
pinMode(alarm, OUTPUT); //defines alarm pin8 as an output
Serial.begin(9600);
}
void loop() {
if (pumpFirst = pump1) {
pumpFirst = pump2; // defines logic for first pump selection, based on previous first pump.
pumpSecond = pump1; // defines logic for second pump selection, based on first pump. (line above}
else {
pumpFirst = pump1; // defines logic for first pump selection, based on previous first pump not being true for above condition.
pumpSecond = pump2; // defines logic for second pump selection, based on first pump. (line above)
}
analogLevelHigh = analogRead(levelHigh); //
voltageLevelHigh = (5.0 / 1023.0) * analogLevelHigh; //
analogLevelLow = analogRead(levelLow); //
voltageLevelLow = (5.0 / 1023.0) * analogLevelLow; //
Serial.print("voltageLevelHigh = ");
Serial.println(voltageLevelHigh);
Serial.print("voltageLevelLow = ");
Serial.println(voltageLevelLow);
delay(1000);
if (voltageLevelHigh >= 4.5 && voltageLevelLow >= 4.5) { // defines high level in tank.
digitalWrite(pumpFirst, HIGH); // first pump to start
pumpFirstStartTime = millis(); // Records pump start time
pumpFirst = pumpFirstStartTime;
pumpSecond = pumpSecondStartTime;
if (voltageLevelHigh >= 4.5 && voltageLevelLow >= 4.5 && millis() - (pumpFirstStartTime > pumpFirstOvertime)) { //time from firstpump start to overtime interval
digitalWrite(pumpSecond, HIGH); // second pump called ON
pumpStartTime = millis(); // Records pump start time
}
}
if (voltageLevelHigh >= 4.5 && voltageLevelLow >= 4.5 && millis() - (pumpSecondStartTime > pumpSecondOvertime)) { //time from second pump start to overtime interval
digitalWrite(alarm, HIGH); // alarm called to activate.
}
Serial.print("pumpFirst time ");
Serial.println(pumpStartTime);
else(voltageLevelLow <= 1.5);
{
(digitalWrite(pumpFirst, LOW)); // first pump to stop
(digitalWrite(pumpSecond, LOW)); // second pump to stop
digitalWrite(alarm, LOW); // alarm called to deactivate.
}
}
The code making no senses is down to coding being a very new thing to me, I've read through a couple of books and watched a lot of Paul McWhorter on youtube, but practically building projects of this complexity is highlighting my inexperience.
If you can suggest what parts are re-useable in a re-hash of the project and where I should think to change how I go about things next time, i'd appreciate it.
Keep your analog values as the integers you read. You gain no precision by converting to floats and you lose some every time you operate using floats.
0 to 1023 are discreet data. If you would divide then first promote the int value into a long and multiply by say 1000 to give 3 places to lose and interpret the values as more smaller units then Work in small units and only use decimalizing conversion for Display. That way you can keep the original precision.
Analog value of 512 has the same meaning as read*VCC/1024 without wasting cycles using precision-losing floats!
Thanks PaulRB, I really appreciate your effort. I shall spend the weekend comparing the code I produced against your version, I'm sure i will be learning a lot of new terms and formatting.