system
October 18, 2014, 1:26pm
1
Hi.
I am new at coding and could use some help
I use Arduino mega 2560 and 4x3 keypad to get one relay on and off,I have got it work, but I want to do this:
when i Press 1 once for turning relay on, then turn it off by pressing 1 again.
How can i do that?
Can someone add that in the sketch for me.
#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] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, A6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin1 = 53; // Relay Pin 53 on Arduino Mega 2560
boolean ledPin_state;
void setup(){
Serial.begin(9600);
pinMode(ledPin1, OUTPUT); // Sets the digital pin as output.
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
if (key == '1')
{
digitalWrite(ledPin1,HIGH);
}
if (key == '2')
{
digitalWrite(ledPin1,LOW);
}
}
}
system
October 18, 2014, 1:49pm
2
when i Press 1 once for turning relay on, then turn it off by pressing 1 again.
So, what's the problem? Can you recognize when '1' is pressed? Can you remember whether the relay is on or off? If '1' is pressed and the relay is on, turn it off. If '1' is pressed and the relay is off, turn it on.
system
October 18, 2014, 2:03pm
3
Right now ,i use two button from 4x3 keypad (key 1 and 2) (one button to get relay ON and one button to get relay OFF.
The problem is,i want only use one button (Key 1) to get relay ON/OFF.
system
October 18, 2014, 2:39pm
4
The problem is,i want only use one button (Key 1) to get relay ON/OFF.
No, that isn't the problem. That is what you want to do.
What is the problem? Why don't you just do what you want to do?
Why is the relay connected to a pin called ledPin1? Are you planning to connect an LED to relayPin1?
Where are you attempting to remember the state of the pin?
#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] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, A6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin1 = 53; // Relay Pin 53 on Arduino Mega 2560
byte ledPin2 = 54; // Relay Pin 54 on Arduino Mega 2560 for instance
boolean ledPin_state;
boolean state1 = false;
boolean state2 = false;
void setup(){
Serial.begin(9600);
pinMode(ledPin1, OUTPUT); // Sets the digital pin as output.
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
if (key == '1') {
state1 = !state1;
if (state1) {
digitalWrite(ledPin1,HIGH);
} else {
digitalWrite(ledPin1,LOW);
}
}
if (key == '2') {
state2 = !state2;
if (state2) {
digitalWrite(ledPin2,HIGH);
} else {
digitalWrite(ledPin2,LOW);
}
}
}
system
October 18, 2014, 4:17pm
6
Thank you surbyte Is working
#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] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, A6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte Relay14 = 48; // Relay Pin 48 on Arduino Mega 2560
byte Relay15 = 46; // Relay Pin 46 on Arduino Mega 2560
boolean ledPin_state;
boolean state1 = false;
boolean state2 = false;
void setup(){
Serial.begin(9600);
pinMode(Relay14, OUTPUT); // Sets the digital pin as output.
pinMode(Relay15, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
if (key == '1') {
state1 = !state1;
if (state1) {
digitalWrite(Relay14,HIGH);
} else {
digitalWrite(Relay14,LOW);
}
}
if (key == '2') {
state2 = !state2;
if (state2) {
digitalWrite(Relay15,HIGH);
} else {
digitalWrite(Relay15,LOW);
}
}
}
}