我的博客日记
智能家居服务系统的代码
用到了寄存器,中断,等等
2017-10-2411:56:36
1 //**********************************************// 2 //*************智能家居服务系统*****************// 3 //************2017.10.22************************// 4 #include <SoftwareSerial.h> 5 #include <EEPROM.h> //寄存器库 6 #include <Servo.h> //舵机库 7 //**********************// 8 int pos; 9 int a=22;//寄存器记忆开关按下状态 10 int value;//寄存器定义 11 int address=5; 12 int led=4; 13 int fan=5; 14 int music=6; 15 char val='/'; 16 Servo myservo; 17 //*******************wait******************// 18 void setup() 19 { 20 EEPROM.write(address,a); 21 attachInterrupt(0, romote,RISING); 22 Serial.begin(9600); 23 pinMode(2,INPUT); 24 pinMode(4,OUTPUT); 25 pinMode(5,OUTPUT); 26 pinMode(6,OUTPUT); 27 pinMode(7,OUTPUT); 28 digitalWrite(2,LOW); 29 digitalWrite(5,HIGH); 30 myservo.attach(9); 31 myservo.write(pos); 32 delay(100); 33 myservo.detach(); 34 } 35 //*************************do it***************// 36 void loop() 37 { 38 char c=Serial.read(); 39 switch(c) 40 { 41 case('k'):dooropen();break; 42 case('g'):doorclose();break; 43 } 44 } 45 //************************************// 46 void romote()//外部中断 47 { 48 value = EEPROM.read(address);//寄存器记忆按钮状态 49 if(value==a) 50 { 51 digitalWrite(5,LOW); 52 EEPROM.write(address,11); 53 } 54 else 55 { 56 digitalWrite(5,HIGH); 57 EEPROM.write(address,22); 58 } 59 60 } 61 void ledopen() 62 { 63 digitalWrite(2,LOW); 64 } 65 void ledclose() 66 { 67 digitalWrite(2,HIGH); 68 } 69 void dooropen() 70 { 71 myservo.attach(9); 72 for(pos =60 ; pos <= 160; pos +=1) 73 { 74 myservo.write(pos); 75 delay(15); 76 } 77 bluetooth(); 78 } 79 void doorclose() 80 { 81 myservo.attach(9); 82 for(pos =160 ; pos >= 60; pos -=1) 83 { 84 myservo.write(pos); 85 delay(15); 86 } 87 } 88 void bluetooth()//蓝牙反馈数据 89 { 90 Serial.println("Success"); 91 }
@青山不移,文笔不息。学习,坚持,梦想青春!