1 Nhờ các bạn giúp đỡ Wed Dec 11, 2013 5:17 am
mình đang làm bài tập lớn về thiết kế hệ thống báo động cho 1 ngôi nhà mình có tham khảo được 1 đoạn code nhưng đọc chưa hiểu lắm mong bạn nào giỏi về C giúp đỡ mình với xin chân thành cảm ơn các bạn
đây là đoạn code của mình,các bạn hiểu chỉ mình với nhé!
đây là đoạn code của mình,các bạn hiểu chỉ mình với nhé!
- Code:
#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Metro.h>
#include <OneWire.h>
/////////////////////////////////
//0x28, 0xB9, 0x91, 0x20, 0x05, 0x00, 0x00, 0xB0
//0x28, 0x52, 0x2E, 0x21, 0x05, 0x00, 0x00, 0xCE
LiquidCrystal lcd(36, 32, 28, 26, 24, 22);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'F','0','E','D'}
};
byte rowPins[ROWS] = { 46, 48, 50, 52 };
byte colPins[COLS] = { 38, 40, 42, 44 };
Metro namgiay = Metro(5000);
Metro timer1 = Metro(100);
Metro timer0 = Metro(4000);
Metro motgiay = Metro(1000);
Metro timerbaove = Metro(10000);
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
///////////////////////////////
OneWire ds(A0);
//OneWire ds(A0);
int flagauto,acess,nummenu,flagconfirm;
int flagex,flagmo,flagti,flagnu,flagte,flagpa,flagchangePass,flags,flags1,flagcmd;
int k=0,j=0,p=0,i=0,cmd1,cmd2,cmd3,cmd4,flagq=0;
int devON[10],devOFF[10],dao=1;
int csim=53;
int buzz =11;
int sensorPin[9] = {A0,A1,A2,A3,A4,A5,A6,A7,A8};
int Valueset[9] = {300,300,300,300,300,300,300,300,300};
int Value[9];
int Alarm[9];
String Defiin[9]={"Cam bien chay","Chuyen dong WC","Khi gas","Mua","Phong khach","Chuyen dong cua sau","Kinh vo","Chuyen truoc nha","cam bienDongcua"};
int temp=1024;
int outPin[9] = {0,2,3,4,5,6,7,8,9};
String Defiout[9]={"","Den Hien" ,"Quat thong gio","Den Phong khach","Den WC","Phong Ngu","","Dong cua ..Mua","Mo cua truoc",};
int turntbi[9];
int trangthai;
int tempset,flag5s;
int bdong;
int H,M,S,trH,trM,trS;
String pass="1234";
String tempchar="",temppass="";
String SMS="";
String numphone="905653008";
String temp2="";
char key;
float tempindoor,tempoutdoor;
int flagsensor;
byte present = 0;
byte data[12];
byte addr[8];
char mode='2';
int flaghien,flaggas;
int flagcuasau,flagphongkhach,flagkinh,flagchay ,flagmocua,flagdongcua;
int modeenable;
int flagpaentry=0;
///////////////////////////////
void setup()
{ for(i=0;i<8;i++){
pinMode(outPin[i],OUTPUT);
digitalWrite(outPin[i], LOW);
}
pinMode(buzz,OUTPUT);
pinMode(34, OUTPUT);
pinMode(53, OUTPUT);
digitalWrite(34, LOW);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("SMART HOME");
Serial.begin(9600);
Serial1.begin(9600);
Serial.print("\r\n--- SMART SOLUTION ---\r\n");
Serial.println("Intiting Sim900..");
lcd.setCursor(0, 1);
loop1:
lcd.print("Int Sim..");
digitalWrite(csim, HIGH);
delay(2000);
digitalWrite(csim, LOW);
lcd.setCursor(0, 1);
lcd.print("Please wait..");
while(SMS.indexOf("Call Ready",0)==-1)
{
SMS="";
while (Serial1.available()>0){
char inChar = Serial1.read();
SMS += inChar;
flags1=1;
Serial.write(inChar);
delay(10);
}if(SMS.indexOf("NORMAL POWER DOWN",0)>-1){
lcd.setCursor(0, 1);
lcd.print("Restart SIM..");goto loop1;}
} Serial.print("--- LET BEGIN ---\r\n");
lcd.setCursor(0, 1);
lcd.print("Let begin");
digitalWrite(buzz, HIGH);
delay(100);
digitalWrite(buzz, LOW);
delay(100);
digitalWrite(buzz, HIGH);
delay(100);
digitalWrite(buzz, LOW);
delay(100);
for(i=0;i<4;i++) pass[i]= EEPROM.read(i);
for(i=10;i<19;i++) numphone[i-10]= EEPROM.read(i);
tempchar="12";
for(i=20;i<22;i++){
tempchar[i-20]=EEPROM.read(i);
lcd.setCursor(i-10, 1);
lcd.print(tempchar[i-20]) ;
}tempset=tempchar.toInt();
tempoutdoor=0;
tempindoor=0;
H=19;
}
void loop()
{
/////////////////////////// QUET SENSOR VA XU LY
Value[0]=tempindoor;
Value[1] = analogRead(sensorPin[1] );//cam bien chuyen WC
Value[2] = analogRead(sensorPin[2] );//khi ga
Value[3] = analogRead(sensorPin[3] );//mua
Value[4] = analogRead(sensorPin[4] );//khoang cach
Value[5] = analogRead(sensorPin[5] );//chuyen dong cua sau
Value[6] = analogRead(sensorPin[6] );//kinh võ
Value[7] = analogRead(sensorPin[7] );//chuyen dong truoc
Value[8]= analogRead(sensorPin[8] );//cam bien dong cua
//for(i=0;i<9;i++)
// Serial.println(Value[i]+Defiin[i]);
//delay(100);
if (Value[3]<100) digitalWrite(outPin[7], HIGH);else digitalWrite(outPin[7], LOW);//troi mua
if (Value[7]<100){flaghien=1;digitalWrite(outPin[1], HIGH);}else {digitalWrite(outPin[1], LOW);flaghien=0;}//den hien
if (Value[1]>500)digitalWrite(outPin[4], HIGH);else digitalWrite(outPin[4], LOW);//wC
if (Value[2]>500){flaggas=1;digitalWrite(outPin[1], HIGH);digitalWrite(outPin[2], HIGH);}else{ digitalWrite(outPin[2], LOW);flaggas=0;} //khi gas
//if(Value[0]>tempset)digitalWrite(outPin[5], HIGH);else digitalWrite(outPin[5], LOW);
if(tempoutdoor>60){trangthai=1;flagchay=1; digitalWrite(outPin[1], HIGH);digitalWrite(outPin[3], HIGH);}
if(!flagmocua)if(Value[8]>500){digitalWrite(outPin[8], HIGH);flagmocua=1;}
if(Value[8]>500&&flagmocua){flagdongcua=1;}
if(Value[8]<500&&flagdongcua&&!flaghien){if(!flag5s){flag5s=1;namgiay.interval(5000);}}
if(namgiay.check()&&flag5s){digitalWrite(outPin[8], LOW);flagmocua=0;flagdongcua=0;flag5s=0;}
///////////////////////////
//sau che do an toan if (Value[8]>"cam bien chuyen donng") bao dong; den sang nhan tin
//or cua tu dong mo khong co tin hieu dieu khien -bao dong; den sang; nhan tin
//khi gas co bao dong.. bat quat thong gio ,nhan tin ..
//nhiet do cao hon 50 bao dong nhan tin, bat quat thong gio.
//nhan paas=? cua mo va se tu dogn dong sau ns
if(modeenable==1){
if(Value[5]<100){ trangthai=1;flagcuasau=1;bdong=5;}
if(Value[4]>550){ trangthai=1;flagphongkhach=1;bdong=4;digitalWrite(outPin[3], HIGH);}
if(Value[6]<670){ trangthai=1;flagkinh=1;bdong=6;}
}
if(H>=18&&H<=22){
if(tempoutdoor>28&&tempoutdoor<50)digitalWrite(outPin[2], HIGH);//else digitalWrite(outPin[2], LOW);
digitalWrite(outPin[3], HIGH);
}else if(mode!='1'){digitalWrite(outPin[3], LOW);}//
if(flagpaentry==0){if(H<22&&H>=17||H>=5&&H<8){mode='2';modeenable=0;}else {mode='1';}}
/////////////////////////////////////////
if(flagsensor==0){
flagsensor==1;
ds.search(addr);
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
}
if(motgiay.check()){
flagsensor=0;
if (tempoutdoor>60) trangthai=0;
if (tempoutdoor<60) flagchay=0;
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
if( OneWire::crc8( data, 8)==data[8]){
if(addr[1]==0xB9) tempindoor = float(data[1]<<8|data[0])/16 ;
if(addr[1]==0x52) tempoutdoor= float(data[1]<<8|data[0])/16 ;
}
if(S++ >58){S=0;
if(M++ >58){M=0;
if(H++ >22)H=0;
}
}
}
if(acess==0&&timer1.check()){
if(trangthai==0){
nummenu=0;
lcd.clear();
lcd.print(H);lcd.print(":");lcd.print(M);lcd.print(":");lcd.print(S);lcd.print(" Mode: ");lcd.print(mode);
lcd.setCursor(0, 1);
lcd.print(tempindoor); lcd.print((char)223);lcd.print("C /");
lcd.print(tempoutdoor);lcd.print((char)223);lcd.print("C");
if(timerbaove.check()&&mode=='1')modeenable=1;
}
else baodong();
timer1.interval(100);
http://lcd.print(float(data[1]<<8|data[0])/16);
}
if(timer0.check())acess=0;
//////////////////
sim();
menu();
}
//////////////////////////////////////////////
void inputpass(int n,int m){
int i;timer0.reset(); key =0;
while(key!='D'&&key!='E'){
key=kpd.getKey();
if(key=='E')tempchar="";
else if(key&&key!='D'){
tempchar+=key;
for(i=0;i<tempchar.length();i++){
lcd.setCursor(n+i, m);
lcd.print('*');
}
}
}
}
/////////////////////////////////////////
void inputchar(int n,int m){
int i;timer0.reset(); key = 0;
while(key!='D'&&key!='E'&&key!='F'){
key=kpd.getKey();
if(key=='E')tempchar="";
else if(key&&key!='D'){
tempchar+=key;
for(i=0;i<tempchar.length();i++){
lcd.setCursor(n+i, m);
lcd.print(tempchar[i]);
}
}
}
}
///////////////////////////
void mode2(){
if(flagmo==1){
if(key=='D'){
lcd.clear();
lcd.print("Mode Protect");
lcd.setCursor(0, 1);
lcd.print("1-ON,2-OFF,3-AUTO");
key=0;tempchar="1";
lcd.setCursor(13, 1);
while(key!='D'&&key!='E'&&key!='F')
{key = kpd.getKey();
if(key=='E')tempchar="";
else if(key&&key!='D'){mode=key;lcd.print(key);}
}
if(mode=='1') {timerbaove.interval(10000);}
flagpaentry=1;
if(mode=='3') {flagpaentry=0;lcd.clear();
lcd.print("Auto");delay(1000);}
htmenu();
}
}
}
//////////////////////////////////
void mete(){
if(flagte==1){
if(key=='D'){
lcd.clear();
lcd.print(tempindoor);lcd.print((char)223);lcd.print("C-Indoor");
lcd.setCursor(0, 1);
lcd.print(tempoutdoor);lcd.print((char)223);lcd.print("C-Outdoor");
key=0;tempchar="12";
while(key!='A'&&key!='B'&&key!='D'&&key!='E'&&key!='F'){key = kpd.getKey();}
if(key=='A'){
lcd.clear();lcd.print("Curent temp.set") ;
for(i=20;i<22;i++){
tempchar[i-20]= EEPROM.read(i);
lcd.setCursor(i-10, 1);
lcd.print(tempchar[i-20]) ;
}tempset=tempchar.toInt();
}tempchar="";
while(key!='B'&&key!='D'&&key!='E'&&key!='F'){key = kpd.getKey();}
if(key=='B'){
lcd.clear();lcd.print("Enter Temp: ") ;
inputchar(0,1);
for(i=20;i<22;i++)
EEPROM.write(i,tempchar[i-20]);
tempset=tempchar.toInt();
}htmenu();
}
}
}
void menum(){
if(flagnu==1){
if(key=='D'){
lcd.clear();
lcd.print("Curent->UP") ;
lcd.setCursor(0, 1);
lcd.print("Edit ->DOWN") ;
key=0;
while(key!='A'&&key!='B'&&key!='D'&&key!='E'&&key!='F'){key = kpd.getKey();}
if(key=='A'){
tempchar="";
lcd.clear();lcd.print("Curent Num") ;
for(i=10;i<19;i++){
numphone[i-10]= EEPROM.read(i);
lcd.setCursor(i-10, 1);
lcd.print(numphone[i-10]) ;
}}
while(key!='B'&&key!='D'&&key!='E'&&key!='F'){key = kpd.getKey();}
if(key=='B'){
tempchar="";
lcd.clear();lcd.print("Enter Num") ;
inputchar(0,1);
numphone=tempchar;
for(i=10;i<19;i++)
EEPROM.write(i,numphone[i-10]);
}htmenu();
}
}
}
void metime(){
if(flagti==1){
if(key=='D'){
lcd.clear();
lcd.print("Curent time:");lcd.print(H);lcd.print(":");lcd.print(M);lcd.print(":");lcd.print(S);
lcd.setCursor(0, 1);
lcd.print("Entry timer ");
key=0;
while(key!='A'&&key!='B'&&key!='D'&&key!='E'&&key!='F'){key = kpd.getKey();}
if(key=='A'){
tempchar="";
lcd.clear();lcd.print("Set system time:") ;
lcd.setCursor(0,1);
lcd.print("HH:MM:SS->");
/////////////
int i; key = 0;
while(key!='D'&&key!='E'&&key!='F'){
key=kpd.getKey();
if(key=='E')tempchar="";
else if(key&&key!='D'){
tempchar+=key;
if(tempchar.length()==2)tempchar+=':';
if(tempchar.length()==5)tempchar+=':';
lcd.setCursor(0, 7);
lcd.print(tempchar);
http://lcd.print(H);lcd.print(":");lcd.print(M);lcd.print(":");lcd.print(S);
}
}if(key=='D'){
H=tempchar.substring(0,2).toInt();
M=tempchar.substring(3,5).toInt();
S=tempchar.substring(6,8).toInt();
lcd.clear();
lcd.print(tempchar);
}
}
if(key=='B'){
tempchar="";
lcd.clear();lcd.print("Entry Timer: ") ;
//////////// in9timer(0,1);
int i; key = 0;
while(key!='D'&&key!='E'&&key!='F'){
key=kpd.getKey();
if(key=='E')tempchar="";
else if(key&&key!='D'){
tempchar+=key;
if(tempchar.length()==2)tempchar+=':';
if(tempchar.length()==5)tempchar+=':';
lcd.setCursor(0, 7);
lcd.print(tempchar);
http://lcd.print(H);lcd.print(":");lcd.print(M);lcd.print(":");lcd.print(S);
}}
if(key=='D'){
trH=tempchar.substring(0,2).toInt();
trM=tempchar.substring(3,5).toInt();
trS=tempchar.substring(6,8).toInt();
lcd.clear();
lcd.print(tempchar);
}
}
}
}
}
//////////////////////////////////////
void menu(){
key = kpd.getKey();
if(key){
timer0.reset();
timer1.interval(10000);timer0.interval(4000);
if(flagex){while(key!='F')key = kpd.getKey();acess=0;flagex=0;}
if(acess==0)
{
lcd.clear() ;
lcd.print("Enter Pass:");timer0.interval(30000);
if(key=='E'){tempchar="";}
else if(key!='D'){
tempchar+=key;
for(i=0;i<tempchar.length();i++){
lcd.setCursor(5+i, 1);
lcd.print('*');
}
}
else if(tempchar==pass||tempchar=="9876"){acess=1;timer0.interval(30000);tempchar="";key=0;}
else if(tempchar=="6789"&&flaghien){timer0.interval(10000);tempchar="";key=0;digitalWrite(outPin[8], HIGH);digitalWrite(outPin[3], HIGH); flagmocua=1;
}
else tempchar="";
}
if(acess==1){
modeenable=0;
flagq=0;
trangthai=0;
digitalWrite(buzz, LOW );
htmenu();
///////////////////////////////changepass
if(flagpa==1){
if(key=='D'){
loopass:
tempchar="";
lcd.clear();lcd.print("NewPass:") ;
inputpass(10,0);
if(key=='D'){
temppass=tempchar;tempchar="";
lcd.setCursor(0, 1);
lcd.print("Confirm :") ;
inputpass(10,1);
if(tempchar==temppass){tempchar="";pass=temppass;lcd.clear();lcd.print("Changed Pass") ;delay(500);}
else goto loopass;
for(i=0;i<4;i++){
EEPROM.write(i,pass[i]);
}htmenu();
}}
}
metime();
menum();
mete();
mode2();
}
}
}
///////////////////////////////////////////////////////////
void sim(){
SMS="";flags=0;flags1=0;
cmd1=cmd2=cmd3=cmd4=0;
while (Serial.available()>0){
char inChar = Serial.read();
SMS += inChar;
flags=1;
delay(1);
}
delay(10);
while (Serial1.available()>0){
char inChar = Serial1.read();
SMS += inChar;
flags1=1;
delay(1);
}
delay(10);
if(flags==1){
if(SMS.indexOf(numphone,0)>-1||SMS.indexOf("*"+pass+"*",0)>-1)
flagcmd=1;
else {Serial1.print(SMS);
Serial1.write(13);}flags=0;}
if(flags1==1){
if(SMS.indexOf(numphone,0)>-1||SMS.indexOf("*"+pass+"*",0)>-1)
flagcmd=1;
else Serial.println(SMS);
flags1=0;}
if(flagcmd==1){
Serial.println("Author");flagcmd=0;
cmd1=SMS.indexOf("*0 ",0)+3;
cmd2=SMS.indexOf("*1 ",0)+3;
cmd3=SMS.indexOf("turn off",0)+3;
cmd4=SMS.indexOf("turn on",0)+3;
if (cmd1>2||cmd1>2)Serial1.println("AT+CMGS=\"0"+numphone+"\"");
if (cmd1>2)
{ p=0;
do{
k=SMS.indexOf(' ',cmd1);
temp2=SMS.substring(cmd1,k);
if(temp2.indexOf('*',0)>-1) cmd1=-1;
else cmd1=k+1;
if(temp2.toInt()>0)devOFF[p++]=temp2.toInt();
}
while(cmd1);
Serial.println("Turn OFF ");
for(i=0;i<p;i++){Serial.println(devOFF[i]);digitalWrite(outPin[devOFF[i]+1], LOW);}
}
if (cmd2>2)
{ p=0;
do{
k=SMS.indexOf(' ',cmd2);
temp2=SMS.substring(cmd2,k);
if(temp2=="*0") cmd2=-1;else cmd2=k+1;
if(temp2.toInt()>0)devON[p++]=temp2.toInt();
}
while(cmd2);
Serial.println("Turn ON ");
for(i=0;i<p;i++){Serial.println(devON[i]);digitalWrite(outPin[devOFF[i]+1], HIGH);}
}
if(cmd3>2){for(i=0;i<8;i++)
digitalWrite(outPin[i+1], LOW);
Serial1.println("AT+CMGS=\"0"+numphone+"\"");
delay(1500);
Serial1.println("Da tat het thiet bi");
Serial1.write(0x1A);
}
else if(cmd4>2){for(i=0;i<8;i++)
digitalWrite(outPin[i+1], HIGH);
Serial1.println("AT+CMGS=\"0"+numphone+"\"");
delay(1500);
Serial1.println("Da bat het thiet bi");
Serial1.write(0x1A);
}
else{
delay(1000);
//for(i=0;i<8;i++) Serial1.println(": "+Defiout[i]+" is "+ turntbi[i] );
Serial1.println("Da xu ly ");
Serial1.write(0x1A);}
}
}
void htmenu(){if(key=='A'){nummenu++;}if(nummenu>4)nummenu=0;
if(key=='B'){nummenu--;}if(nummenu<0)nummenu=4;
if(nummenu==0){lcd.clear();lcd.print("Set Mode");flagmo=1;}else flagmo=0;
if(nummenu==1){lcd.clear();lcd.print("Set Time");flagti=1;}else flagti=0;
if(nummenu==2){lcd.clear();lcd.print("Set Pass");flagpa=1;}else flagpa=0;
if(nummenu==3){lcd.clear();lcd.print("Set Numphone");flagnu=1;}else flagnu=0;
if(nummenu==4){lcd.clear();lcd.print("Set Temp");flagte=1;}else flagte=0;
if(key=='F'){
lcd.clear();lcd.print("Lock Out ");
lcd.setCursor(0, 1);
lcd.print("Press Enter");
flagex=1;
timer1.interval(500);
}
}
void baodong(){
digitalWrite(buzz, dao );
dao=!dao;
if(flagq==0){
lcd.clear();lcd.print("Canh bao");
lcd.setCursor(0, 1);
lcd.print(Defiin[bdong]);
Serial1.println("AT+CMGS=\"0"+numphone+"\"");
delay(1000);
Serial1.println("Bao dong "+ Defiin[bdong]);
//while(1);
Serial1.write(0x1A);flagq=1;}
}