Hi I recently completed my Servo keypad project and I wanted to add a function where if you push a button it will unlock the door from the inside as I can not manually turn it. I have tried to implement this into my code but it will not work. Here is the code and I will highlight the area of concern. Please also note that the code works by itself, just not with the keypad code included in it. I posted the code I need to implement into the main project below the main code, because it would not let me change the color. Thank you for any help!
#include <Keypad.h>
#include <Servo.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char inputArray[4]; //array to gather user keypad presses
char Main[4] = {'5','9','9','7'}; //array to hold keypad password
char Guest[4] = {'4','1','1','8'}; //array to allow guest to enter
char Lock[4] = {'1','1','1','1'}; //disables guest mode
char Unlock[4] = {'2','2','2','2'}; //enables guest mode
#define ledPin 13 //led to register keypad pressses
#define registerPin 12 //led that registers correct password entry
int i = 0;
int state = 1; //Creates the state
int inputPin = 10; // Push button input pin
int btnVal = 0; // Current value of pushbutton
Servo mrservo; // create servo object to control the servo
void setup()
{
Serial.begin(9600); //open serial port
pinMode(ledPin, OUTPUT); //define led pin as output
pinMode(registerPin, OUTPUT); //define led pin as output
mrservo.attach(9); //attaches servo to pin 9 on the servo object
pinMode(inputPin, INPUT); // Push Button
}
void loop()
{
btnVal = digitalRead(inputPin);
if(btnVal == HIGH) {
mrservo.write(95); //Move the servo to 95 degrees
delay(1000); //Wait 1 second
mrservo.write(0); //Move the servo to 0 degrees
delay(1000); //Wait 1 second
}
{
while (state = 1)
{
char key = kpd.getKey();
//if a key is pressed
if(key)
{
//turn on ledPin
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
inputArray[i] = key; //store entry into array
i++;
Serial.println(key); //print keypad character entry to serial port
if (key=='*')
{
Serial.println("Reset");
i=0; //reset i
}
if (i == 4) //if 4 presses have been made
{
{
//match input array to password array
if (inputArray[0] == Main[0] &&
inputArray[1] == Main[1] &&
inputArray[2] == Main[2] &&
inputArray[3] == Main[3])
{
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(2000);
digitalWrite(registerPin, LOW);
mrservo.write(75);
delay(15000);
mrservo.write(30);
delay(500);
}
}
{
//match input array to password array
if (inputArray[0] == Guest[0] &&
inputArray[1] == Guest[1] &&
inputArray[2] == Guest[2] &&
inputArray[3] == Guest[3])
{
// moves servo after correct code entered.
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(2000);
digitalWrite(registerPin, LOW);
mrservo.write(75);
delay(15000);
mrservo.write(30);
delay(500);
}
}
{
//match input array to password array
if (inputArray[0] == Lock[0] &&
inputArray[1] == Lock[1] &&
inputArray[2] == Lock[2] &&
inputArray[3] == Lock[3])
{
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(2000);
digitalWrite(registerPin, LOW);
Serial.println("State 2");
state = 2;
break;
}
}
{
i=0; //reset i
}
}
}
};
}
{
while (state = 2)
{
char key = kpd.getKey();
//if a key is pressed
if(key)
{
//turn on ledPin
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
inputArray[i] = key; //store entry into array
i++;
Serial.println(key); //print keypad character entry to serial port
if (key=='*')
{
Serial.println("Reset");
i=0; //reset i
}
if (i == 4) //if 4 presses have been made
{
{
//match input array to password array
if (inputArray[0] == Main[0] &&
inputArray[1] == Main[1] &&
inputArray[2] == Main[2] &&
inputArray[3] == Main[3])
{
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(2000);
digitalWrite(registerPin, LOW);
mrservo.write(75);
delay(15000);
mrservo.write(30);
delay(500);
}
}
{
//match input array to password array
if (inputArray[0] == Unlock[0] &&
inputArray[1] == Unlock[1] &&
inputArray[2] == Unlock[2] &&
inputArray[3] == Unlock[3])
{
Serial.println("State 1");
state = 1;
break;
digitalWrite(registerPin, HIGH); //turn on registerPin led
delay(2000);
digitalWrite(registerPin, LOW);
}
}
{
i=0; //reset i
}
}
}
};
}
}
This is the code I need to implement, above I have tried but it will not work.
#include <Servo.h>
int inputPin = 10;
int btnVal = 0;
Servo mrservo;
void setup()
{
pinMode(inputPin, INPUT);
mrservo.attach(9);
}
void loop()
{
btnVal = digitalRead(inputPin);
if(btnVal == HIGH) {
mrservo.write(95); //Move the servo to 95 degrees
delay(1000); //Wait 1 second
mrservo.write(0); //Move the servo to 0 degrees
delay(1000); //Wait 1 second
}
}