1

I have a keypad that is connected to my Arduino and each time a key is pressed, the number should be outputted to an LCD screen. The issue is that after the screen is cleared, and a new ID is being entered, any number on the first row will not trigger a keypress event but only on its first click. If you press it twice, it works completely fine. I am using the Keypad library and here is my code. Thanks for any help!

#include <ESP8266WiFi.h>
#include <Keypad.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x3F // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

const char* ssid     = "**";
const char* password = "**";

const char* host = "**";
const char* getSystem = "**";
const char* apiKey = "**";
const char* systemIdentifier = "**";

const byte n_rows = 4;
const byte n_cols = 3;

char keys[n_rows][n_cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[n_rows] = {D2, D3, D4, D5};
byte colPins[n_cols] = {D6, D7, D8};

int n = 1;
String idNumber;

boolean checkingIn = false;

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  lcd.begin (16, 2); 


  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); 

  lcd.print("Connecting");

  while (WiFi.status() != WL_CONNECTED) {
    lcd.print(".");
    delay(500);
  }

  setLCD("Please Enter ID#", 0);
  setLCD("                ", 1);

  delay(500);
}

void loop() {
  if (!checkingIn) {
    char myKey = myKeypad.getKey();
    Serial.print(myKey);
    if (myKey != NULL && (String)myKey != "*" && (String)myKey != "#") {
      Serial.print("Key pressed: ");
      Serial.println(myKey);
      idNumber += (String)myKey;
      setLCD(idNumber, 1);
    } else if ((String)myKey == "*" && idNumber.length() > 0) {
      idNumber = idNumber.substring(0, idNumber.length() - 1);
      setLCD("                ", 1);
      setLCD(idNumber, 1);
    } else if ((String)myKey == "#") {
      setLCD("                ", 0);
      setLCD("                ", 1);
      setLCD(" Checking in... ", 0);
      checkIn();
    }
  }
}

void setLCD(String text, int line) {
  lcd.setBacklight(HIGH);
  lcd.setCursor(0, line);
  lcd.print(text);
}

void checkIn() {
  checkingIn = true;
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    setLCD("                ", 1);
    setLCD("    Failed!     ", 0);
    return;
  }

  // We now create a URI for the request
  String url = "/assets/api.php?SYSTEM=";
  url += getSystem;
  url += "&API_KEY=";
  url += apiKey;
  url += "&SYSTEM_IDENTIFIER=";
  url += systemIdentifier;
  url += "&ID=";
  url += idNumber;

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 1000) {
      setLCD("                ", 1);
      setLCD("    Failed!     ", 0);
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to //Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    if (line.indexOf("[response|failed]") > -1) {
      setLCD("Invalid Login...", 0);
      setLCD("                ", 1);
    } else if (line.indexOf("*") > -1) {
      setLCD("Welcome:        ", 0);
      setLCD("                ", 1);
      setLCD(line, 1);
    }
    //Serial.println(line);
  }
  idNumber = "";
  delay(750);
  checkingIn = false;
  setLCD("Please Enter ID#", 0);
  setLCD("                ", 1);
}
  • Why are you using D4, D5 and D6 for both the lcd and the keypad? That won't work, surely? – Nick Gammon Jan 15 '18 at 22:28
  • Which Arduino is it? – Nick Gammon Jan 15 '18 at 22:38
  • @NickGammon It's IC2, completely different protocol so yes, that works. Also, Wemos D1 WiFi – Carl Flood Jan 15 '18 at 23:03
  • So just to be clear, D4 and D4_pin are completely different pins? That's kind-of confusing isn't it? – Nick Gammon Jan 16 '18 at 4:02
  • 1
    I think a minimal, complete, verifiable example would be useful here. It'll be easier for people to understand so you'll attract more help, and very likely you'll figure it out yourself in the process of creating it. – Mark Smith Jan 16 '18 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.