极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9974|回复: 4

红外线根据温度控制空调和风扇的代码问题请教

[复制链接]
发表于 2013-7-30 13:03:18 | 显示全部楼层 |阅读模式
代码如下:
========================================================
//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 就运行正常了。
请大家帮我看一下问题出现在什么地方?谢谢!
回复

使用道具 举报

 楼主| 发表于 2013-7-30 17:15:53 | 显示全部楼层
没人帮帮我吗?
回复 支持 反对

使用道具 举报

发表于 2013-8-26 09:33:36 | 显示全部楼层
试了一下,但是不会,不过我这几天正做空调的红外解码呢,我想把每个温度每个状态都弄出来,但是单片机内存不够,正苦恼呢,这些解码都有规律吗
回复 支持 反对

使用道具 举报

发表于 2013-8-26 13:58:30 | 显示全部楼层
有的時候抓出來的數字稍微有誤差,稍微整理一下還是可以
建議先做個簡單程序單純發送,確定抓下來的碼沒問題再來找規律
格力空調我用9000+4400開頭,訊號段650+550跟650+1600,結尾650+40000可以發送
回复 支持 反对

使用道具 举报

发表于 2013-8-26 15:06:33 | 显示全部楼层
内存不够?
试着搜下我的帖子,把你的数据定义到flash里面。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 21:31 , Processed in 0.037926 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表