1

Could someone help me with this code? I want to make an ATM machine for 5 users. But, I don't know what's wrong with the code.

#include <Keypad.h>


#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,A0,A1);
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char status[5],Pin1[] = {"54321"},Pin2[] = {"22711"},Pin3[] = {"20213"},Pin4[] = {"93810"},Pin5[] = {"67530"};
int step;
int i;
int x;
char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
String debit = "";
String credit = "";
byte pin_rows[ROW_NUM] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
char key = keypad.getKey();
int balance1 = 400;
int balance2 = 400;
int balance3 = 400;
int balance4 = 7000;
int balance5 = 10000;
void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("PLZ enter ur pin");
}
char statusatm = ' ';
void loop(){
  key = keypad.getKey();
  if(step == 0){
    if (key){
      if(i == 0){
        lcd.clear();
        lcd.setCursor(6,0);
      }
      while(key == HIGH);
      status[i]=key;
      lcd.print("*");
      i++;
    }
    if(i==5){
      i=0;
      lcd.clear();
      //strcmp gives 0 if the lists matches and 1 if it doesn't 
      if(!strcmp(Pin1,status)){
        x = 1;
         step++;
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
      else{
        if(!strcmp(Pin2,status)){
          x = 2;
          step++;
          lcd.setCursor(0,0);
      lcd.print("Click 1.Balance2");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");

        }
        else{
        if(!strcmp(Pin3,status)){
          x = 3;
          step++;
          lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");

        }
        else{
        if(!strcmp(Pin4,status)){
          x = 4;
          step++;
          lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");

        }
        else{
        if(!strcmp(Pin5,status)){
          x = 5;
          step++;
          lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");

        }
        else{
          lcd.setCursor(0,0);
          lcd.print("We can't find ");
          lcd.setCursor(0,1);
          lcd.print("this pin");

              }
            }
          }
        }
      }
    }
  }
  if( x == 1){
    
  key = keypad.getKey();
  if( step == 1){
    if (key){
      if(key == 'C' || key == 'A'){
        lcd.clear();
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
        if(key == '1'){
            lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("ur current Bal.");
          lcd.setCursor(0,1);
          lcd.print(balance1);
        }
      else if(key == '2'){
            lcd.clear();
        statusatm = 'd';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur deb. rup");
            step++;
        lcd.setCursor(0,1);
      }
      else if(key == '3'){
            lcd.clear();
        statusatm = 'h';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur crd. rup");
            step++;
        lcd.setCursor(0,1);
      }
    }
  }
  if(statusatm == 'd'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(debit.toInt());
            balance1 = balance1 - debit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }
          else{
            debit += key;
            Serial.println(debit);
            lcd.print(key);
          }
        }
    }
  if(statusatm == 'h'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(credit.toInt());
            balance1 = balance1 + credit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else{
            credit += key;
            Serial.println(credit);
            lcd.print(key);
          }
        }
    }
  }
}
else{
  if( x == 2){
    
  key = keypad.getKey();
  if( step == 1){
    if (key){
      if(key == 'C' || key == 'A'){
        lcd.clear();
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
        if(key == '1'){
            lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("ur current Bal.");
          lcd.setCursor(0,1);
          lcd.print(balance2);
        }
      else if(key == '2'){
            lcd.clear();
        statusatm = 'd';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur deb. rup");
            step++;
        lcd.setCursor(0,1);
      }
      else if(key == '3'){
            lcd.clear();
        statusatm = 'h';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur crd. rup");
            step++;
        lcd.setCursor(0,1);
      }
    }
  }
  if(statusatm == 'd'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(debit.toInt());
            balance2 = balance2 - debit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }
          else{
            debit += key;
            Serial.println(debit);
            lcd.print(key);
          }
        }
    }
  if(statusatm == 'h'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(credit.toInt());
            balance2 = balance2 + credit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else{
            credit += key;
            Serial.println(credit);
            lcd.print(key);
          }
        }
    }
  }
}
else{
  if( x == 3){
    
  key = keypad.getKey();
  if( step == 1){
    if (key){
      if(key == 'C' || key == 'A'){
        lcd.clear();
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
        if(key == '1'){
            lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("ur current Bal.");
          lcd.setCursor(0,1);
          lcd.print(balance3);
        }
      else if(key == '2'){
            lcd.clear();
        statusatm = 'd';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur deb. rup");
            step++;
        lcd.setCursor(0,1);
      }
      else if(key == '3'){
            lcd.clear();
        statusatm = 'h';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur crd. rup");
            step++;
        lcd.setCursor(0,1);
      }
    }
  }
  if(statusatm == 'd'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(debit.toInt());
            balance3 = balance3 - debit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }
          else{
            debit += key;
            Serial.println(debit);
            lcd.print(key);
          }
        }
    }
  if(statusatm == 'h'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(credit.toInt());
            balance3 = balance3 + credit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else{
            credit += key;
            Serial.println(credit);
            lcd.print(key);
          }
        }
    }
  }
}
else{
  if( x == 4){
   
  key = keypad.getKey();
  if( step == 1){
    if (key){
      if(key == 'C' || key == 'A'){
        lcd.clear();
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
        if(key == '1'){
            lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("ur current Bal.");
          lcd.setCursor(0,1);
          lcd.print(balance4);
        }
      else if(key == '2'){
            lcd.clear();
        statusatm = 'd';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur deb. rup");
            step++;
        lcd.setCursor(0,1);
      }
      else if(key == '3'){
            lcd.clear();
        statusatm = 'h';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur crd. rup");
            step++;
        lcd.setCursor(0,1);
      }
    }
  }
  if(statusatm == 'd'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(debit.toInt());
            balance4 = balance4 - debit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }
          else{
            debit += key;
            Serial.println(debit);
            lcd.print(key);
          }
        }
    }
  if(statusatm == 'h'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(credit.toInt());
            balance4 = balance4 + credit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else{
            credit += key;
            Serial.println(credit);
            lcd.print(key);
          }
        }
    }
  }
}
else{
  if( x == 5){
    
  key = keypad.getKey();
  if( step == 1){
    if (key){
      if(key == 'C' || key == 'A'){
        lcd.clear();
        lcd.setCursor(0,0);
      lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
      }
        if(key == '1'){
            lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("ur current Bal.");
          lcd.setCursor(0,1);
          lcd.print(balance5);
        }
      else if(key == '2'){
            lcd.clear();
        statusatm = 'd';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur deb. rup");
            step++;
        lcd.setCursor(0,1);
      }
      else if(key == '3'){
            lcd.clear();
        statusatm = 'h';
          lcd.setCursor(0,0);
          lcd.print("PLZ rit ur crd. rup");
            step++;
        lcd.setCursor(0,1);
      }
    }
  }
  if(statusatm == 'd'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(debit.toInt());
            balance5 = balance5 - debit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }
          else{
            debit += key;
            Serial.println(debit);
            lcd.print(key);
          }
        }
    }
  if(statusatm == 'h'){
    key = keypad.getKey();
        if(key){
          if(key == 'C'){
            Serial.println(credit.toInt());
            balance5 = balance5 + credit.toInt();
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else if(key == 'A'){
            lcd.clear();
              lcd.print("Click  1.Balance");
    lcd.setCursor(0,1);
    lcd.print("2.Debit 3.Credit");
            step =1;
            statusatm = ' ';
          }else{
            credit += key;
            Serial.println(credit);
            lcd.print(key);
          }
        }
    }
  }
}
5
  • 6
    You need to explain, what problem you have with the code. What exactly did you expect and what is the code actually doing? – chrisl Jun 9 at 20:26
  • 3
    your code formatting is awful ... please fix the formatting so that the code is easier to read – jsotola Jun 9 at 20:59
  • 1
    Yeah, part of the key to getting to where you want is probably cleaning up what you already have. Splitting it into more functions would be a good step also. – timemage Jun 9 at 21:12
  • 1
  • 1
    The number of users is orthogonal to the problem(s). You may want to look into simple state machines to control overall behavior, and recognize that the behavior is the same regardless of which user is using the machine—only the account is different (or whatever you’re using as as an abstraction for a “user”. – Dave Newton Jun 10 at 11:56

Browse other questions tagged or ask your own question.