Can Someone Please share the suggestion to me in my PROJECT.

Hello to All,

I am working on one college project but fail to complete it due to some coding issue.

As im new to coding and trying this project from last 2months but still im stuck.

Everyone please suggest were im wrong in coding and how it can be made .

My project is concern about the pressure sensor and pump .

1-i want to set the pressure level with button1 and button2 which will display on LCD......Which i have done.

2-after that i will press button4 to set the ON and OFF time to run the motor.once time is set i will press button3 and process start. which i am not able to do.
i want to set the time duration in Minutes .

I am attaching my schematic of project......and code below.

Please please help,teach,suggest......

#include <LiquidCrystal.h>

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

const byte button1=  7;        //----------------Increment------------------------//
const byte button2=  8; 
const byte button3=  9;    //----------------Decrement------------------------//
const int button4=   6;
int pump= 50;
//int sensor=A1;

//--------------------------------------------------------------------------------------------------------------------//

unsigned long duration=  0;  /////.......how long to run pump in minute.....
unsigned long starttime= 0; ////........how long to run pump in minute.....
int startime=0;
int stoptime=0;            ///
int state=LOW;
//-------------------------------------------------------------------------------------------------------------------//

int buttonPushCounter =0;   ////.........initial value..........
int buttonState1      =0;   ////.........initial value..........
int buttonState2      =0;   ////.........initial value..........
int buttonState3      =0;
int buttonState4      =0;
int lastButtonState  =0 ;   ////.........initial value..........
 //volatile int sen =2;
