Activate 2 relays with keyboard

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;
}
}

Hi
Welcome to the forum.

Please re-read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

I think your request is;

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

Your code seems to be trying to do a lot more than that.

I suggest you write your code in stages.

  • Write code just to read your keyboard, using Serial.print to monitor.
  • Write code just to operate 1 relay, using an input HIGH/LOW to operate it.
  • Write code just to operate 2 relays, using an 2 inputs HIGH/LOW to operate them.
  • Then combine them.

It sounds a laborious process but it will enable you to debug problems sooo much quicker.

What relays are you using, post a link to spec/data please.
What model Arduino are you using?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please do not post a Fritzy picture, we need a schematic, not a component layout.

Thanks.. Tom.. :slight_smile: :slight_smile: :slight_smile: :slight_smile: