#include <SoftwareSerial.h>
#define TX_PIN 9
#define RX_PIN 8
SoftwareSerial bluetooth(RX_PIN, TX_PIN);
long BTBAUD=57600;
char lenhbat;
int relay1 = 10;
int giobat = 0;
int phutbat = 0;
int giaybat = 0;
char lenhtat;
int giotat = 0;
int phuttat = 0;
int giaytat = 0;
//==============end bluetooth============
#include <IRremote.h>
int RECV_PIN = 11; // the pin where you connect the output pin of sensor
byte led1 = 6;//OUT1
byte led2 = 5;//AUTO1
int pv=3;
// dau vao tin hieu tm pin mat troi
int CM = 4;//CHUYEN DIỆN LƯỚI
//int v; // biến tính toán chuyển mạch
int u;
int t=255;// trạng thái led1
int a=255;// trạng thái led2
//
void setup()
{
//=========khoi setup bluetooth==========
bluetooth.begin(BTBAUD);
//==========ket thuc koi tao=============
Serial.begin(9600);
//Serial.begin(19200);
// you can ommit this linesss
pinMode(led1, OUTPUT); // MAIN
pinMode(led2, OUTPUT); // AUTO
pinMode(7, OUTPUT); //TRUNG GIAN MỞ AUTO
pinMode(13,OUTPUT);
pinMode(CM,OUTPUT); // CHUYỂN MẠCH
pinMode(pv,INPUT); //
pinMode(A0,INPUT); // ĐẦU VÀO PV
pinMode(A1,INPUT);
pinMode(A2,INPUT); // ĐỌC ĐẦU VÀO MAIN
pinMode(A3,INPUT); // ĐỌC ĐẦU VÀO AUTO
digitalWrite(7,LOW);
digitalWrite(13,LOW);
digitalWrite(CM,LOW);
digitalWrite(2,LOW); // RESET
analogWrite(5,0);
analogWrite(6,0);
//==============================
(giobat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giobat);
bluetooth.print(" : ");
(phutbat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phutbat);
bluetooth.print(" : ");
(giaybat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaybat);
bluetooth.println();
(giotat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giotat);
bluetooth.print(" : ");
(phuttat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phuttat);
bluetooth.print(" : ");
(giaytat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaytat);
bluetooth.println();
}
void loop() {
HENGIO_BATDEN();
HENGIO_TATDEN();
}
void HENGIO_BATDEN(){
if (bluetooth.available()>0 ) {
lenhbat=bluetooth.read();
Serial.write(lenhbat);
//============ Hien thi cho che do cai dat thoi gian ====================
(giobat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giobat);
bluetooth.print(" : ");
(phutbat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phutbat);
bluetooth.print(" : ");
(giaybat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaybat);
bluetooth.println();
}
else lenhbat=0;
switch (lenhbat){
case 'a':
analogWrite(led1,t);
break;
case 'b':
analogWrite(led1,LOW);
break;
case '1':
giobat ++;
delay(600);
if( giobat ==24) {
giobat = 0;}
//=====================================
break;
case '2':
giobat -=1;
delay(600);
if ( giobat < 0) {
giobat = 24;}
//===============================
break;
case'3':
phutbat ++;
delay(600);
if( phutbat > 59) {
phutbat = 0;
}
//==================================
break;
case'4':
phutbat -=1;
delay(600);
if ( phutbat < 0) {
phutbat = 59;
}
//=====================================
break;
case'5':
giaybat ++;
delay(600);
if( giaybat > 59) {
giaybat = 0;
}
//=========================================
break;
case'6':
giaybat -=1;
delay(600);
if ( giaybat < 0) {
giaybat = 59;
}
break;
case'8':{ // lenh reset
delay(500);
giobat = 0;
phutbat = 0;
giaybat = 0;
}
break;
default:
break;
}}
//===============hẹn giờ tắt đèn==========
void HENGIO_TATDEN(){
if (bluetooth.available()>0 ) {
lenhtat=bluetooth.read();
Serial.write(lenhtat);
//============ Hien thi cho che do cai dat thoi gian ====================
(giotat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giotat);
bluetooth.print(" : ");
(phuttat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(phuttat);
bluetooth.print(" : ");
(giaytat < 10) ? bluetooth.print("0") : NULL;
bluetooth.print(giaytat);
bluetooth.println();
}
else lenhtat=0;
switch (lenhtat){
case 'x':
giotat ++;
delay(600);
if( giotat ==24) {
giotat = 0;}
//=====================================
break;
case 'y':
giotat -=1;
delay(600);
if ( giotat < 0) {
giotat = 24;}
//===============================
break;
case'z':
phuttat ++;
delay(600);
if( phuttat > 59) {
phuttat = 0;
}
//==================================
break;
case'p':
phuttat -=1;
delay(600);
if ( phuttat < 0) {
phuttat = 59;
}
//=====================================
break;
case'q':
giaytat ++;
delay(600);
if( giaytat > 59) {
giaytat = 0;
}
//=========================================
break;
case'r':
giaytat -=1;
delay(600);
if ( giaytat < 0) {
giaytat = 59;
}
break;
default:
break;
}}
//==============================================