void setPressure();         ////.........set pressure sensor function.........
//unsigned long runCycle();   ///..........set runcycle start and stop the motor..........
void welCome();
//void Liquid();
void Vaccum();
void Lcd();
void setup() {
  
  // put your setup code here, to run once:
   pinMode(button1, INPUT);  ///.........Up arrow increment button.....
   pinMode(button2, INPUT); ///.........down arrow decrement button....
   pinMode(sensor,INPUT); ///...........Pressure sensor......
   pinMode(button3, INPUT);    ///............Enter buttton........   
   pinMode(pump,OUTPUT); ///........... pump.........
   pinMode(8,OUTPUT);
   pinMode(button4,INPUT);
//   attachInterrupt(0,Liquid,CHANGE);
 Serial.begin(9600);
  lcd.begin(16,2);
  lcd.display();
  lcd.setCursor(3,0);
  lcd.print("HELLO");
  lcd.setCursor(2,1);
  lcd.print("WELCOME YOU");
  
  delay(5000);
  if(buttonState3==LOW)
    {
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("PRESS ENTER"); 
      lcd.setCursor(3,1);
      lcd.print("TO BEGIN"); 
      delay(1000);
      lcd.clear();
    }
 
  
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonState1 = digitalRead(button1);
  int buttonState2 = digitalRead(button2);
  int buttonState3 = digitalRead(button3);
  int buttonState4=digitalRead(button4);
int state=LOW;
 
    
setPressure();
   
  
if (buttonState1 != lastButtonState)
{
 
if (buttonState1 == HIGH)
{
      buttonPushCounter+=10;
    
      buttonPushCounter=constrain(buttonPushCounter,10,90);
      lcd.setCursor(2,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("mmgh");
      delay(500);
}

}

lastButtonState = buttonState1;
 
if (buttonState2 != lastButtonState)
  if(buttonState2==HIGH)
{
  {
    
    buttonPushCounter-=10;
    
    buttonPushCounter=constrain(buttonPushCounter,10,100);
      lcd.setCursor(2,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("mmgh");
       delay(500);
  }
}
lastButtonState = buttonState2;
   

if(buttonState4==HIGH)
{

  Vaccum();
}
 
}

//------------------------------------------------------Welcome dispaly function-------------------------------------------------//

  void welCome()
{
 if(buttonState3==LOW)
    {
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.print("PRESS ENTER"); 
      lcd.setCursor(3,1);
      lcd.print("TO BEGIN"); 
      delay(1000);
      lcd.clear();
    }
  
  }


//------------------------------------------------------Set pressure value----------------------------------------------------//

void setPressure()
{
  
  lcd.setCursor(0,0);
  lcd.print("SET PRESSURE"); 
  lcd.setCursor(0,1);
  lcd.print("P=");
  

}

///---------------------- display Vaccum set time ----------------------------
 void Vaccum()
 {
  lcd.clear();
 buttonState4=digitalRead(button4);
 while(buttonState4==HIGH )
  {
  
  lcd.setCursor(0,0);
  lcd.print("Vaccum ON");
  lcd.setCursor(0,1);
  lcd.print("T=");
  delay(1000);
 
  if (buttonState1 != lastButtonState)
{
 
if (buttonState1 == HIGH)
{
      buttonPushCounter++;
    
      buttonPushCounter=constrain(buttonPushCounter,1,10);
      lcd.setCursor(3,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("Minutes");
      delay(500);
}

}

lastButtonState = buttonState1;
 
if (buttonState2 != lastButtonState)
  if(buttonState2==HIGH)
{
  {
    
    buttonPushCounter--;
    
    buttonPushCounter=constrain(buttonPushCounter,1,10);
      lcd.setCursor(3,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("Minutes");
       delay(500);
  }
}
lastButtonState = buttonState2;
  }  
 }

Try using the auto format tool in the IDE (ctrl-T) and see if anything shows itself as a problem.
Hint: this

  {
    {

almost never happens in C/C++
(Your code, as posted, doesn't compile, BTW
Also, "vacuum", not "vaccum")

Thanks Awol Sir for your time ans sugguestion.

I will check that ...

you can give better name to variables. For e.g. up button variable can be named as

byte upButton=7
byte downButton=8

it will help the readers to understand the code easily.

unsigned long starttime= 0; ////........how long to run pump in minute.....

int startime=0;

startime and starttime in parallel is a very confusing idea.
Comments should be meaningful and variable names should tell their meaning.

If you have a version that works but is not yet complete, first remove all unnecessary garbage not used for this limited functionality, tidy it up (comments, variable names as above) and test if it still works(!). Save it, be happy and proud, then try to add the missing functionality. Then come back here, if still necessary.

That's my first suggestions to share, sorry.

const byte button1=  7;        //----------------Increment------------------------//
const byte button3=  9;    //----------------Decrement------------------------//

If the names reflected what the switches are actually used for, the useless (and uselessly long) comments wouldn't be needed.

unsigned long starttime= 0; ////........how long to run pump in minute.....

That's a dumb name for duration, or a dumb comment for start time. Which one is dumb?

unsigned long starttime= 0; ////........how long to run pump in minute.....
int startime=0;

Why do you have two variables with such similar names? Have you noticed that ALL of the examples use camelCase for the names of variables? Get your SHIFT key fixed or learn to bang it now and then.

 int buttonState1 = digitalRead(button1);
  int buttonState2 = digitalRead(button2);
  int buttonState3 = digitalRead(button3);
  int buttonState4=digitalRead(button4);

Local variables with the same name as global variables are rarely a good idea.

I seriously doubt that the global variables with those names are needed.

int state=LOW;

State of what?

if (buttonState2 != lastButtonState)
  if(buttonState2==HIGH)
{
  {

That
just
wrong
looks.

 void welCome()
{
 if(buttonState3==LOW)

It would make more sense to only call this function if needed.

I gave up on reading the code here. I was going cross-eyed.

which i am not able to do.

You are not able to push the start button, but you are able to push the other buttons. Why on earth can't you push the start button?

Sir as i tested for auto format its says no change required.

I test same code its compiling in my IDE.

im pasting same again.

#include <LiquidCrystal.h>



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

const byte button1 =  7;       //----------------Increment------------------------//
const byte button2 =  8;
const byte button3 =  9;   //----------------Decrement------------------------//
const int button4 =   6;
int pump = 50;
int sensor = A1;

//--------------------------------------------------------------------------------------------------------------------//

unsigned long duration =  0; /////.......how long to run pump in minute.....
unsigned long starttime = 0; ////........how long to run pump in minute.....
int startime = 0;
int stoptime = 0;          ///
int state = LOW;
//-------------------------------------------------------------------------------------------------------------------//

int buttonPushCounter = 0;  ////.........initial value..........
int buttonState1      = 0;  ////.........initial value..........
int buttonState2      = 0;  ////.........initial value..........
int buttonState3      = 0;
int buttonState4      = 0;
int lastButtonState  = 0 ;  ////.........initial value..........
//volatile int sen =2;
void setPressure();         ////.........set pressure sensor function.........
//unsigned long runCycle();   ///..........set runcycle start and stop the motor..........
void welCome();
//void Liquid();
void Vaccum();
void Lcd();
void setup() {

  // put your setup code here, to run once:
  pinMode(button1, INPUT);  ///.........Up arrow increment button.....
  pinMode(button2, INPUT); ///.........down arrow decrement button....
  pinMode(sensor, INPUT); ///...........Pressure sensor......
  pinMode(button3, INPUT);    ///............Enter buttton........
  pinMode(pump, OUTPUT); ///........... pump.........
  pinMode(8, OUTPUT);
  pinMode(button4, INPUT);
  //   attachInterrupt(0,Liquid,CHANGE);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.display();
  lcd.setCursor(3, 0);
  lcd.print("HELLO");
  lcd.setCursor(2, 1);
  lcd.print("WELCOME YOU");

  delay(5000);
  if (buttonState3 == LOW)
  {
    lcd.clear();
    lcd.setCursor(2, 0);
    lcd.print("PRESS ENTER");
    lcd.setCursor(3, 1);
    lcd.print("TO BEGIN");
    delay(1000);
    lcd.clear();
  }


}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonState1 = digitalRead(button1);
  int buttonState2 = digitalRead(button2);
  int buttonState3 = digitalRead(button3);
  int buttonState4 = digitalRead(button4);
  int state = LOW;


  setPressure();


  if (buttonState1 != lastButtonState)
  {

    if (buttonState1 == HIGH)
    {
      buttonPushCounter += 10;

      buttonPushCounter = constrain(buttonPushCounter, 10, 90);
      lcd.setCursor(2, 1);
      lcd.print(buttonPushCounter);
      lcd.setCursor(5, 1);
      lcd.print("mmgh");
      delay(500);
    }

  }

  lastButtonState = buttonState1;

  if (buttonState2 != lastButtonState)
    if (buttonState2 == HIGH)
    {
      {

        buttonPushCounter -= 10;

        buttonPushCounter = constrain(buttonPushCounter, 10, 100);
        lcd.setCursor(2, 1);
        lcd.print(buttonPushCounter);
        lcd.setCursor(5, 1);
        lcd.print("mmgh");
        delay(500);
      }
    }
  lastButtonState = buttonState2;


  if (buttonState4 == HIGH)
  {
    Vaccum();
  }
}

//------------------------------------------------------Welcome dispaly function-------------------------------------------------//

void welCome()
{
  if (buttonState3 == LOW)
  {
    lcd.clear();
    lcd.setCursor(2, 0);
    lcd.print("PRESS ENTER");
    lcd.setCursor(3, 1);
    lcd.print("TO BEGIN");
    delay(1000);
    lcd.clear();
  }

}


//------------------------------------------------------Set pressure value----------------------------------------------------//

void setPressure()
{
  lcd.setCursor(0, 0);
  lcd.print("SET PRESSURE");
  lcd.setCursor(0, 1);
  lcd.print("P=");
}

///---------------------- display Vaccum set time ----------------------------
void Vaccum()
{
  lcd.clear();
  buttonState4 = digitalRead(button4);
  while (buttonState4 == HIGH )
  {

    lcd.setCursor(0, 0);
    lcd.print("Vaccum ON");
    lcd.setCursor(0, 1);
    lcd.print("T=");
    delay(1000);

    if (buttonState1 != lastButtonState)
    {

      if (buttonState1 == HIGH)
      {
        buttonPushCounter++;

        buttonPushCounter = constrain(buttonPushCounter, 1, 10);
        lcd.setCursor(3, 1);
        lcd.print(buttonPushCounter);
        lcd.setCursor(5, 1);
        lcd.print("Minutes");
        delay(500);
      }

    }

    lastButtonState = buttonState1;

    if (buttonState2 != lastButtonState)
      if (buttonState2 == HIGH)
      {
        {

          buttonPushCounter--;

          buttonPushCounter = constrain(buttonPushCounter, 1, 10);
          lcd.setCursor(3, 1);
          lcd.print(buttonPushCounter);
          lcd.setCursor(5, 1);
          lcd.print("Minutes");
          delay(500);
        }
      }
    lastButtonState = buttonState2;
  }
}

PaulS:
You are not able to push the start button, but you are able to push the other buttons. Why on earth can't you push the start button?

LOL

Please do the modifications as suggested by others, give proper names to variables. Then come back and post the updated sketch.

Thanks every one for reply i will design the code as per suggestion for easy understanding.

with positive hope im going to change all the worng set in code and come back very soon .

all suggestion was helpfull and had good learning.

i like this platform...thanks once againg to everyone.

Hey guys don't you all think that this is a consider a case of XY problem:

the various post initiated by rohitturns:

  1. <how to run motor by set time using push button >
  2. <how Check sensor level after some defined mintue, if not turn off machine>
  3. <setup of time duration with pushbutton to start and stop motor ( by user )>
  4. <Button press function>

Anyway I can see that he is a very consistent person though....

@ rohitturns:

I do not have all the hardware that you have for your set up so I could not try the coding on my side...
To be honest you should change the way you name your variable..

Lets say it is me doing the project....

I would first define the Input and Output of the project and also its respective pin....

Input:

  1. Increment Switch 2
  2. Decrement Switch 3
  3. Start Switch 4
  4. Set Switch 5
  5. Pressure sensor A0

Output:

  1. LCD ( well this pin is up to you )
  2. Pump ( what so ever the pin number is )

then I will define the process....

I would Imagine I would love to have a Welcoming Screen when the system first start up...
If I press Start switch, The LCD display asking to set the Pressure Setting... The setting could be change using increment or decrement switch and be set using the set switch. After the set switch have been press, The LCD display asking to set the duration of the system to be on.... again both increment or decrement switch can manipulate the variable of the display, pressing the set switch move the LCD to display a forth Screen asking the user to press the start switch... from the moment the user press the start switch, Pump will be turn on... after some time ( the duration set in the third screen ), The system check the pressure sensor, ( depending on the code ) ( maybe if pressure is less or maybe is could be more the the set pressure point ( set in screen 2 ) the motor is turn off and the LCD turn back to Welcoming screen. One added extra if i could use the Start switch at any moment to skip to the end using the predefine value ...

I hope so far this is consistent with what you wanted to do...

Yes Exactly this what i want and i am trying.....

Thanks for your time and reading my questions posted by me......

If you c sir all my forum question bring my one project .

I am changing mistake which i did in code as suggested for Forum member.

please add ur suggestion too.

Hello Everyone.

Thanks for suggestions......

i done changes in my code as per my understanding and feedback by forum member....

i still confirm my Vaccum function code is wrong but i dnt have any idea about how to design it .

2nd problem in setPressure function while loop remain ON and wont allow to run or go on other function.

please check code once as per changes and easy name of variables..

#include <LiquidCrystal.h>

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

const byte button1 =  7;     //Increment button
const byte button2 =  8;     //Decrement button
const byte button3 =  9;     //button to enter in setpressure level
const int  button4 =  6;     //button to enter in set time for pump to ON and OFF
int pump = 50;
int sensor = A1;              
//----------------------------------------------------------------------------

int buttonPushCounter = 0;  
int upbutton          = 0;  
int downbutton        = 0;  
int enterbutton       = 0;
int setpumptime       = 0;
int lastButtonState   = 0;  
//------------------------------------------------------------------------

void setPressure();         //setpressure sensor function.
void welCome();             // WELCOME DISPLAY
void Vaccum();              // SET TIME DURATION OF PUMP 

//-------------------------------------------------------------------------

void setup() {

  // put your setup code here, to run once:
  pinMode(button1, INPUT);  
  pinMode(button2, INPUT); 
  pinMode(button3, INPUT); 
  pinMode(button4, INPUT);   
  pinMode(pump, OUTPUT); 
  pinMode(sensor, INPUT); 
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.display();
  lcd.setCursor(3, 0);
  lcd.print("HELLO");
  lcd.setCursor(2, 1);
  lcd.print(" DEAR JUDGE");
  delay(5000);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  int Upbutton    = digitalRead(button1);
  int Downbutton  = digitalRead(button2);
  int Enterbutton = digitalRead(button3);
  int SetPumpTime = digitalRead(button4);
  int sensorVal=  digitalRead(sensor);
 
  welCome();

if(Enterbutton==HIGH)
  {
    setPressure();
  }

if (SetPumpTime == HIGH)
      {
        Vaccum();
      }

  
}

//--Welcome dispaly function----//

void welCome()
{
  int Enterbutton=digitalRead(button3);  
  if (Enterbutton == LOW)
  {
    lcd.clear();
    lcd.setCursor(2, 0);
    lcd.print("PRESS ENTER");
    lcd.setCursor(3, 1);
    lcd.print("TO BEGIN");
    delay(1000);
    lcd.clear();
  }

}


//--Set pressure value-//

void setPressure()
{  
int sensorVal=  digitalRead(sensor);
int Enterbutton=digitalRead(button3);  
while(Enterbutton== HIGH)
{
  lcd.setCursor(0,0);
  lcd.print("SET PRESSURE"); 
  lcd.setCursor(0,1);
  lcd.print("P="); 
  lcd.print(buttonPushCounter);

int Upbutton = digitalRead(button1);
 
if (Upbutton!= lastButtonState)
{
 
if (Upbutton == HIGH)
{
      buttonPushCounter+=10;
      buttonPushCounter=constrain(buttonPushCounter,10,90);
     
      lcd.setCursor(2,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("mmgh");
      delay(500);
}

}

lastButtonState = Upbutton;

int Downbutton = digitalRead(button2);
 
if (Downbutton != lastButtonState)
{
  if(Downbutton==HIGH)

  {
    buttonPushCounter-=10;
    buttonPushCounter=constrain(buttonPushCounter,10,100);
      
      lcd.setCursor(2,1); 
      lcd.print(buttonPushCounter);
      lcd.setCursor(5,1);
      lcd.print("mmgh");
      delay(500);
  }
 
}


lastButtonState = Downbutton;

}

}


//-- display Vaccum set time --


void Vaccum()
{
  lcd.clear();
  int Upbutton=digitalRead(button1);
  int Downbutton=digitalRead(button2);
  int SetPumpTime = digitalRead(button4);
  while(SetPumpTime == HIGH )
  {

    lcd.setCursor(0, 0);
    lcd.print("Vaccum ON");
    lcd.setCursor(0, 1);
    lcd.print("T=");
    delay(1000);

      if (Upbutton == HIGH)
      {
        buttonPushCounter++;

        buttonPushCounter = constrain(buttonPushCounter, 1, 10);
        lcd.setCursor(3, 1);
        lcd.print(buttonPushCounter);
        lcd.setCursor(5, 1);
        lcd.print("Minutes");
        delay(500);
      }

    }

    lastButtonState = Upbutton;

      if (Downbutton == HIGH)
      {
          buttonPushCounter--;

          buttonPushCounter = constrain(buttonPushCounter, 1, 10);
          lcd.setCursor(3, 1);
          lcd.print(buttonPushCounter);
          lcd.setCursor(5, 1);
          lcd.print("Minutes");
          delay(500);
        }
      }
    lastButtonState = Downbutton;
  }


}

Posted by Ashraf_Zolkopli

I would Imagine I would love to have a Welcoming Screen when the system first start up...
If I press Start switch, The LCD display asking to set the Pressure Setting... The setting could be change using increment or decrement switch and be set using the set switch. After the set switch have been press, The LCD display asking to set the duration of the system to be on.... again both increment or decrement switch can manipulate the variable of the display, pressing the set switch move the LCD to display a forth Screen asking the user to press the start switch... from the moment the user press the start switch, Pump will be turn on... after some time ( the duration set in the third screen ), The system check the pressure sensor, ( depending on the code ) ( maybe if pressure is less or maybe is could be more the the set pressure point ( set in screen 2 ) the motor is turn off and the LCD turn back to Welcoming screen. One added extra if i could use the Start switch at any moment to skip to the end using the predefine value ...

Thats what i want to code....

Rohit, when I say variable names means it include

const byte button1 =  7;     //Increment button[color=#222222][/color]
const byte button2 =  8;     //Decrement button[color=#222222][/color]
const byte button3 =  9;     //button to enter in setpressure level[color=#222222][/color]
const int  button4 =  6;     //button to enter in set time for pump to ON and OFF

why cant you change it like

#define INCREMENT_PIN 7
#define DECREMENT_PIN 8
#define SET_PRESSURE_PIN 9
#define SET_PUMP_TIME_PIN 6;

Now the code reviewer will know that, what pin he is looking at, just by seeing the name of it.

Enterbutton = digitalRead(button3);

add that code again inside the while loop of setPressure.

SetPumpTime = digitalRead(button4);

add that code again inside the while loop of vaccum function.

I assume your objective is to press and hold button3 to enter into set pressure state. Same like press and hold button4 to enter Vaccum settings changes.

The issue here once this button is high and enter into while loop and loop will continue for ever. Because the variable's value is not changing inside the loop.

Note: change the name of these button3 and button4 ASAP.

@ rohitturns:

Ouh my god rohitturns, You haven't implement a single suggestion that we had given to you...

why cant you put something like

const uint8_t incrementSwitch = 7;
const uint8_t decrementSwitch = 8;
const uint8_t startSwitch = 9;
const uint8_t SetSwitch = 6;

for the debouncing and state change detection... Why not use a library like the Bounce2 library?

for the menu just use a Finite state Machine method...

enum menu
{
  WelcomeScreen,
  TimeScreen,
  PressureScreen,
  StartScreen,
} LCDMenu;

Let's not complicate things with some extra libraries like Bounce2 or Finite state machine.

I believe his system will work if he fix some bugs.

Sony sir my objective is when I press button3 it should show and enter into setPressure function.

And once button4 is press it should show and enter into Vacuum function.

I change variable name of button3 and button4 as u said.

Ashraf_Zolkopli sir thanks for reply...

I'm new in coding but I will work hard as per all members suggestion.

And with everyone suggestions I will make this project successful.

please check my post #14

Ok sir