Keypad 3 Servo Locking Mechanism Code Confusion

Hello, I have been really bugged by this, I've researched the Keypad 3 Servo Locking Mechanism many times, and I have been trying to find the error that comes with this coding. I've ran it through TinkerCAD's Circuit system with a correct setup (already have made sure that it is correct), and for some reason the code does not want to work with the setup. I've been trying to solve it for 5 hours, and it's been tedious to find it. I'll post the code below and the system that I have been working with in TinkerCAD if anybody is interested in the wiring. Also, a plus to the system itself is how I can wire the system differently.

[TinkerCAD Circuit setup is under attachments]

Keypad 3 Servo Locking Mechanism Code:

#include <Servo.h>
#include <Keypad.h>

Servo servo1; //The servo is called 'servo1' from now on
Servo servo2; //The servo is called 'servo2' from now on
Servo servo3; //The servo is called 'servo3' from now on

char* password1 = "123"; //We set the password. In this case 'AC3'
char* password2 = "456"; //We set the password. In this case '888'
char* password3 = "888"; //We set the password. In this case 'AB3'

int i = 0;
int j = 0;
int k = 0;

const byte ROWS = 4; //In this two lines we define how many rows and columns
const byte COLS = 4; //our keypad has

char keys[ROWS][COLS] = { //The characters on the keys are defined here
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'#', '0', '*', 'D'},
};

byte rowPins[ROWS] = {13, 12, 7, 6}; //The connection with the arduino is
byte colPins[COLS] = {5, 4, 3, 2}; //listed here
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

int greenLED = 8; //The green LED is connected to pin 8

void setup(){
pinMode(greenLED, OUTPUT);
servo1.attach(9); //The servo is connected to pin 11
servo2.attach(10); //The servo is connected to pin 11
servo3.attach(11); //The servo is connected to pin 11
setLocked1(true);
setLocked2(true);
setLocked3(true);
Serial.begin(9600);
delay(2000);
}

void loop(){
char key = keypad.getKey();
if(key != NO_KEY){
Serial.write(key);
if (key == '' || key == '#'){//If the lock is open it can be locked again by pushing '' or '#'
i = 0;
j = 0;
k = 0;
setLocked1(true); //The command to close the lock after „“ or „#“ is pushed
setLocked2(true); //The command to close the lock after „
“ or „#“ is pushed
setLocked3(true); //The command to close the lock after „“ or „#“ is pushed
}
if (key == password1
){*

  • i++;*
  • }*
  • if (i == 3){ //This part defines how many characters our code will have. In this case we have 3 characters.*
  • setLocked1(false);*
  • }*
  • if (key == password2[j]){*
  • j++;*
  • }*
  • if (j == 3){ //This part defines how many characters our code will have. In this case we have 3 characters.*
  • setLocked2(false);*
  • }*
  • if (key == password3[k]){*
  • k++;*
  • }*
  • if (k == 3){ //This part defines how many characters our code will have. In this case we have 3 characters.*
  • setLocked3(false);*
  • }*
  • delay(100);*
  • }*
    }
    void setLocked1(int locked1)
    {
  • if(locked1) // If the lock is closed..*
  • {*
  • digitalWrite(greenLED, LOW); //green LED off*
  • servo1.write(0); //and the servo should turn to a 0 degree position.*
  • }*
  • else //if the lock is open..*
  • {*
  • digitalWrite(greenLED, HIGH);//green LED blinks once*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • servo1.write(90); //..and the servo should turn to a 90 degree position.*
  • }*
    }
    void setLocked2(int locked2)
    {
  • if(locked2) // If the lock is closed..*
  • {*
  • digitalWrite(greenLED, LOW); //green LED off*
  • servo2.write(0); //and the servo should turn to a 0 degree position.*
  • }*
  • else //if the lock is open..*
  • {*
  • digitalWrite(greenLED, HIGH);//green LED blinks twice*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • digitalWrite(greenLED, HIGH);*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • servo2.write(90); //..and the servo should turn to a 90 degree position.*
  • } *
    }
    void setLocked3(int locked3)
    {
  • if(locked3) // If the lock is closed..*
  • {*
  • digitalWrite(greenLED, LOW); //green LED off*
  • servo3.write(0); //and the servo should turn to a 0 degree position.*
  • }*
  • else //if the lock is open..*
  • {*
  • digitalWrite(greenLED, HIGH);//green LED blinks thrice*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • digitalWrite(greenLED, HIGH);*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • digitalWrite(greenLED, HIGH);*
  • delay(500);*
  • digitalWrite(greenLED, LOW);*
  • delay(250);*
  • servo3.write(90); //..and the servo should turn to a 90 degree position.*
  • }*
    }

