Go Down

Topic: Error not getting solved near if statement (Read 53 times) previous topic - next topic

albert937

Code: [Select]


#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;
       

    }

 
}
}




albert937


OldSteve

#2
Today at 10:31 am Last Edit: Today at 10:35 am by OldSteve
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.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

albert937

Code: [Select]
#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]

albert937

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

OldSteve

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?:-
Code: [Select]
   for (int i = 0; i >= 0;)
    {
        char key = kpd.getKey();
        if (key)
            if (key == 'E')
                return;
    }


And in the above, this:-
Code: [Select]
if (key)
    if (key == 'E')
        return;
should look like this:-
Code: [Select]
if (key)
{
    if (key == 'E')
        return;
}


You need to rethink your method of doing this:-
Code: [Select]
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:-
Code: [Select]
lcd.print("Balance:" + balvol);
You could do this:-
Code: [Select]
lcd.print("Balance:");
lcd.print(balvol);


This will not work as you expect, either:-
Code: [Select]
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.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

UKHeliBob

Code: [Select]

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

Even if this compiled, how likely is it that key will be 'S' ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up