// DS1302: CE pin -> Arduino Digital 2 // I/O pin -> Arduino Digital 3 // SCLK pin -> Arduino Digital 4 #include // Init the DS1302 DS1302 rtc(2, 3, 4); // Init a Time-data structure Time t; #include Servo myservo; int pos = 0; #include // IRremote库声明 int RECV_PIN = 8; //定义红外接收器的引脚为8 IRrecv irrecv(RECV_PIN); decode_results results; //解码结果放在 decode results结构的 result中 const int GreenLED = 7; bool Servo_Flag = false; bool flag=true; void setup() { Serial.begin(9600); irrecv.enableIRIn(); // 启动接收器 pinMode(GreenLED,OUTPUT) ; //LED,指示状态 digitalWrite(GreenLED,HIGH); myservo.attach(9); // attaches the servo on pin 9 to the servo object myservo.write(0); // Set the clock to run-mode, and disable the write protection rtc.halt(false); rtc.writeProtect(false); // The following lines can be commented out to use the values already stored in the DS1302 //rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY rtc.setTime(12,0,0); // Set the time to 12:00:00 (24hr format) rtc.setDate(12,12,2021); // Set the date to August 6th, 2010 } void loop() { if (irrecv.decode(&results))//解码成功,收到一组红外讯号 { Serial.println(results.value, HEX);//以16进制换行输出接收代码 irrecv.resume(); // 接收下一个值 if (results.value == 0XFF18E7) { Servo_Flag=false; //关闭 } if (results.value == 0XFF4AB5) { Servo_Flag=true; //开启 } } delay(100); Serial.print("Servo_Flag-->"); Serial.println(Servo_Flag); //////////////////////////////////////////////////////////////// Time_Get(); if(t.hour==22&&t.min==0&&t.sec==0) //晚上12点关灯信号 { Servo_Flag=false; } /////////////////////////////////////////////////////////////// if(Servo_Flag==true)//开启 { Open(); }else{ Close(); } } void Time_Get() { // Get data from the DS1302 t = rtc.getTime(); // Send date over serial connection Serial.print("Today is the "); Serial.print(t.date, DEC); Serial.print(". day of "); Serial.print(rtc.getMonthStr()); Serial.print(" in the year "); Serial.print(t.year, DEC); Serial.println("."); // Send Day-of-Week and time //Serial.print("It is the "); //Serial.print(t.dow, DEC); Serial.print(". day of the week (counting monday as the 1th), and it has passed "); Serial.print(t.hour, DEC); Serial.print(" hour(s), "); Serial.print(t.min, DEC); Serial.print(" minute(s) and "); Serial.print(t.sec, DEC); Serial.println(" second(s) since midnight."); // Send a divider for readability Serial.println(" - - - - - - - - - - - - - - - - - - - - -"); // Wait one second before repeating :) delay (1000); } void Open() { if(flag==true) { for (pos = 0; pos <= 90; pos += 1) { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position } flag=false; } } void Close() { if(flag==false) { for (pos = 90; pos >= 0; pos -= 1) { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position } flag=true; } }