Pump Controller

I am working on a project on water level controller with 3 phase motor starter. I have gone through a thread where you suggested to define enum and switch/case i have tried the method with the codes as an example.But the output I'm getting as the relay doesn't turn off after 2 sec but LCD is showing "pump off".

p.s- Im a newbie to microcontrollers..

this is how I wrote down the example code for simulation…
I’m lost in understanding the error…
plz can you help?

#include <LiquidCrystal.h>




int qut=A1;
int hlf=A2;
int thf=A3;
int ful=A4;

int motor=8; //relay01 (need always relay on condition for 2 seconds and after 2 second its need to goes off condition)

int nvc=7;   //relay02 (need always relay on condition for 2 seconds and after 2 second its need to goes off condition)



int i;
int q;
int h;
int t;
int f;



int v=100; //comparison variable

int m=0;   //motor 



int n=0;    //DOL nvc 


  enum MOTORCONTROL {
  WAIT_EMPTY,         // wait till tank is empty
  MOTOR_ON,           // activate ON relay
  MOTOR_ON_WAIT,      // wait 2 seconds till motor activated
  MOTOR_ON_COMPLETE,  // de-activate ON relay
  WAIT_FULL,          // wait till tank is full
  MOTOR_OFF,          // activate OFF relay
  MOTOR_OFF_WAIT,     // wait 2 seconds till motor de-activated
  MOTOR_OFF_COMPLETE  // de-activate OFF relay

};

  MOTORCONTROL motorControl = WAIT_EMPTY;



LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){


pinMode(qut,INPUT);
pinMode(hlf,INPUT);
pinMode(qut,INPUT);
pinMode(ful,INPUT);

pinMode(motor,OUTPUT);


pinMode(nvc,OUTPUT);

lcd.begin(16, 2);


digitalWrite(nvc,LOW);   // its for  stop


digitalWrite(motor,LOW);


lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("RoboPump WATER");
 lcd.setCursor(0,1);
 lcd.print("LEVEL CONTROLLER");
 delay(100);

}

void loop()
{

i=digitalRead(motor);

n=digitalRead(nvc);

q=analogRead(qut);
h=analogRead(hlf);
t=analogRead(thf);
f=analogRead(ful);
lcd.clear();

if(f>v && t>v && h>v && q>v)
{

lcd.setCursor(0,0);
lcd.print(char(255));
lcd.print(char(255));
lcd.print(char(255));
lcd.print(char(255));
lcd.setCursor(5,0);
lcd.print("TANK FULL");
m=0;

}

else
{
if(f<v && t>v && h>v && q>v)
{

lcd.setCursor(0,0);
lcd.print(char(255));
lcd.print(char(255));
lcd.print(char(255));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("LEVEL 03");

}
else
{
if(f<v && t<v && h>v && q>v)
{

lcd.setCursor(0,0);
lcd.print(char(255));
lcd.print(char(255));
lcd.print(char(219));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("LEVEL 02");

}
else
if(f<v && t<v && h<v && q>v)
{

lcd.setCursor(0,0);
lcd.print(char(255));
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("LEVEL 01");

}
else
{
if(f<v && t<v && h<v && q<v)
{
lcd.setCursor(0,0);
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.print(char(219));
lcd.setCursor(5,0);
lcd.print("TANK EMPTY");

m=1;

}
else
{

lcd.setCursor(0,0);
lcd.print("ERROR!");

m=0;
}
}}}

if(m==HIGH)
{
lcd.setCursor(0,1);
lcd.print("PUMP ON");

}

else
{
lcd.setCursor(0,1);
lcd.print("PUMP OFF");
}


  switch (motorControl)
  {
    case WAIT_EMPTY:
      if (m == 1)
      {
        motorControl = MOTOR_ON;
      }
      break;
    case MOTOR_ON:
      digitalWrite(motor, HIGH);
      motorControl = MOTOR_ON_WAIT;
      break;
    case MOTOR_ON_WAIT:
      delay(2000);
      motorControl = MOTOR_ON_WAIT;
      break;
    case MOTOR_ON_COMPLETE:
      digitalWrite(motor, LOW);
      motorControl = WAIT_FULL;
      break;
    case WAIT_FULL:
      if (m == 0)
      {
        motorControl = MOTOR_OFF;
      }
      break;
    case MOTOR_OFF:
      digitalWrite(nvc, HIGH);
      motorControl = MOTOR_ON_WAIT;
      break;
    case MOTOR_OFF_WAIT:
      delay(2000);
      motorControl = MOTOR_ON_WAIT;
      break;
    case MOTOR_OFF_COMPLETE:
      digitalWrite(nvc, LOW);
      motorControl = WAIT_EMPTY;
      break;
  }



delay(100);
lcd.clear();

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

there was one bug I could locate that in in the

void setup()
pinMode()

which I fixed
i.e qut was set input 2 times

Rickypaul:
i.e qut was set input 2 times

Google "idempotency"

do I have to edit more for replacement with your code??

i dint get that..

anyone please help me with the code please.

thank you.

Hi,
@Rickypaul, the problem is you have HiJacked this thread from the original poster.
This makes it very difficult as you are asking us to solve your problem with your code while we try to help the Original Poster with his.

I have posted to the moderator to try and fix this problem so we can work separately with each problem.

Tom... :slight_smile:

@Rickypaul, do not hijack. Thread split.

Rickypaul:
plz can you help?

I can't. What little time I can spend on this forum was wasted cleaning up your mess.

Rickypaul:
I am working on a project on water level controller with 3 phase motor starter. I have gone through a thread where you suggested to define enum and switch/case i have tried the method with the codes as an example.But the output I’m getting as the relay doesn’t turn off after 2 sec but LCD is showing “pump off”.

p.s- Im a newbie to microcontrollers…

Unfortunately there was a bug in the statemachine. I fixed it in the thread that was hijacked.

Below the complete sketch with the part that I fixed.

#include <LiquidCrystal.h>

int qut = A1;
int hlf = A2;
int thf = A3;
int ful = A4;

int motor = 8; //relay01 (need always relay on condition for 2 seconds and after 2 second its need to goes off condition)

int nvc = 7; //relay02 (need always relay on condition for 2 seconds and after 2 second its need to goes off condition)

int i;
int q;
int h;
int t;
int f;

int v = 100; //comparison variable

int m = 0; //motor
int n = 0;  //DOL nvc


enum MOTORCONTROL {
  WAIT_EMPTY,         // wait till tank is empty
  MOTOR_ON,           // activate ON relay
  MOTOR_ON_WAIT,      // wait 2 seconds till motor activated
  MOTOR_ON_COMPLETE,  // de-activate ON relay
  WAIT_FULL,          // wait till tank is full
  MOTOR_OFF,          // activate OFF relay
  MOTOR_OFF_WAIT,     // wait 2 seconds till motor de-activated
  MOTOR_OFF_COMPLETE  // de-activate OFF relay

};

MOTORCONTROL motorControl = WAIT_EMPTY;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  pinMode(qut, INPUT);
  pinMode(hlf, INPUT);
  pinMode(qut, INPUT);
  pinMode(ful, INPUT);

  pinMode(motor, OUTPUT);
  pinMode(nvc, OUTPUT);

  lcd.begin(16, 2);

  digitalWrite(nvc, LOW);  // its for  stop
  digitalWrite(motor, LOW);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("RoboPump WATER");
  lcd.setCursor(0, 1);
  lcd.print("LEVEL CONTROLLER");
  delay(100);

}

