#include <Adafruit_MCP23X17.h>
const int pushButton[] = {2, 3, 4, 5, 6, 7}; // define push button inputs
const int relayPin[] = {13, 12, 11, 10, 9, 8}; // output pins where 4 relays will be connected
String relayNames[] = {"CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7"}; // Just put name for 4 relays
int pushed[] = {0, 0, 0, 0, 0, 0}; // status of each buttons
int relayStatus[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; // initial status of relay
String id;
void setup() {
// Robojax.com 4-Relay-4-Push button 20181211
Serial.begin(9600);// initialize serial monitor
for (int i = 0; i < 6; i++)
{
pinMode(pushButton[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], LOW);// initial relay status to be OFF
}
// Robojax.com 4-Relay-4-Push button 20181211
}
void loop() {
// Robojax.com 4-Relay-4-Push button 20181211
for (int i = 0; i < 6; i++)
{
int val = digitalRead(pushButton[i]);
delay(1);
if (val == LOW && relayStatus[i] == HIGH) {
Serial.println("aaaaaaaaaaaaaaaaa");
pushed[i] = 1 - pushed[i];
delay(1);
if (pushed[i] == HIGH) {
id = relayNames[i] + "ON";
Serial.print(relayNames[i]);
Serial.println(" ON");
digitalWrite(relayPin[i], HIGH);
} else {
Serial.print(relayNames[i]);
Serial.println(" OFF");
digitalWrite(relayPin[i], LOW);
id = relayNames[i] + "OFF";
}// else
}// if
relayStatus[i] = val;
if (id == "CH1ON") {
Serial.println("1_on");
} else if (id == "CH1OFF") {
Serial.println("1_off");
}
}// for
// Serial.println("==");
//delay(1);
// Robojax.com 4-Relay-4-Push button 20181211
}// loop end