Code verification

Hi I am new to Arduino and need a second opinion to whether this code will work, I'm not sure about the if, else if, and else if it is in the correct order.

My Project is a water cooler startup, water fill and circulate, fan run and drain when shut down.

Flow diagram is:

Power on - start water solenoid if water level is low - wait 60 seconds (allow water to fill and circulate) - start fan- (stop water solenoid if water level is high - start pump - stop pump if water level is low -start solenoid (loop))
Power off - stop water solenoid- stop pump- stop fan - start drain solenoid for 30 second (allow water to drain)

// constants won't change. They're used here to set pin numbers:

const int waterPin = 13; // the number of the water solenoid pin
const int levelPin = 12; // the number of water level sensor pin
const int pumpPin = 11; // the number of water pump pin
const int fanPin = 10; // the number of the fan pin
const int drainPin = 9; // the number of the drain solenoid
const int mainPin = 2; // the number of the main power pin
const int levelPin = 3; // the number of water level sensor pin

// variables will change:
int mainState = 0; // variable for reading the mainpower status
int levelState = 0; // variable for reading the water level status

void setup() {
// initialize the water pin as an output:
pinMode(waterPin, OUTPUT);
// initialize the pump pin as an output:
pinMode (pumpPin, OUTPUT);
// initialize the fan motor pin as an output:
pinMode (fanPin, OUTPUT);
// initialize the drain solenoid pin as an output:
pinMode(drainPin, OUTPUT);
// initialize the main power pin as an input:
pinMode(mainPin, INPUT);
// initialize the level pin as an intput:
pinMode(levelPin, INPUT);
}

void loop() {
// read the state of the main power value:
mainState = digitalRead(mainPin);
// read the state of the water level value:
levelState = digitalRead(levelPin);
// check if the main power is on and water level is empty (sensor = High); MainState = high and waterState = High:
if (mainState == HIGH && levelState == HIGH) {
// turn pump off, water on and delay fan on for 60 seconds: {
digitalWrite(pumpPin, LOW);
digitalWrite(waterPin, HIGH);
delay(60000);
digitalWrite(fanPin, HIGH);
// check if the main power is off; MainState = Low and waterState = High/Low:
} else if (mainState == LOW) {
// turn all off and drain water:
digitalWrite(waterPin, LOW);
digitalWrite(pumpPin, LOW);
digitalWrite(fanPin, LOW);
digitalWrite(drainPin, HIGH);
delay(30000);
digitalWrite(drainPin, LOW);
// check if water level is full(sensor = Low); MainState = high and waterState = Low
} else {
// turn water off and start pump:
digitalWrite(waterPin, LOW);
digitalWrite(pumpPin, HIGH);

}
}

This will be much appreciated

Thanks

What made you think that your question has anything to do with the Website and Forum section which is clearly stated to be for “Improvements for the web system, applications to moderator, spam, etc.”? I have suggested to the Moderator to move it to the Programming section.

This sort of carelessness makes unnecessary work for the Moderators.

The best way to answer your question is to try the program. If it works you probably won’t need any help. If it doesn’t work then you will have additional information that will make it easier for us to help you. Test early and often.

Also, when posting code please use the code button </>

so your code 
looks like this

It makes it much easier to help you.

See How to use the forum

…R

@karelholder

Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

What happens when you run it :wink:

Presumably you have a test bed, even if you have buttons for the inputs and leds for the outputs?

If I were to check it for you I would set up something like that anyway.

Flow diagram is:

... except it's text. I personally find it much simpler to use a state diagram. See for example #13 here that I provided for another question.

What will happen if the water level at start up is low, but not 60s’ worth of supply low, just a “bit” low. You have a delay of 60s to fill it up, so if it’s full after 5s you have 55s’ worth of overflow.

You need a delay()-less approach here I think; let loop() do the work, it’s good at that.

Once I’ve had enough coffee I’ll do you a state diagram for how I see this.

Here's my take on this in a state diagram. I would code each state as a case in a switch...case.

706498.png

706498.png

Just noticed when I started to look at coding this, that far from having tried to run this sketch you haven't even compiled it:

exit status 1
redefinition of 'const int levelPin'

You have these:

const int levelPin = 12;     // the number of water level sensor pin
const int levelPin = 3;     // the number of water level sensor pin

I get that you might like someone to check that your code will do what you want (although I'm not sure why you can't do that yourself) but surely you can at least provide code that compiles?

// constants won't change. They're used here to set pin numbers:

const int waterPin =  13;   // the number of the water solenoid pin
const int levelPin = 12;     // the number of water level sensor pin
const int pumpPin = 11;      // the number of water pump pin
const int fanPin = 10;       // the number of the fan pin
const int drainPin = 9;     // the number of the drain solenoid
const int mainPin = 2;      // the number of the main power pin

// variables will change:
int mainState = 0;         // variable for reading the mainpower status
int levelState = 0;        // variable for reading the water level status

void setup() {
  // initialize the water pin as an output:
  pinMode(waterPin, OUTPUT);
  // initialize the pump pin as an output:
  pinMode (pumpPin, OUTPUT);
  // initialize the fan motor pin as an output:
  pinMode (fanPin, OUTPUT);
  // initialize the drain solenoid pin as an output:
  pinMode(drainPin, OUTPUT);
  // initialize the main power pin as an input:
  pinMode(mainPin, INPUT);
  // initialize the level pin as an intput:
  pinMode(levelPin, INPUT);
 }

void loop() {
  // read the state of the main power value:
  mainState = digitalRead(mainPin);
  // read the state of the water level value:
  levelState = digitalRead(levelPin);
  // check if the main power is on and water level is empty (sensor = High); MainState = high and waterState = High:
  if (mainState == HIGH && levelState == HIGH) {
      // turn pump off, water on and delay fan on for 60 seconds: {
      digitalWrite(pumpPin, LOW);
      digitalWrite(waterPin, HIGH);
      delay(60000);
      digitalWrite(fanPin, HIGH);
  // check if the main power is off; MainState = Low and waterState = High/Low: 
  } else if (mainState == LOW) {
    // turn all off and drain water:
    digitalWrite(waterPin, LOW);
    digitalWrite(pumpPin, LOW);
    digitalWrite(fanPin, LOW);
    digitalWrite(drainPin, HIGH);
    delay(30000);
    digitalWrite(drainPin, LOW);
  // check if water level is full(sensor = Low); MainState = high and waterState = Low    
   }  else {
    // turn water off and start pump:
    digitalWrite(waterPin, LOW);
    digitalWrite(pumpPin, HIGH);
        
  }
}

Sorry the correct verified code above;
100% sure its empty, drain valve is a dump valve, tank empty in a few seconds
"What will happen if the water level at start up is low, but not 60s' worth of supply low, just a "bit" low. You have a delay of 60s to fill it up, so if it's full after 5s you have 55s' worth of overflow''
The fill up process is not important, just to prevent warm air from getting in, the 60s delay only the fan startup, it cannot overfill as the level switch will cut off the water valve and start the pump.
Thanks, your state diagram it is spot on, except for the 60sec delay on the fan start
My question; I'm not sure about the if, else if, and else if it is in the correct order. this is my first program.
Thanks