Why does the code not take key input

#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
//servo library
Servo servo;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

int trigPin = 1;    
int echoPin = 0;   
int servoPin = 13;
int led = 10;
int x;
String y;
int number = 6528;
long duration, dist, average;
long aver[3];   //array for average
String bando = "";
String strinput = "";
boolean run = true;
char input[4];

const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 6, 5, 4, 3 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { A0, A1, A2, A3 }; // Pins connected to R1, R2, R3, R4

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);



void setup() {
  lcd.begin(16, 2);
  // you can now interact with the LCD, e.g.:
  Serial.begin(9600);
  servo.attach(servoPin);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.write(0);         //close cap on power on
  delay(100);
  servo.detach();
}

void print(String text) {
  lcd.setCursor(0,0);
  lcd.print(text);
  for (int scrollCounter = 0; scrollCounter < text.length(); scrollCounter++)
  {
    lcd.scrollDisplayLeft();
    delay(200);   
  }
  lcd.clear();
}

void pront(String text1, String text2){
  lcd.home();
  lcd.print(text1);
  lcd.setCursor(0,1);
  lcd.print(text2);
}

void measure() {  
  digitalWrite(10,HIGH);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  dist = (duration/2) / 29.1;     //obtain distance
}

void getDistance(){
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
    aver[i]=dist;            
    delay(10);              //delay between measurements
  }
  dist=(aver[0]+aver[1]+aver[2])/3;
}

void loop() {
  lcd.print("Press 0 to start");
  delay(10);
  char start = keypad.getKey();
  lcd.clear();
  if (start == '0'){
    lcd.clear();
    print(" Welcome to the Maths Lab");
    print(" This is a ATM machine");
    /*
    while (run){
      for (int i=0;i<=2;i++) {   //average distance
        measure();               
        aver[i]=dist;            
        delay(10);              //delay between measurements
      
        }
      dist=(aver[0]+aver[1]+aver[2])/3;    
      if (dist < 5){
        run = false;
      }
    }
    lcd.clear();
    */
    lcd.print("Enter the pin");
    for(int i = 0; i < 5; i++){
      delay(10);
      input[i] = keypad.getKey();
      strinput = strinput + input[i];
    }
    lcd.clear();
    if (strinput == "6528"){
      print("Well Done. You found the number");
      print("Here's your prize");
      servo.attach(servoPin);
      delay(1);
      servo.write(0);  
      delay(700);       
      servo.write(110);    
      delay(700);
      servo.detach();
    }
    }

  return 0;
}
 for(int i = 0; i < 5; i++){
      delay(10);
      input[i] = keypad.getKey();
      strinput = strinput + input[i];
    }

How long has the user got to type in the 4 digit pin ?

As much time as he likes

How long will that for loop take to run ?

By the way, you are writing to element 4 of the array and that does not exist

how do i make the program wait until the user enters a number

    keypad.getKey()

will always return immediately.

You have to look for a return value of NO_KEY, which means that the user did not press a key since the last time.

Stash only valid keys, not NO_KEYs. Count until you have stashed 5 valid characters, or however many you are looking for, or until a special key meaning "enter", perhaps, has been seen.

There is no need to delay(10). Also, a while loop might make better reading… while you haven't received five keypad characters, keep calling getKey.

HTH

a7

1 Like

You want: input[i] = keypad.waitForKey();

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.