Getting the order of operations to work

Hello,

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

pumpFirst = pumpFirstStartTime;
pumpSecond = pumpSecondStartTime;

    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.
}
}```

Please post full code in code tags.
Before that: press ctrl-t in the ide. This will autoformat your code.

Read on state machine. This forum has good info on how to apply state machines. It is not easy at first, but once you get it, it is very useful.

How are your level meters connected? What type are they? Why are you ecaluating voltage (instead of HIGH/ LOW)?

So far I do not see any need for a uC.
Could be achieved with a basic probe relay system.

1 Like

Mistake there. Careful where you put ;

But I'm sorry to say that, in general, your code makes no sense: often the result of copying & pasting with no understanding.

1 Like

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.
    }
  }

Thanks for your prompt response,

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!

1 Like

Two problems here:
PumpFirst does not have a value yet..
Should be ==. Not =.

1 Like

is the same as

pumpFirst = pump1;
if (pumpFirst != 0) { whaaa-dee-doo-dah };

It is. And I can immediately tell that the code you posted won't verify/upload. There is a } missing before the first else in loop().

How does this look?

const byte pump1pin = 7;       // defines 1st pump connection point
const byte pump2pin = 5;       // defines 2nd pump connection point
const byte levelHighPin = A1;  // defines HIGH level sensor connection point
const byte levelLowPin = A0;   // defines LOW level sensor connection point
const byte alarmPin = 8;   // defines alarm connection point

unsigned long pumpStartTime;
const unsigned long pumpFirstOvertime = 30000UL;
const unsigned long pumpSecondOvertime = 60000UL;

void setup() {
  pinMode(pump1Pin, OUTPUT);     //defines pump 1 pin7 as an output
  pinMode(pump2pin, OUTPUT);     //defines pump 2 pin5 as an output
  pinMode(levelHighPin, INPUT_PULLUP);  //defines HIGH level  pinA1 as an input
  pinMode(levelLowPin, INPUT_PULLUP);   //defines LOW level pinA0 an an input
  pinMode(alarmPin, OUTPUT);     //defines alarm pin8 as an output
  Serial.begin(115200);
}

void loop() {
    if (digitalRead(levelLowPin) == LOW and digitalRead(pump1pin) == HIGH) {
      Serial.println("Tank level low, stopping pumps & alarm");
      digitalWrite(pump1pin, LOW);
      digitalWrite(pump2pin, LOW);
      digitalWrite(alarmPin, LOW);
    }
    else if (digitalRead(levelHighPin) == HIGH and digitalRead(pump1pin) == LOW) {
      Serial.println("Tank level high, starting first pump");
      digitalWrite(pump1pin, HIGH);
      pumpStartTime = millis();
    }
    else if (digitalRead(levelHighPin) == HIGH and digitalRead(pump2pin) == LOW and millis() - pumpStartTime >= pumpFirstOvertime) {
      Serial.println("Tank level still high, starting second pump");
      digitalWrite(pump2pin, HIGH);
    }
    else if (digitalRead(levelHighPin) == HIGH and digitalRead(pump2pin) == HIGH and millis() - pumpStartTime >= pumpSecondOvertime) {
      Serial.println("Tank level still high, alarm on");
      digitalWrite(alarmPin, HIGH);
    }
}
1 Like

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.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.