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.*
- }*
}
