#include <EEPROM.h>
#include <Wire.h>
#define ON 1
#define OFF 0
const int relay1 =13; //A
const int relay2 =12; //B
const int relay3 =11; //C
const int relay4 =10; //D
const int relay5 =9; //E
const int relay6 =8; //F
const int relay7 =7; //G
const int relay8 =6; // H
const int relay9 =5; //I
const int relay10 =4; //J
const int relay11 =3; //K
const int relay12 =2; //L
const int relay13 =A3; //M
const int relay14 =A2; //N
const int relay15 =A1; //O
const int relay16 =A0; //P
char input;
int mode = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(F("Control 16 Channel Relay Via Bluetooth"));
pinMode(relay1,OUTPUT);digitalWrite (relay1, OFF);
pinMode(relay2,OUTPUT);digitalWrite (relay2, OFF);
pinMode(relay3,OUTPUT);digitalWrite (relay3, OFF);
pinMode(relay4,OUTPUT);digitalWrite (relay4, OFF);
pinMode(relay5,OUTPUT);digitalWrite (relay5, OFF);
pinMode(relay6,OUTPUT);digitalWrite (relay6, OFF);
pinMode(relay7,OUTPUT);digitalWrite (relay7, OFF);
pinMode(relay8,OUTPUT);digitalWrite (relay8, OFF);
pinMode(relay9,OUTPUT);digitalWrite (relay9, OFF);
pinMode(relay10,OUTPUT);digitalWrite (relay10, OFF);
pinMode(relay11,OUTPUT);digitalWrite (relay11, OFF);
pinMode(relay12,OUTPUT);digitalWrite (relay12, OFF);
pinMode(relay13,OUTPUT);digitalWrite (relay13, OFF);
pinMode(relay14,OUTPUT);digitalWrite (relay14, OFF);
pinMode(relay15,OUTPUT);digitalWrite (relay15, OFF);
pinMode(relay16,OUTPUT);digitalWrite (relay16, OFF);
Wire.begin();
//ReadEEPROM();
}
void loop() {
// put your main code here, to run repeatedly:
if (mode == 1){mode1();}
if (mode == 2){mode2();}
if (mode == 3){mode3();}
}
void mode1(){
if(Serial.available()>0)
{
input = Serial.read();
if (input =='A'){
digitalWrite(relay1, ON);
EEPROM.write(1,0);
Serial.println("Relay 1 ON di mode 1");}
if (input =='a'){
digitalWrite(relay1, OFF);
EEPROM.write(1,1);
Serial.println("Relay 1 OFF di mode 1");}
if (input =='B'){
digitalWrite(relay2, ON);
EEPROM.write(2,0);
Serial.println("Relay 2 ON di mode 1");}
if (input =='b'){
digitalWrite(relay2, OFF);
EEPROM.write(2,1);
Serial.println("Relay 2 OFF di mode 1");}
if (input =='C'){
digitalWrite(relay3, ON);
EEPROM.write(3,0);
Serial.println("Relay 3 ON di mode 1");}
if (input =='c'){
digitalWrite(relay3, OFF);
EEPROM.write(3,1);
Serial.println("Relay 3 OFF di mode 1");}
if (input =='D'){
digitalWrite(relay4, ON);
EEPROM.write(4,0);
Serial.println("Relay 4 ON di mode 1");}
if (input =='d'){
digitalWrite(relay4, OFF);
EEPROM.write(4,1);
Serial.println("Relay 4 OFF di mode 1");}
if (input =='E'){
digitalWrite(relay5, ON);
EEPROM.write(5,0);
Serial.println("Relay 5 ON di mode 1");}
if (input =='e'){
digitalWrite(relay5, OFF);
EEPROM.write(5,1);
Serial.println("Relay 5 OFF di mode 1");}
if (input =='F'){
digitalWrite(relay6, ON);
EEPROM.write(6,0);
Serial.println("Relay 6 ON di mode 1");}
if (input =='f'){
digitalWrite(relay6, OFF);
EEPROM.write(6,1);
Serial.println("Relay 6 OFF di mode 1");}
if (input =='G'){
digitalWrite(relay7, ON);
EEPROM.write(7,0);
Serial.println("Relay 7 ON di mode 1");}
if (input =='g'){
digitalWrite(relay7, OFF);
EEPROM.write(7,1);
Serial.println("Relay 7 OFF di mode 1");}
if (input =='H'){
digitalWrite(relay8, ON);
EEPROM.write(8,0);
Serial.println("Relay 8 ON di mode 1");}
if (input =='h'){
digitalWrite(relay8, OFF);
EEPROM.write(8,1);
Serial.println("Relay 8 OFF di mode 1");}
if (input =='I'){ //mulai edit disini
digitalWrite(relay9, ON);
EEPROM.write(9,0);
Serial.println("Relay 9 ON di mode 1");}
if (input =='i'){
digitalWrite(relay9, OFF);
EEPROM.write(9,1);
Serial.println("Relay 9 OFF di mode 1");}
if (input =='J'){
digitalWrite(relay10, ON);
EEPROM.write(10,0);
Serial.println("Relay 10 ON di mode 1");}
if (input =='j'){
digitalWrite(relay10, OFF);
EEPROM.write(10,1);
Serial.println("Relay 10 OFF di mode 1");}
if (input =='K'){
digitalWrite(relay11, ON);
EEPROM.write(11,0);
Serial.println("Relay 11 ON di mode 1");}
if (input =='k'){
digitalWrite(relay11, OFF);
EEPROM.write(11,1);
Serial.println("Relay 11 OFF di mode 1");}
if (input =='L'){
digitalWrite(relay12, ON);
EEPROM.write(12,0);
Serial.println("Relay 12 ON di mode 1");}
if (input =='l'){
digitalWrite(relay12, OFF);
EEPROM.write(12,1);
Serial.println("Relay 12 OFF di mode 1");}
if (input =='M'){
digitalWrite(relay13, ON);
EEPROM.write(13,0);
Serial.println("Relay 13 ON di mode 1");}
if (input =='m'){
digitalWrite(relay13, OFF);
EEPROM.write(13,1);
Serial.println("Relay 13 OFF di mode 1");}
if (input =='N'){
digitalWrite(relay14, ON);
EEPROM.write(14,0);
Serial.println("Relay 14 ON di mode 1");}
if (input =='n'){
digitalWrite(relay14, OFF);
EEPROM.write(14,1);
Serial.println("Relay 14 OFF di mode 1");}
if (input =='O'){
digitalWrite(relay15, ON);
EEPROM.write(15,0);
Serial.println("Relay 15 ON di mode 1");}
if (input =='o'){
digitalWrite(relay15, OFF);
EEPROM.write(15,1);
Serial.println("Relay 15 OFF di mode 1");}
if (input =='P'){
digitalWrite(relay16, ON);
EEPROM.write(16,0);
Serial.println("Relay16 ON di mode 1");}
if (input =='p'){
digitalWrite(relay16, OFF);
EEPROM.write(16,1);
Serial.println("Relay 16 OFF di mode 1");}
if (input =='Q'){
mode = 1;
Serial.println("pindah ke mode 1");}
if (input =='R'){
mode = 2;
Serial.println("pindah ke mode 2");}
if (input =='S'){
mode = 3;
Serial.println("pindah ke mode 3");}
}
}
void mode2(){
if(Serial.available()>0)
{
input = Serial.read();
if (input =='A'){
Wire.beginTransmission(8);
Wire.write("A");
Wire.endTransmission();
Serial.println("Mode 2 Relay 1 ON");}
if (input =='a'){
Wire.beginTransmission(8);
Wire.write("a");
Wire.endTransmission();
Serial.println("Mode 2 Relay 1 OFF");}
if (input =='B'){
Wire.beginTransmission(8);
Wire.write("B");
Wire.endTransmission();
Serial.println("Mode 2 Relay 2 ON");}
if (input =='b'){
Wire.beginTransmission(8);
Wire.write("b");
Wire.endTransmission();
Serial.println("Mode 2 Relay 2 OFF");}
if (input =='C'){
Wire.beginTransmission(8);
Wire.write("C");
Wire.endTransmission();
Serial.println("Mode 2 Relay 3 ON");}
if (input =='c'){
Wire.beginTransmission(8);
Wire.write("c");
Wire.endTransmission();
Serial.println("Mode 2 Relay 3 OFF");}
if (input =='D'){
Wire.beginTransmission(8);
Wire.write("D");
Wire.endTransmission();
Serial.println("Mode 2 Relay 4 ON");}
if (input =='d'){
Wire.beginTransmission(8);
Wire.write("d");
Wire.endTransmission();
Serial.println("Mode 2 Relay 4 OFF");}
if (input =='E'){
Wire.beginTransmission(8);
Wire.write("E");
Wire.endTransmission();
Serial.println("Mode 2 Relay 5 ON");}
if (input =='e'){
Wire.beginTransmission(8);
Wire.write("e");
Wire.endTransmission();
Serial.println("Mode 2 Relay 5 OFF");}
if (input =='F'){
Wire.beginTransmission(8);
Wire.write("F");
Wire.endTransmission();
Serial.println("Mode 2 Relay 6 ON");}
if (input =='f'){
Wire.beginTransmission(8);
Wire.write("f");
Wire.endTransmission();
Serial.println("Mode 2 Relay 6 OFF");}
if (input =='G'){
Wire.beginTransmission(8);
Wire.write("G");
Wire.endTransmission();
Serial.println("Mode 2 Relay 7 ON");}
if (input =='g'){
Wire.beginTransmission(8);
Wire.write("g");
Wire.endTransmission();
Serial.println("Mode 2 Relay 7 OFF");}
if (input =='H'){
Wire.beginTransmission(8);
Wire.write("H");
Wire.endTransmission();
Serial.println("Mode 2 Relay 8 ON");}
if (input =='h'){
Wire.beginTransmission(8);
Wire.write("h");
Wire.endTransmission();
Serial.println("Mode 2 Relay 8 OFF");}
if (input =='I'){ //mulai edit disini
Wire.beginTransmission(8);
Wire.write("I");
Wire.endTransmission();
Serial.println("Mode 2 Relay 9 ON");}
if (input =='i'){
Wire.beginTransmission(8);
Wire.write("i");
Wire.endTransmission();
Serial.println("Mode 2 Relay 9 OFF");}
if (input =='J'){
Wire.beginTransmission(8);
Wire.write("J");
Wire.endTransmission();
Serial.println("Mode 2 Relay 10 ON");}
if (input =='j'){
Wire.beginTransmission(8);
Wire.write("j");
Wire.endTransmission();
Serial.println("Mode 2 Relay 10 OFF");}
if (input =='K'){
Wire.beginTransmission(8);
Wire.write("K");
Wire.endTransmission();
Serial.println("Mode 2 Relay 11 ON");}
if (input =='k'){
Wire.beginTransmission(8);
Wire.write("k");
Wire.endTransmission();
Serial.println("Mode 2 Relay 11 OFF");}
if (input =='L'){
Wire.beginTransmission(8);
Wire.write("L");
Wire.endTransmission();
Serial.println("Mode 2 Relay 12 ON");}
if (input =='l'){
Wire.beginTransmission(8);
Wire.write("l");
Wire.endTransmission();
Serial.println("Mode 2 Relay 12 OFF");}
if (input =='M'){
Wire.beginTransmission(8);
Wire.write("M");
Wire.endTransmission();
Serial.println("Mode 2 Relay 13 ON");}
if (input =='m'){
Wire.beginTransmission(8);
Wire.write("m");
Wire.endTransmission();
Serial.println("Mode 2 Relay 13 OFF");}
if (input =='N'){
Wire.beginTransmission(8);
Wire.write("N");
Wire.endTransmission();
Serial.println("Mode 2 Relay 14 ON");}
if (input =='n'){
Wire.beginTransmission(8);
Wire.write("n");
Wire.endTransmission();
Serial.println("Mode 2 Relay 14 OFF");}
if (input =='O'){
Wire.beginTransmission(8);
Wire.write("O");
Wire.endTransmission();
Serial.println("Mode 2 Relay 15 ON");}
if (input =='o'){
Wire.beginTransmission(8);
Wire.write("o");
Wire.endTransmission();
Serial.println("Mode 2 Relay 15 OFF");}
if (input =='P'){
Wire.beginTransmission(8);
Wire.write("P");
Wire.endTransmission();
Serial.println("Mode 2 Relay 16 ON");}
if (input =='p'){
Wire.beginTransmission(8);
Wire.write("p");
Wire.endTransmission();
Serial.println("Mode 2 Relay 16 OFF");}
if (input =='Q'){
mode = 1;
Serial.println("pindah ke mode 1");}
if (input =='R'){
mode = 2;
Serial.println("pindah ke mode 2");}
if (input =='S'){
mode = 3;
Serial.println("pindah ke mode 3");}
}
}
void mode3(){
if(Serial.available()>0)
{
input = Serial.read();
if (input =='A'){
Wire.beginTransmission(9);
Wire.write("A");
Wire.endTransmission();
Serial.println("Mode 3 Relay 1 ON");}
if (input =='a'){
Wire.beginTransmission(9);
Wire.write("a");
Wire.endTransmission();
Serial.println("Mode 3 Relay 1 OFF");}
if (input =='B'){
Wire.beginTransmission(9);
Wire.write("B");
Wire.endTransmission();
Serial.println("Mode 3 Relay 2 ON");}
if (input =='b'){
Wire.beginTransmission(9);
Wire.write("b");
Wire.endTransmission();
Serial.println("Mode 3 Relay 2 OFF");}
if (input =='C'){
Wire.beginTransmission(9);
Wire.write("C");
Wire.endTransmission();
Serial.println("Mode 3 Relay 3 ON");}
if (input =='c'){
Wire.beginTransmission(9);
Wire.write("c");
Wire.endTransmission();
Serial.println("Mode 3 Relay 3 OFF");}
if (input =='D'){
Wire.beginTransmission(9);
Wire.write("D");
Wire.endTransmission();
Serial.println("Mode 3 Relay 4 ON");}
if (input =='d'){
Wire.beginTransmission(9);
Wire.write("d");
Wire.endTransmission();
Serial.println("Mode 3 Relay 4 OFF");}
if (input =='E'){
Wire.beginTransmission(9);
Wire.write("E");
Wire.endTransmission();
Serial.println("Mode 3 Relay 5 ON");}
if (input =='e'){
Wire.beginTransmission(9);
Wire.write("e");
Wire.endTransmission();
Serial.println("Mode 3 Relay 5 OFF");}
if (input =='F'){
Wire.beginTransmission(9);
Wire.write("F");
Wire.endTransmission();
Serial.println("Mode 3 Relay 6 ON");}
if (input =='f'){
Wire.beginTransmission(9);
Wire.write("f");
Wire.endTransmission();
Serial.println("Mode 3 Relay 6 OFF");}
if (input =='G'){
Wire.beginTransmission(9);
Wire.write("G");
Wire.endTransmission();
Serial.println("Mode 3 Relay 7 ON");}
if (input =='g'){
Wire.beginTransmission(9);
Wire.write("g");
Wire.endTransmission();
Serial.println("Mode 3 Relay 7 OFF");}
if (input =='H'){
Wire.beginTransmission(9);
Wire.write("H");
Wire.endTransmission();
Serial.println("Mode 3 Relay 8 ON");}
if (input =='h'){
Wire.beginTransmission(9);
Wire.write("h");
Wire.endTransmission();
Serial.println("Mode 3 Relay 8 OFF");}
if (input =='I'){ //mulai edit disini
Wire.beginTransmission(9);
Wire.write("I");
Wire.endTransmission();
Serial.println("Mode 3 Relay 9 ON");}
if (input =='i'){
Wire.beginTransmission(9);
Wire.write("i");
Wire.endTransmission();
Serial.println("Mode 3 Relay 9 OFF");}
if (input =='J'){
Wire.beginTransmission(9);
Wire.write("J");
Wire.endTransmission();
Serial.println("Mode 3 Relay 10 ON");}
if (input =='j'){
Wire.beginTransmission(9);
Wire.write("j");
Wire.endTransmission();
Serial.println("Mode 3 Relay 10 OFF");}
if (input =='K'){
Wire.beginTransmission(9);
Wire.write("K");
Wire.endTransmission();
Serial.println("Mode 3 Relay 11 ON");}
if (input =='k'){
Wire.beginTransmission(9);
Wire.write("k");
Wire.endTransmission();
Serial.println("Mode 3 Relay 11 OFF");}
if (input =='L'){
Wire.beginTransmission(9);
Wire.write("L");
Wire.endTransmission();
Serial.println("Mode 3 Relay 12 ON");}
if (input =='l'){
Wire.beginTransmission(9);
Wire.write("l");
Wire.endTransmission();
Serial.println("Mode 3 Relay 12 OFF");}
if (input =='M'){
Wire.beginTransmission(9);
Wire.write("M");
Wire.endTransmission();
Serial.println("Mode 3 Relay 13 ON");}
if (input =='m'){
Wire.beginTransmission(9);
Wire.write("m");
Wire.endTransmission();
Serial.println("Mode 3 Relay 13 OFF");}
if (input =='N'){
Wire.beginTransmission(9);
Wire.write("N");
Wire.endTransmission();
Serial.println("Mode 3 Relay 14 ON");}
if (input =='n'){
Wire.beginTransmission(9);
Wire.write("n");
Wire.endTransmission();
Serial.println("Mode 3 Relay 14 OFF");}
if (input =='O'){
Wire.beginTransmission(9);
Wire.write("O");
Wire.endTransmission();
Serial.println("Mode 3 Relay 15 ON");}
if (input =='o'){
Wire.beginTransmission(9);
Wire.write("o");
Wire.endTransmission();
Serial.println("Mode 3 Relay 15 OFF");}
if (input =='P'){
Wire.beginTransmission(9);
Wire.write("P");
Wire.endTransmission();
Serial.println("Mode 3 Relay 16 ON");}
if (input =='p'){
Wire.beginTransmission(9);
Wire.write("p");
Wire.endTransmission();
Serial.println("Mode 3 Relay 16 OFF");}
if (input =='Q'){
mode = 1;
Serial.println("pindah ke mode 1");}
if (input =='R'){
mode = 2;
Serial.println("pindah ke mode 2");}
if (input =='S'){
mode = 3;
Serial.println("ke mode 3");}
}
}