Activate 2 relays with keyboard
My problem is the following I want to control two relays with a matrix keyboard as follows when pressing the number 1 of the matrix keyboard activates the number 1 and when you press the number two of the matrix keyboard activates the number 2, working as Pushbutton only when they are pressed then when they stop pressing it they have to turn off I created a code but they are turning on the two relays together I do not know what to do here I attach the code to see if they can help me
/* @file EventSerialKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates using the KeypadEvent.
|| #
*/
#include <Keypad.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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 9;
byte ledPin1 =13;
boolean blink = false;
boolean ledPin_state;
boolean ledPin1_state;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, LOW); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
pinMode(ledPin1, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin1, LOW); // Turn the LED on.
ledPin_state = digitalRead(ledPin1); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}
void loop(){
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
if (blink){
digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(0);
}
if (blink){
digitalWrite(ledPin1,!digitalRead(ledPin1)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(0);
}
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == ‘#’) {
digitalWrite(ledPin,!digitalRead(ledPin));
ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
digitalWrite(ledPin1,!digitalRead(ledPin1));
ledPin1_state = digitalRead(ledPin1); // Remember LED state, lit or unlit.
}
break;
case RELEASED:
if (key == ‘*’) {
digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
blink = false;
}
if (key == ‘1’) {
digitalWrite(ledPin1,ledPin1_state); // Restore LED state from before it started blinking.
blink = false;
}
break;
case HOLD:
if (key == ‘*’) {
blink = true; // Blink the LED when holding the * key.
}
if (key == ‘1’) {
blink = true; // Blink the LED when holding the * key.
}
break;
}
}