Keypad and servo not working

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the custom library
#include <Servo.h>

// Keypad configuration
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad

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

// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display

// Servo configuration
Servo myServo;

String inputCode = "";
const String pass1 = "4444";
const String pass2 = "5555";
const String pass3 = "1729";

void setup() {
  lcd.begin(); // Initialize the LCD with 16 columns and 2 rows
  lcd.backlight();
  myServo.attach(10); // Attach servo to pin 10
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
  myServo.write(0);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    if (key == '#') {
      checkPassword();
      inputCode = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    } else if (key == '*') {
      inputCode = "";
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter Password:");
    } else {
      inputCode += key;
      lcd.setCursor(0, 1);
      lcd.print(inputCode);
    }
  }
}

void checkPassword() {
  if (inputCode == pass1 || inputCode == pass2 || inputCode == pass3) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Access Granted");
    myServo.write(90); // Rotate servo to 90 degrees
    delay(3000); // Keep servo in position for 3 seconds
    myServo.write(0); // Rotate servo back to 0 degrees
    lcd.clear();
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Access Denied");
    delay(2000); // Display Access Denied for 2 seconds
    lcd.clear();
  }
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
}

Hi,
The servo motor is just pulsing and not moving and the 789C row of the 4x4 keypad does not work
We tried changing the servo and the keypad.
Thank you

Welcome to the forum

Which Arduino board are you using and which pins of the Arduino do you have the LCD connected to ?

Try the test sketch you used to familiarize yourself with servos and the test sketch you used to familiarize yourself with keypads.

If you don't have two sketches (three actually, the LCD clu,d use one, too) like that, write them.

Test your wiring and your hardware.

Divide and conquer.

How are you providing power to the servo?

a7

Post an annotated schematic showing exactly how you have this wired, be sure to include all power sources.

in loop(), the default always appends to inputCode. But what if the keypad wasn't pressed? don't you need to check for NO_KEY and only process a return value when it is not NO_KEY?

Check your Keypad wiring as per Fig-1:


Figure-1:

I think this is what does that

  char key = keypad.getKey();

  if (key) {  // is there a key

Besides, if that wasn't doing it, the string would rapidly become the size of the known universe.

a7

thanks, i missed that

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