you declare:char* password1 = "123";
you used: char key = keypad.getKey();
and then: if (key == password1)
you compared char* with char
=> it does not work.

Read multiple input characters in to a string and then compare with the password. See How to use Keypad
for Password Input

I meant to edit out the asterisks that are on the 'char* password1 = "123"' function. I edited them out, and got the follow error messages:

8:18: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
9:18: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
10:18: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
In function 'void loop()':
56:27: error: invalid types 'char[int]' for array subscript
62:27: error: invalid types 'char[int]' for array subscript
68:27: error: invalid types 'char[int]' for array subscript

Use "String" class as above mentioned link

I am new to arduino, as I do not even understand how this works. My teacher gave me this for an assignment for class, and I seriously do not understand what you are saying by "String" class. I don't understand what the String class would be used for.

The char* is not the problem. The problem is you're comparing a single keypad entry to a string of three characters. You need to collect three sequential, valid, entries from the keypad in a null-terminated c-string. Then you can compare the two strings (arrays) using strcmp() to determine a match.

Use Robin2’s serial input basics as a guide to getting your keypad input in a usable form

When you have more than two variables that are the same except for a trailing number it is time to put them in an array.

You have to accumulate the received characters in a buffer so you compare the whole string of characters to the possible passwords. The way the sketch is written you would just have to press all of the buttons three times to unlock all three: 0123456789ABCD0123456789ABCD0123456789ABCD would be the universal password.

Here is an example of using arrays and putting keypad input into a buffer. Note: if you want to turn all three servos you have to enter the three passwords correctly in any order. If you get a character wrong you have to press the "#" or "*" button to lock all three and start over.

#include <Servo.h>
#include <Keypad.h>


// We have the same number of servos, servo pins, and passwords.
const byte ChannelCount = 3;


Servo Servos[ChannelCount]; //The servo is called 'servo1' from now on
const byte ServoPins[ChannelCount] = {9, 10, 11};
const char *Passwords[ChannelCount] = {"123", "456", "888"};


const byte GreenLEDPin = 8; //The green LED is connected to pin 8


// Keypad
const byte ROWS = 4; //In this two lines we define how many rows and columns
const byte COLS = 4; //our keypad has


char keys[ROWS][COLS] =   //The characters on the keys are defined here
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'#', '0', '*', 'D'},
};


byte rowPins[ROWS] = {13, 12, 7, 6}; //The connection with the arduino is
byte colPins[COLS] = {5, 4, 3, 2}; //listed here
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


const byte MAX_INPUT_LENGTH = 10;
char InputBuffer[MAX_INPUT_LENGTH + 1] = "";


void setup()
{
  Serial.begin(9600);
  while (!Serial);


  pinMode(GreenLEDPin, OUTPUT);
  digitalWrite(GreenLEDPin, LOW);


  for (int channel = 0; channel < ChannelCount; channel++)
  {
    Servos[channel].attach(ServoPins[channel]); //The servo is connected to pin 11
    Lock(channel);
  }
}


void loop()
{
  char key = keypad.getKey();
  if (key == NO_KEY)
    return; // No input so nothing to do


  Serial.write(key);


  if (key == '*' || key == '#') //If the lock is open it can be locked again by pushing '*' or '#'
  {
    InputBuffer[0] = '\0'; // Make the buffer empty
    for (int channel = 0; channel < ChannelCount; channel++)
    {
      Lock(channel); //The command to close the lock after „*" or „#" is pushed
    }
  }
  else
  {
    // Some other character
    int len = strlen(InputBuffer);
    if (len < MAX_INPUT_LENGTH)
    {
      InputBuffer[len++] = key;
      InputBuffer[len] = '\0'; //Add terminator
    }


    for (int channel = 0; channel < ChannelCount; channel++)
    {
      if (strcmp(InputBuffer, Passwords[channel]) == 0)  // Check for equal
      {
        Unlock(channel);
        InputBuffer[0] = '\0'; // Empty the input buffer to prepare for next password
        // NOTE: You can't have two passwords the same.
      }
    }
  }
}


void Lock(int channel)
{
  digitalWrite(GreenLEDPin, LOW); //green LED off
  Servos[channel].write(0); //and the servo should turn to a 0 degree position.
}


void Unlock(int channel)
{
  digitalWrite(GreenLEDPin, HIGH);//green LED blinks thrice
  delay(500);
  digitalWrite(GreenLEDPin, LOW);
  delay(250);
  digitalWrite(GreenLEDPin, HIGH);
  delay(500);
  digitalWrite(GreenLEDPin, LOW);
  delay(250);
  digitalWrite(GreenLEDPin, HIGH);
  delay(500);
  digitalWrite(GreenLEDPin, LOW);
  delay(250);


  Servos[channel].write(90); //..and the servo should turn to a 90 degree position.
}