I've got a program for a password protected doorlock, which I got working.
I'm trying to add to the code, to add functionality where I can toggle an LED on/off with a button.
Both codes function on their own and serve their desired purpose, but when I try to combine them into the same sketch, the LED button works but the rest of my design stops working. I can no longer input a password to activate the motor.
How can I combine the two codes and have them both work?
Here is the working keypad/lock code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define Password_Length 5
int signalPin = 10;
int greenLedPin = 11;
int redLedPin = 12;
char Data[Password_Length] = "4";
char Master[Password_Length] = "1234";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 3;
bool ledState = 0;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x3f, 16, 2);
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.clear();
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Enter Password:");
customKey = customKeypad.getKey();
if (customKey){
Data[data_count] = customKey;
lcd.setCursor(data_count,1);
lcd.print(Data[data_count]);
data_count++;
}
if(data_count == Password_Length-1){
lcd.clear();
if(!strcmp(Data, Master)){
lcd.print("Correct");
pinMode(signalPin, OUTPUT);
digitalWrite(signalPin, LOW); //turns on motor
digitalWrite(greenLedPin, HIGH); //turns on led
delay(2000);
digitalWrite(signalPin, HIGH); //turns off motor
digitalWrite(greenLedPin, LOW); //turns off led
}
else{
lcd.print("Incorrect");
digitalWrite(redLedPin, HIGH); //turns on led
delay(2000);
digitalWrite(redLedPin, LOW); //turns off led
}
lcd.clear();
clearData();
}
}
void clearData(){
while(data_count !=0){
Data[data_count--] = 0;
}
return;
}
and here is the code I'm using for the LED on/off toggle button
int button = 2;
int led = 13;
int status = false;
void setup(){
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor,
unpressed button is HIGH
}
void loop()
{
//a) if the button is not pressed the false status is reversed by !status
and the LED turns on
//b) if the button is pressed the true status is reversed by !status and
the LED turns off
if (digitalRead(button) == true) {
status = !status;
digitalWrite(led, status);
} while(digitalRead(button) == true);
delay(50); // keeps a small delay
}
true
orfalse
, it returnsHIGH
orLOW
. Coincidentally they are the same numbers internally, but it is better practice to check for what the function is documented to return. See: arduino.cc/reference/en/language/functions/digital-io/…