Help with try function

hi all, can someone help me to code for try function on arduino. by using relays, ultrasonic sensors and flow sensors. My goal in making this is to build an automatic pump, where sometimes when the pump is on the water tank is not full, but the water source suddenly runs out and I have to manually turn off the pump and turn it back on a few minutes later to check the water source is there.

There are several conditions where the relay must be on and must be off. for example if the tank is empty then the relay / pump is on, but when the relay is on for 10 seconds and if there is no water flowing on the flow sensor then the relay turns off for 20 seconds and turns on again for 10 seconds while checking the water flow, repeats for 3 times, if in the process there is water flow then the relay/pump will continue to turn on until the tank is full, but if in the process there is still no water flow then the relay/pump must be off for 1 minute, and if after 1 minute the relay is off, the relay returns position trying early.

theory :

  1. tank is empty (YES)
  2. relay (ON) for 10 second while check water flow
  3. (YES) water flowing, relay ON until tank is full, but if
    (NO) water flowing, relay off for 20 second.
    repeat 3 times.
    if still (NO) water flowing, then relay OFF for 1 minute, after that start from beggining.

read about finite state machine.

there are many tutorials on line, even libraries

an introduction here on the Arduino site:
https://create.arduino.cc/projecthub/tolentinocotesta/let-s-learn-how-to-use-finite-state-machine-with-arduino-c524ac?ref=part&ref_id=8233&offset=13

and google will give you zillions more like

Yes but no helper will do the entire coding.
Start by dividing the project into smaller parts.
Reading buttons or switches is one part.
Handling a relay module is another.

Draw up a map for the flow of the code. The name is: Flow charts. That map should tell the different timings of events, when they will we activated and how.

State machine is probably best as already mentioned, but they are tricky if you have never used one.

You can likely put this together with messier code using delays.

Try coding some precursor projects:

  1. Is the tank full

  2. Run pump for ten seconds and turn it off

  3. Run pump, check for flow, turn it off.

These will be handy for building the final system.

Hello Romiez_Fach
Post your current sketch, well formated, with comments and in so called code tags "</>" and a schematic, not a Fritzy diagram, to see how we can help.
Have a nice day and enjoy coding in C++.

very complicated for me, hope someone can help

if (TankLevelPer <= mintank) { 
    pumpoutstate = LOW; 
    buzz(10, blinkTime);
    Serial.println("I");
    if (FlowPMIn <= 1) { 
      //Serial.println("I1");

      if (pumpinstate == HIGH)    
      {
        if ((millis() - prevTimePumpInOn) >= PumpInOnDelay) { 
          pumpinstate = LOW; //pompa isi mati
          Serial.println("I1");
          lcd1.setCursor(0, 3);
          lcd1.print(">      ");
          lcd1.print("  ");
        }
      }
      else
      {
        if ((millis() - prevTimePumpInOn) >= PumpInOffDelay) {
          prevTimePumpInOn = millis();
          pumpinstate = HIGH; 
          Serial.println("I2");
          lcd1.setCursor(0, 3);
          lcd1.print("> Check");
          if (FlowPMIn >= 1) {
            Serial.println("I3");
            pumpinstate = HIGH;
            lcd1.setCursor(0, 3);
            lcd1.print("> Refil");
            lcd1.print("  ");
          }
          if ((FlowPMIn <= 1) && (TankLevelPer <= 100))
          {
            if (pumpinstate == HIGH)
            {
              if ((millis() - prevTimePumpInOn) >= PumpInOnDelay) {
                prevTimePumpInOn = millis();
                pumpinstate = LOW;
                Serial.println("I4");
              }
            }
            else
            {
              if ((millis() - prevTimePumpInOn) >= PumpInOffDelay) {
                prevTimePumpInOn = millis();
                pumpinstate = HIGH;
                Serial.println("I5");
              }
            }
          }
        }
      }
    }
  }

Code tags are better, code is incomplete though.

Hello Romiez_Fach
I have made a small review of the given sketch snipset.
My recoemmendation take some time, study the IPO model and take a piece of paper plus a pencil and design a program structure. Identify modules could to be coded and tested separetly. Merge after sucessfull testing all modules together to the needed project.
It is highly recommented to take a view into some powerful C++ instructions like STRUCT, ENUM, ARRAY, CASE/SWITCH,UDT, RANGE-BASED-FOR-LOOP, etc, too.

Have a nice day and enjoy coding in C++.

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