void loop()
{

  i = digitalRead(motor);

  n = digitalRead(nvc);

  q = analogRead(qut);
  h = analogRead(hlf);
  t = analogRead(thf);
  f = analogRead(ful);
  lcd.clear();

  if (f > v && t > v && h > v && q > v)
  {

    lcd.setCursor(0, 0);
    lcd.print(char(255));
    lcd.print(char(255));
    lcd.print(char(255));
    lcd.print(char(255));
    lcd.setCursor(5, 0);
    lcd.print("TANK FULL");
    m = 0;

  }

  else
  {
    if (f < v && t > v && h > v && q > v)
    {

      lcd.setCursor(0, 0);
      lcd.print(char(255));
      lcd.print(char(255));
      lcd.print(char(255));
      lcd.print(char(219));
      lcd.setCursor(5, 0);
      lcd.print("LEVEL 03");

    }
    else
    {
      if (f < v && t < v && h > v && q > v)
      {

        lcd.setCursor(0, 0);
        lcd.print(char(255));
        lcd.print(char(255));
        lcd.print(char(219));
        lcd.print(char(219));
        lcd.setCursor(5, 0);
        lcd.print("LEVEL 02");

      }
      else if (f < v && t < v && h < v && q > v)
      {

        lcd.setCursor(0, 0);
        lcd.print(char(255));
        lcd.print(char(219));
        lcd.print(char(219));
        lcd.print(char(219));
        lcd.setCursor(5, 0);
        lcd.print("LEVEL 01");

      }
      else
      {
        if (f < v && t < v && h < v && q < v)
        {
          lcd.setCursor(0, 0);
          lcd.print(char(219));
          lcd.print(char(219));
          lcd.print(char(219));
          lcd.print(char(219));
          lcd.setCursor(5, 0);
          lcd.print("TANK EMPTY");

          m = 1;

        }
        else
        {

          lcd.setCursor(0, 0);
          lcd.print("ERROR!");

          m = 0;
        }
      }
    }
  }

  if (m == HIGH)
  {
    lcd.setCursor(0, 1);
    lcd.print("PUMP ON");

  }

  else
  {
    lcd.setCursor(0, 1);
    lcd.print("PUMP OFF");
  }


  switch (motorControl)
  {
    case WAIT_EMPTY:
      if (m == 1)
      {
        motorControl = MOTOR_ON;
      }
      break;
    case MOTOR_ON:
      digitalWrite(motor, HIGH);
      motorControl = MOTOR_ON_WAIT;
      break;
    case MOTOR_ON_WAIT:
      delay(2000);
      motorControl = MOTOR_ON_COMPLETE;
      break;
    case MOTOR_ON_COMPLETE:
      digitalWrite(motor, LOW);
      motorControl = WAIT_FULL;
      break;
    case WAIT_FULL:
      if (m == 0)
      {
        motorControl = MOTOR_OFF;
      }
      break;
    case MOTOR_OFF:
      digitalWrite(nvc, HIGH);
      motorControl = MOTOR_OFF_WAIT;
      break;
    case MOTOR_OFF_WAIT:
      delay(2000);
      motorControl = MOTOR_OFF_COMPLETE;
      break;
    case MOTOR_OFF_COMPLETE:
      digitalWrite(nvc, LOW);
      motorControl = WAIT_EMPTY;
      break;
  }

  delay(100);
  lcd.clear();
}