hello everyone, i want to switch a relay (actually 4, but i cant even get one to work) by typing in a code on a keypad
-board: arduino uno
-relay board: HL-54 V1.0
-keypad: 3x4 matrix keypad
this is what i have ( code from arduinogetstarted that i modified):
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
const int RELAY_PIN = 2; // the Arduino pin, which connects to the IN pin of relay
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password = "1234"; // change your password here
String input_password;
void setup(){
Serial.begin(9600);
input_password.reserve(32); // maximum input characters is 33, change if needed
pinMode(RELAY_PIN, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
if(key == '*') {
input_password = ""; // clear input password
} else if(key == '#') {
if(password == input_password) {
Serial.println("password is correct");
digitalWrite(RELAY_PIN, HIGH);
delay(500);
digitalWrite(RELAY_PIN, LOW);
delay(500);// DO YOUR WORK HERE
} else {
Serial.println("password is incorrect, try again");
}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
}
the password works fine (it reacts as it should in the 'monitor', but the relay is being unpredictable (relay sometimes open and sometimes closed on reboot, switches relay on and off quickly after accepting code, in other cases relay stays closed or open indefinitely, arduino seems to power pin 1 when pressing buttons on keypad(?))
i tried adding delay, but no improvement ![]()
i hope i've posted this the right way, i tried figuring it out myself but wasted several hours to no avail. In no way do i expect someone to just write me the code, i hope someone can point out what i'm doing wrong. This is my first project btw, i'm trying to learn.
thanks in advance!