Error not getting solved near if statement


#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘4’ },
{‘5’,‘6’,‘7’,‘8’ },
{‘9’,‘0’,‘Z’,‘E’ },
{‘A’,‘B’,‘C’,‘S’ }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10, 5 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int adj= 4; //per litre delay
int pump =13;
int pass=1234;
int paas;
String in;
String vol;
int voll;
int balvol;
int l;
int tiime;
int cutime;
int ontime;
byte pumpstate;

void setup() {

lcd.begin(20, 4);
lcd.print(“Welcome To”);
lcd.setCursor(0, 1);
lcd.print(" 9~3~7 ");
delay(5000);
lcd.clear();

}

void loop()
{
defafunc();
keytillenter();

if(passcheck()==1)
{
lcd.clear();
lcd.print(“Correct Password”);
lcd.clear();

pumpinp();
pumpcalc();
timed_pump(tiime);

}

}

void timed_pump(int max_time)
{
digitalWrite(13,HIGH);
int initial_time = millis();
int elapsed_time = 0;
char key;
while( (elapsed_time < max_time) && ( if(key==‘S’) )
{
elapsed_time = millis() - initial_time;
key = kpd.getKey();
}
digitalWrite(13,LOW);
return;
}

void pumpcalc()
{
tiime= volladj1000;}

void pumpinp()
{
lcd.clear();
for(int i=0;i>=0;)
{
delay(100);
lcd.clear();
lcd.print(“Enter Quantity :”);
lcd.setCursor(0, 1);
lcd.print(vol);
char key = kpd.getKey();
if(key)
switch (key)
{
case ‘1’:
in=in+1;
break;

case ‘2’:
in=in+2;
break;

case ‘3’:
in=in+3;
break;

case ‘4’:
in=in+4;
break;

case ‘5’:
in=in+5;
break;

case ‘6’:
in=in+6;
break;

case ‘7’:
in=in+7;
break;

case ‘8’:
in=in+8;
break;

case ‘9’:
in=in+9;
break;

case ‘Z’:

l =in.length();
l=l-1;
in.remove(l);
break;

case ‘E’:
voll=vol.toInt();
vol.remove(0,(in.length()));
return;
break;

case ‘0’:
in=in+0;
break;

}

}
}

void defafunc()
{

lcd.clear();
lcd.print(“Balance:”+balvol);
lcd.setCursor(0,1);
lcd.print(“Press Enter”);
lcd.setCursor(0,2);
lcd.print(“To continue”);

for(int i=0;i>=0;)
{
char key = kpd.getKey();
if(key)
if(key== ‘E’)
return;
}

}

int passcheck()
{

if(paas==pass)
return 1;
else
return 0;
}

void keytillenter()
{
lcd.clear();
for(int i=0;i>=0;)
{
delay(100);
lcd.clear();
lcd.print(“Enter your password”);
lcd.setCursor(0, 1);
lcd.print(in);
char key = kpd.getKey();
if(key)
switch (key)
{
case ‘1’:
in=in+1;
break;

case ‘2’:
in=in+2;
break;

case ‘3’:
in=in+3;
break;

case ‘4’:
in=in+4;
break;

case ‘5’:
in=in+5;
break;

case ‘6’:
in=in+6;
break;

case ‘7’:
in=in+7;
break;

case ‘8’:
in=in+8;
break;

case ‘9’:
in=in+9;
break;

case ‘Z’:

l =in.length();
l=l-1;
in.remove(l);
break;

case ‘E’:
paas=in.toInt();
in.remove(0,(in.length()));
return;
break;

case ‘0’:
in=in+0;
break;

}

}
}

Problem is in timed motor function

If you really want help:- 1. Post your code within code tags, not inline. (See item #7 here:- How to use this forum ) 2. Format the code correctly before posting, (Ctrl-T in the IDE will do this). 3. Remove unnecessary blank lines. One blank line is sufficient to separate the code sections. 4. Describe the exact problem. ie What do you expect to happen, and what is actually happening.

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);



const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
 {'1', '2', '3', '4' },
 {'5', '6', '7', '8' },
 {'9', '0', 'Z', 'E' },
 {'A', 'B', 'C', 'S' }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10, 5 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int adj = 4; //per litre delay
int pump = 13;
int pass = 1234;
int paas;
String in;
String vol;
int voll;
int balvol;
int l;
int tiime;
int cutime;
int ontime;
byte pumpstate;



void setup() {

 lcd.begin(20, 4);
 lcd.print("Welcome To");
 lcd.setCursor(0, 1);
 lcd.print("    9~3~7   ");
 delay(5000);
 lcd.clear();

}

void loop()
{
 defafunc();
 keytillenter();

 if (passcheck() == 1)
 {
   lcd.clear();
   lcd.print("Correct Password");
   lcd.clear();


   pumpinp();
   pumpcalc();
   timed_pump(tiime);


 }

}



void timed_pump(int max_time)
{
 digitalWrite(13, HIGH);
 int initial_time = millis();
 int elapsed_time = 0;
 char key;
 while ( (elapsed_time < max_time) && ( if (key == 'S') )
 {
   elapsed_time = millis() - initial_time;
     key = kpd.getKey();
   }
 digitalWrite(13, LOW);
 return;
}


void pumpcalc()
{
 tiime = voll * adj * 1000;
}


void pumpinp()
{
 lcd.clear();
 for (int i = 0; i >= 0;)
 {
   delay(100);
   lcd.clear();
   lcd.print("Enter Quantity :");
   lcd.setCursor(0, 1);
   lcd.print(vol);
   char key = kpd.getKey();
   if (key)
     switch (key)
     {
       case '1':
         in = in + 1;
         break;

       case '2':
         in = in + 2;
         break;

       case '3':
         in = in + 3;
         break;

       case '4':
         in = in + 4;
         break;

       case '5':
         in = in + 5;
         break;

       case '6':
         in = in + 6;
         break;

       case '7':
         in = in + 7;
         break;

       case '8':
         in = in + 8;
         break;

       case '9':
         in = in + 9;
         break;


       case 'Z':

         l = in.length();
         l = l - 1;
         in.remove(l);
         break;

       case 'E':
         voll = vol.toInt();
         vol.remove(0, (in.length()));
         return;
         break;

       case '0':
         in = in + 0;
         break;

     }


 }
}

void defafunc()
{


 lcd.clear();
 lcd.print("Balance:" + balvol);
 lcd.setCursor(0, 1);
 lcd.print("Press Enter");
 lcd.setCursor(0, 2);
 lcd.print("To continue");

 for (int i = 0; i >= 0;)
 {
   char key = kpd.getKey();
   if (key)
     if (key == 'E')
       return;
 }

}

int passcheck()
{

 if (paas == pass)
   return 1;
 else
   return 0;
}



void keytillenter()
{
 lcd.clear();
 for (int i = 0; i >= 0;)
 {
   delay(100);
   lcd.clear();
   lcd.print("Enter your password");
   lcd.setCursor(0, 1);
   lcd.print(in);
   char key = kpd.getKey();
   if (key)
     switch (key)
     {
       case '1':
         in = in + 1;
         break;

       case '2':
         in = in + 2;
         break;

       case '3':
         in = in + 3;
         break;

       case '4':
         in = in + 4;
         break;

       case '5':
         in = in + 5;
         break;

       case '6':
         in = in + 6;
         break;

       case '7':
         in = in + 7;
         break;

       case '8':
         in = in + 8;
         break;

       case '9':
         in = in + 9;
         break;


       case 'Z':

         l = in.length();
         l = l - 1;
         in.remove(l);
         break;

       case 'E':
         paas = in.toInt();
         in.remove(0, (in.length()));
         return;
         break;

       case '0':
         in = in + 0;
         break;


     }


 }
}



[code]

[/code]

i want the motor to stay on till the time expires or when the user pushes the stop button on the keypad.

You didn’t mention that your code doesn’t even compile.
Anyway, I think you need to do a bit more reading of the examples, then start again. There are a lot of fundamental errors.

What are you trying to do here?:-

   for (int i = 0; i >= 0;)
    {
        char key = kpd.getKey();
        if (key)
            if (key == 'E')
                return;
    }

And in the above, this:-

if (key)
    if (key == 'E')
        return;

should look like this:-

if (key)
{
    if (key == 'E')
        return;
}

You need to rethink your method of doing this:-while ( (elapsed_time < max_time) && ( if (key == 'S') )(You can’t use an ‘if’ statement like that.)

You can’t print to the LCD like this:-lcd.print("Balance:" + balvol);
You could do this:-

lcd.print("Balance:");
lcd.print(balvol);

This will not work as you expect, either:-

lcd.clear();
lcd.print("Correct Password");
lcd.clear();

The display will be cleared, then “Correct Password” will be printed but immediately cleared again before you can see it.

Overall, as I said, do some more reading of the examples, then write your program a little at a time, checking for errors and that things perform as expected along the way. Don’t try to write it all at once.

  char key;
  while ( (elapsed_time < max_time) && ( if (key == 'S') )

Even if this compiled, how likely is it that key will be ‘S’ ?

It will be s if the user hits the stop button

albert937: It will be s if the user hits the stop button

Don't you think the while loop should run while key not equal 'S'?

You have complete control over what is in key when the while statement is initially evaluated. Don't let it be random garbage.

albert937: It will be s if the user hits the stop button

My apologies for not reading ahead, but you still need to fix that while statement. Get rid of the if as a start.

i solved the error by this

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);



const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1', '2', '3', '4' },
  {'5', '6', '7', '8' },
  {'9', '0', 'Z', 'E' },
  {'A', 'B', 'C', 'S' }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10, 5 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int adj = 4; //per litre delay
int pump = 13;
int pass = 1234;
int paas;
String in;
String vol;
int voll;
int balvol;
int l;
int tiime;
int cutime;
int ontime;
byte pumpstate;



void setup() {

  lcd.begin(20, 4);
  lcd.print("Welcome To");
  lcd.setCursor(0, 1);
  lcd.print("    9~3~7   ");
  delay(5000);
  lcd.clear();
  Serial.begin(38400);
  digitalWrite(13,LOW);

}

void loop()
{
  digitalWrite(13,LOW);
  defafunc();
  keytillenter();

  if (passcheck() == 1)
  {
    lcd.clear();
    lcd.print("Correct Password");
   
    delay(2000);
    lcd.clear();


    pumpinp();
    pumpcalc();
    timed_pump(tiime);


  }

}



void timed_pump(int max_time)
{Serial.print("pump");
  digitalWrite(13, HIGH);
  int initial_time = millis();
  int elapsed_time = 0;
  char key;
  boolean x;
  while ( (elapsed_time < max_time) && ( x ))
  {
    elapsed_time = millis() - initial_time;
      key = kpd.getKey();
      if(key=='E')
      x=0;
      else
      x=1;
    }
  digitalWrite(13, LOW);
  return;
}


void pumpcalc()
{
  tiime=0;
  Serial.print(voll);
  delay(2000);
  Serial.print(tiime);
  delay(2000);
  Serial.println(adj);
  delay(2000);
  double a=11*4*1000;
  Serial.print("A is");
  Serial.print(a);
  tiime = voll * adj * 1000;
  lcd.print("Time");
  Serial.print(tiime);
  delay(20000);
}


void pumpinp()
{
  lcd.clear();
  for (int i = 0; i >= 0;)
  {
    delay(100);
    lcd.clear();
    lcd.print("Enter Quantity :");
    lcd.setCursor(0, 1);
    lcd.print(vol);
    char key = kpd.getKey();
    if (key)
      switch (key)
      {
        case '1':
          vol = vol + 1;
          break;

        case '2':
          vol = vol + 2;
          break;

        case '3':
          vol = vol + 3;
          break;

        case '4':
          vol = vol + 4;
          break;

        case '5':
          vol = vol + 5;
          break;

        case '6':
          vol = vol + 6;
          break;

        case '7':
          vol = vol + 7;
          break;

        case '8':
          vol = vol + 8;
          break;

        case '9':
          vol = vol + 9;
          break;


        case 'Z':

          l = vol.length();
          l = l - 1;
          vol.remove(l);
          break;

        case 'E':
          voll = vol.toInt();
          vol.remove(0, (in.length()));
          Serial.print(voll);
          delay(5000);
          return;
          break;

        case '0':
          vol = vol + 0;
          break;

      }


  }
}

void defafunc()
{


  lcd.clear();
  lcd.print("Balance:" + balvol);
  lcd.setCursor(0, 1);
  lcd.print("Press Enter");
  lcd.setCursor(0, 2);
  lcd.print("To continue");

  for (int i = 0; i >= 0;)
  {
    char key = kpd.getKey();
    if (key)
      if (key == 'E')
        return;
  }

}













int passcheck()
{

  if (paas == pass)
    return 1;
  else
    return 0;
}



void keytillenter()
{
  lcd.clear();
  for (int i = 0; i >= 0;)
  {
    delay(100);
    lcd.clear();
    lcd.print("Enter your password");
    lcd.setCursor(0, 1);
    lcd.print(in);
    char key = kpd.getKey();
    if (key)
      switch (key)
      {
        case '1':
          in = in + 1;
          break;

        case '2':
          in = in + 2;
          break;

        case '3':
          in = in + 3;
          break;

        case '4':
          in = in + 4;
          break;

        case '5':
          in = in + 5;
          break;

        case '6':
          in = in + 6;
          break;

        case '7':
          in = in + 7;
          break;

        case '8':
          in = in + 8;
          break;

        case '9':
          in = in + 9;
          break;


        case 'Z':

          l = in.length();
          l = l - 1;
          in.remove(l);
          break;

        case 'E':
          paas = in.toInt();
          in.remove(0, (in.length()));
          return;
          break;

        case '0':
          in = in + 0;
          break;


      }


  }
}
  char key;
  boolean x;
  while ( (elapsed_time < max_time) && ( x ))

Local variables should ALWAYS be initialized.

Global ones, too, but at least the compiler will assign default values to all global variables. NOT so with local variables.

(Parentheses) (are) (good,) (but) (they) (can) (be) (overused.)

for (int i = 0; i >= 0;)
{

}

Usually a ‘for’ loop is used to iterate a given number of times, rather than as an infinite loop.
e.g.

for (int i = 0; i < 10; i++) // Loops 10 times
{

}

It does work, but a while loop is more common:-

while(true)
{

}