#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// start of relay settings
const int relayPin[]={A4,A2,A1,A0};// output pins where 8 relays will be connected
String relayNames[] ={"CH1", "CH2","CH3","CH4"};// Just put name for 8 relays
// do not change lines bellow
int pushed[] ={0,0,0,0};// status of each buttons
int relayStatus[] ={LOW,LOW,LOW,LOW};// initial status of relay
// led part
#define led 10
void setup() {
Serial.begin(9600);
for(int i=0; i<8; i++)
{
pinMode(relayPin[i], OUTPUT); // set relay pins as output
digitalWrite(relayPin[i], LOW);// initial relay status to be OFF
}
pinMode(led,OUTPUT);
}
void loop() {
digitalWrite(led,HIGH);
controleKeypad();
}
void controleKeypad(){
int val;
int knum;
char key = keypad.getKey();
// just print the pressed key
if(key && key !='*' && key !='#' && key !='0' && key !='9' ){
knum = (int)key-49;// convert char to integer (one less)
if(knum>=0 && knum<8){
Serial.println(knum);
if(relayStatus[knum] == LOW){
pushed[knum] = 1-pushed[knum];
delay(50);
}// if
controlRelay(knum);// turn relay ON or OFF
}
}else{
val = LOW;
}
if(knum>=0 && knum<8){
relayStatus[knum] = val;
}
delay(50);
}// loop end
void controlRelay(int number)
{
if(pushed[number] == 1)
{
digitalWrite(relayPin[number], HIGH);// Turn ON relay
Serial.print(relayNames[number]);
Serial.println(" ON");
delay(2000);
digitalWrite(relayPin[number], LOW); // turn OFF
Serial.print(relayNames[number]);
Serial.println(" OFF");
}
}