|
|
代码如下:
========================================================
//The LED sender must connect to 3 pin
#include <OneWire.h>
#include <DallasTemperature.h>
#include <IRremote.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
unsigned int rawCodes_fan_close[48] ={
1150,500,1150,550,300,1350,1150,500,1150,550,300,1350,300,1350,300,1350,350,1350,300,1350,300,1350,1150,9244,1250,450,1250,400,400,1250,1250,450,1250,450,350,1250,450,1250,400,1250,400,1250,450,1200,450,1250,1250,13412};
unsigned int rawCodes_fan_notroll[48]={
1250,400,1250,400,450,1250,1250,400,1250,400,450,1200,450,1250,1250,400,450,1200,450,1250,400,1250,450,7950,1300,400,1250,400,450,1200,1300,400,1250,400,450,1200,450,1250,1250,400,450,1200,450,1200,500,1200,450,4808}; //fan head not rolls
unsigned int rawCodes_condition_close[74] ={8900,4450,650,1650,600,550,650,550,600,600,600,550,650,550,600,550,650,550,600,550,650,1650,650,550,650,1600,650,550,650,550,650,500,650,550,650,500,650,550,650,500,650,550,650,550,650,500,650,550,650,500,650,550,650,550,650,500,650,550,650,1600,650,550,650,1650,600,550,650,550,650,1600,650,550,650,15248};
unsigned int rawCodes_condition_open[74] ={8950,4400,650,1650,650,500,650,550,650,1650,650,550,650,500,650,550,600,600,600,550,650,1650,650,500,650,1650,650,550,650,500,650,550,650,500,650,550,650,550,650,500,650,550,650,500,650,550,650,550,650,500,650,550,650,500,650,550,650,500,650,1650,650,550,650,1600,650,550,650,550,600,1650,650,550,650,32896};
IRsend irsend;
float temperaturesValue; // The temperatures value
int SwitchTag = 0;
int ConditionChange = 0;
int FanChange = 0;
int UpperTemperatures = 30; //The High limite temperatures
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
// Start up the library
sensors.begin();
sensors.requestTemperatures(); // Send the command to get temperatures
temperaturesValue = sensors.getTempCByIndex(0); //via sensors ID get the temperatures
Serial.println(temperaturesValue);
if (temperaturesValue > UpperTemperatures ){
Serial.println("condition will open");
SwitchTag = 1;
ConditionChange = 0;
}
else{
Serial.println("fan will open");
SwitchTag = 0;
FanChange = 0;
}
}
void fanChangeStatus(){
irsend.sendRaw(rawCodes_fan_close,48, 38);
delay(1000);
irsend.sendRaw(rawCodes_fan_notroll,48, 38);
Serial.println(temperaturesValue);
Serial.println("fan change status");
Serial.flush();
}
void conditionOpen(){
irsend.sendRaw(rawCodes_condition_open,74, 38);
//delay(1000);
//irsend.sendRaw(rawCodes_condition_open,74, 38);
//delay(1000);
Serial.println("air condition open");
Serial.flush();
}
void conditionClose(){
irsend.sendRaw(rawCodes_condition_close,74, 38);
//delay(1000);
//irsend.sendRaw(rawCodes_condition_close,74, 38);
//while(Serial.read() >= 0);
//delay(1000);
Serial.println("air condition closed");
Serial.flush();
}
void loop(){
//sensors.begin();
sensors.requestTemperatures(); // Send the command to get temperatures
temperaturesValue = sensors.getTempCByIndex(0); //via sensors ID get the temperatures
Serial.println(temperaturesValue);
if (temperaturesValue > UpperTemperatures && SwitchTag == 1){
conditionOpen();
SwitchTag = 0;
if (FanChange == 1){
fanChangeStatus();
}
ConditionChange = 1;
}
else if (temperaturesValue < UpperTemperatures && SwitchTag == 0){
fanChangeStatus();
SwitchTag = 1;
if (ConditionChange == 1){
conditionClose();
}
FanChange = 1;
}
}
===============================================
实现功能:
通过温度检测,高于30度启动空调,低于30度启动风扇
问题:
运行后串口没有输出,就连SETUP()中的Serial.println()都没有输出结果。但屏弊掉任意两个 irsend.sendRaw 就运行正常了。
请大家帮我看一下问题出现在什么地方?谢谢!
|
|