#include <EEPROM.h>
#define ON 0
#define OFF 1
const int relay1 =2;
const int relay2 =3;
const int relay3 =4;
const int relay4 =5;
const int relay5 =6;
const int relay6 =7;
const int relay7 =8;
const int relay8 =9;
const int relay9 =10;
const int relay10 =11;
const int relay11 =12;
const int relay12 =13;
const int relay13 =A0;
const int relay14 =A1;
const int relay15 =A2;
const int relay16 =A3;
byte State1 =1;
byte State2 =1;
byte State3 =1;
byte State4 =1;
byte State5 =1;
byte State6 =1;
byte State7 =1;
byte State8 =1;
byte State9 =1;
byte State10 =1;
byte State11 =1;
byte State12 =1;
byte State13 =1;
byte State14 =1;
byte State15 =1;
byte State16 =1;
char input;
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);
ReadEEPROM();
}
void loop() {
// put your main code here, to run repeatedly:
bluetooth();
}
void bluetooth(){
if(Serial.available()>0)
{
input = Serial.read();
if (input =='A'){
digitalWrite(relay1, ON);
EEPROM.write(1,0);
Serial.println("Relay 1 ON by Bluetooth");}
if (input =='a'){
digitalWrite(relay1, OFF);
EEPROM.write(1,1);
Serial.println("Relay 1 OFF by Bluetooth");}
if (input =='B'){
digitalWrite(relay2, ON);
EEPROM.write(2,0);
Serial.println("Relay 2 ON by Bluetooth");}
if (input =='b'){
digitalWrite(relay2, OFF);
EEPROM.write(2,1);
Serial.println("Relay 2 OFF by Bluetooth");}
if (input =='C'){
digitalWrite(relay3, ON);
EEPROM.write(3,0);
Serial.println("Relay 3 ON by Bluetooth");}
if (input =='c'){
digitalWrite(relay3, OFF);
EEPROM.write(3,1);
Serial.println("Relay 3 OFF by Bluetooth");}
if (input =='D'){
digitalWrite(relay4, ON);
EEPROM.write(4,0);
Serial.println("Relay 4 ON by Bluetooth");}
if (input =='d'){
digitalWrite(relay4, OFF);
EEPROM.write(4,1);
Serial.println("Relay 4 OFF by Bluetooth");}
if (input =='E'){
digitalWrite(relay5, ON);
EEPROM.write(5,0);
Serial.println("Relay 5 ON by Bluetooth");}
if (input =='e'){
digitalWrite(relay5, OFF);
EEPROM.write(5,1);
Serial.println("Relay 5 OFF by Bluetooth");}
if (input =='F'){
digitalWrite(relay6, ON);
EEPROM.write(6,0);
Serial.println("Relay 6 ON by Bluetooth");}
if (input =='f'){
digitalWrite(relay6, OFF);
EEPROM.write(6,1);
Serial.println("Relay 6 OFF by Bluetooth");}
if (input =='G'){
digitalWrite(relay7, ON);
EEPROM.write(7,0);
Serial.println("Relay 7 ON by Bluetooth");}
if (input =='g'){
digitalWrite(relay7, OFF);
EEPROM.write(7,1);
Serial.println("Relay 7 OFF by Bluetooth");}
if (input =='H'){
digitalWrite(relay8, ON);
EEPROM.write(8,0);
Serial.println("Relay 8 ON by Bluetooth");}
if (input =='h'){
digitalWrite(relay8, OFF);
EEPROM.write(8,1);
Serial.println("Relay 8 OFF by Bluetooth");}
if (input =='I'){ //mulai edit disini
digitalWrite(relay9, ON);
EEPROM.write(9,0);
Serial.println("Relay 9 ON by Bluetooth");}
if (input =='i'){
digitalWrite(relay9, OFF);
EEPROM.write(9,1);
Serial.println("Relay 9 OFF by Bluetooth");}
if (input =='J'){
digitalWrite(relay10, ON);
EEPROM.write(10,0);
Serial.println("Relay 10 ON by Bluetooth");}
if (input =='j'){
digitalWrite(relay10, OFF);
EEPROM.write(10,1);
Serial.println("Relay 10 OFF by Bluetooth");}
if (input =='K'){
digitalWrite(relay11, ON);
EEPROM.write(11,0);
Serial.println("Relay 11 ON by Bluetooth");}
if (input =='k'){
digitalWrite(relay11, OFF);
EEPROM.write(11,1);
Serial.println("Relay 11 OFF by Bluetooth");}
if (input =='L'){
digitalWrite(relay12, ON);
EEPROM.write(12,0);
Serial.println("Relay 12 ON by Bluetooth");}
if (input =='l'){
digitalWrite(relay12, OFF);
EEPROM.write(12,1);
Serial.println("Relay 12 OFF by Bluetooth");}
if (input =='M'){
digitalWrite(relay13, ON);
EEPROM.write(13,0);
Serial.println("Relay 13 ON by Bluetooth");}
if (input =='m'){
digitalWrite(relay13, OFF);
EEPROM.write(13,1);
Serial.println("Relay 13 OFF by Bluetooth");}
if (input =='N'){
digitalWrite(relay14, ON);
EEPROM.write(14,0);
Serial.println("Relay 14 ON by Bluetooth");}
if (input =='n'){
digitalWrite(relay14, OFF);
EEPROM.write(14,1);
Serial.println("Relay 14 OFF by Bluetooth");}
if (input =='O'){
digitalWrite(relay15, ON);
EEPROM.write(15,0);
Serial.println("Relay 15 ON by Bluetooth");}
if (input =='o'){
digitalWrite(relay15, OFF);
EEPROM.write(15,1);
Serial.println("Relay 15 OFF by Bluetooth");}
if (input =='P'){
digitalWrite(relay16, ON);
EEPROM.write(16,0);
Serial.println("Relay16 ON by Bluetooth");}
if (input =='p'){
digitalWrite(relay16, OFF);
EEPROM.write(16,1);
Serial.println("Relay 16 OFF by Bluetooth");}
}
}
void ReadEEPROM(){
State1 = EEPROM.read(1);
State2 = EEPROM.read(2);
State3 = EEPROM.read(3);
State4 = EEPROM.read(4);
State5 = EEPROM.read(5);
State6 = EEPROM.read(6);
State7 = EEPROM.read(7);
State8 = EEPROM.read(8);
State9 = EEPROM.read(9);
State10 = EEPROM.read(10);
State11 = EEPROM.read(11);
State12 = EEPROM.read(12);
State13 = EEPROM.read(13);
State14 = EEPROM.read(14);
State15 = EEPROM.read(15);
State16 = EEPROM.read(16);
digitalWrite(relay1,State1);
Serial.print(F("Posisi Terakhir Relay1 ="));
if(State1==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay2,State2);
Serial.print(F("Posisi Terakhir Relay2 ="));
if(State2==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay3,State3);
Serial.print(F("Posisi Terakhir Relay3 ="));
if(State3==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay4,State4);
Serial.print(F("Posisi Terakhir Relay4 ="));
if(State4==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay5,State5);
Serial.print(F("Posisi Terakhir Relay5 ="));
if(State5==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay6,State6);
Serial.print(F("Posisi Terakhir Relay6 ="));
if(State6==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay7,State7);
Serial.print(F("Posisi Terakhir Relay7 ="));
if(State7==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay8,State8);
Serial.print(F("Posisi Terakhir Relay8 ="));
if(State8==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay9,State9); //mulai sini edit
Serial.print(F("Posisi Terakhir Relay9 ="));
if(State9==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay10,State10);
Serial.print(F("Posisi Terakhir Relay10 ="));
if(State10==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay11,State11);
Serial.print(F("Posisi Terakhir Relay11 ="));
if(State11==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay12,State12);
Serial.print(F("Posisi Terakhir Relay12 ="));
if(State12==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay13,State13);
Serial.print(F("Posisi Terakhir Relay13 ="));
if(State13==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay14,State14);
Serial.print(F("Posisi Terakhir Relay14 ="));
if(State14==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay15,State15);
Serial.print(F("Posisi Terakhir Relay15 ="));
if(State15==0){Serial.println("ON");}else{Serial.println("OFF");}
digitalWrite(relay16,State16);
Serial.print(F("Posisi Terakhir Relay16 ="));
if(State16==0){Serial.println("ON");}else{Serial.println("OFF");}
}
void relayON(uint8_t r){
digitalWrite(r+1,ON);
EEPROM.write(r,1);
Serial.print("Relay");
Serial.print(r);
Serial.println("ON by Bluetooth");
}
void relayOFF (uint8_t r){
digitalWrite(r+1,OFF);
EEPROM.write(r,0);
Serial.print("Relay ");
Serial.print(r);
Serial.println("OFF by Bluetooth");
}