ttyp 发表于 2012-11-17 17:05:35

我的红外遥控时钟

本帖最后由 ttyp 于 2012-11-25 10:34 编辑

这是我第一个实用的arduino成品。

使用DS1307为时钟模块,接VCC,GND,A4,A5
LCD1602为显示模块,接2,3,4,5,6,7脚
红外接收模块VS1838B,接11脚

使用红外遥控的mode按钮进入调整日期调整模式,power按钮退出,+,-按钮调整日期,play可以暂停日期,DHT11模块为可扩展模块,插上后在右下角会闪烁显示当前温度和湿度(可热插拔)

下面是代码:

/*
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* DHT11 data to digital pin 11
* VS1838B data to digital pin 12
* DS1307 sda,sck to analog pin 4,5
*/

// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include <DS1307A.h>
#include <IRremote.h>
#include <dht.h>

dht DHT;

int DHT11_PIN =11; //DHT11 PIN 11 连接UNO 11
int RECV_PIN = 12;//定义红外接收器的引脚为12

IRrecv irrecv(RECV_PIN);

decode_results results;

DS1307A ds = DS1307A(2000);
DS1307A_RAM ram;

int mode = 0;
int show = 1;

bool pause = false;
bool blink = false;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
    // set up the LCD's number of columns and rows:
    Serial.begin(9600);
    lcd.begin(16, 2);
    byte C = {
      B10000,
      B00000,
      B00111,
      B01000,
      B01000,
      B01000,
      B00111,
    };   
   
    lcd.createChar(0, C);      
    irrecv.enableIRIn(); // 初始化红外接收器
    //ds.setDate(2012,11,14);
    //ds.setTime(22,47,40);
    //ds.setWeek(WEDNESDAY);
}

void loop() {
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    Time t = ds.getTime();
   
    if (irrecv.decode(&results)) {
      switch(results.value){
            case 0xFF906F://+
            change(&t,mode,1);            
            break;
            case 0xFFA857://-
            change(&t,mode,-1);
            break;
            case 0xFFC23D://>>
            break;
            case 0xFF02FD://<<      
            break;
            case 0xFFA25D://power
            mode = 0;
            blink = false;
            break;
            case 0xFFE21D://silence
            break;
            case 0xFF629D://mode
            if(mode<8)
                mode++;
            else
                mode=0;
            break;
            case 0xFF22DD://play
            pause=!pause;
            ds.halt(pause);
            break;
            case 0xFFE01F://EQ
            break;
            case 0xFF6897://0
            break;
            case 0xFF30CF://1
            break;
            case 0xFF18E7://2
            break;
            case 0xFF7A85://3
            break;
            case 0xFF10EF://4
             break;
            case 0xFF38C7://5
             break;
            case 0xFF5AA5://6
            break;
            case 0xFF42BD://7
            break;
            case 0xFF4AB5://8
            break;
            case 0xFF52AD://9
            break;
            case 0xFF9867://return
            break;
            case 0xFFB04F://usd
            break;
      }      
      irrecv.resume();
    }   
   


    lcd.setCursor(0,0);
   
    char D;
    char T;
    char W;
   
    if(mode!=0)blink=!blink;
   
    strcpy(D,ds.getDateString("YMD",'-'));
    strcpy(T,ds.getTimeString("HMS",':'));
    strcpy(W,ds.getWeekString(FORMAT_SHORT));

    if(blink)
    {
      switch(mode)
      {
      case 1:
          D = ' ';
          D = ' ';   
          D = ' ';
          D = ' ';               
          break;
      case 2:
          D = ' ';
          D = ' ';      
          break;
      case 3:
          D = ' ';
          D = ' ';      
          break;
      case 4:
          W = ' ';
          W = ' ';
          W = ' ';         
          break;                  
      case 5:
          T = ' ';
          T = ' ';
          break;
      case 6:
          T = ' ';
          T = ' ';
          break;
      case 7:
          T = ' ';
          T = ' ';
          break;                           
      }      
    }
      
    lcd.print(D);   
    lcd.print("   ");
    lcd.print(W);
    lcd.setCursor(0, 1);      
    lcd.print(T);
   
    lcd.setCursor(13,1);
   
    int chk = DHT.read11(DHT11_PIN);
   
    char M = {0};
    char N = {0};

   
    switch (chk)
    {
      case DHTLIB_OK:
            if(DHT.humidity>=10){
            M = '0' + DHT.humidity /10;
            }else{
            M = ' ';
            }            
            M = '0' + ((int)DHT.humidity%10);            
            if(DHT.temperature>=10){
            N = '0' + DHT.temperature /10;
            }else{
            N = ' ';
            }            
            N = '0' + ((int)DHT.temperature%10);   
            
            M = '%';
            M = '\0';
            N = '\0';
   
            if(show%6==3)
            lcd.print(M);
            else if(show%6==0){                           
            lcd.print(N);   
            lcd.write((uint8_t)0);
            }   
               
            if(show++>256)show=1;            
            break;
      default:
            lcd.print("   ");
            break;
    }            
   

   
   
    delay(500);
}

void change(Time *t,int m,int v)
{
int dir = 0;
if(v>0)
    dir=1;
else
    dir=-1;

   if(dir==1)
   switch(m)
      {
      case 1:
          if(t->year<2098)
            t->year+=dir;
          else
            t->year=2000;
          break;
      case 2:
          if(t->month<12)
            t->month+=dir;
          else
            t->month=1;      
          break;
      case 3:
          if(t->date<31)
            t->date+=dir;
          else
            t->date=1;   
          break;
      case 4:
          if(t->week<6)
            t->week+=dir;
          else
            t->week=0;      
          break;                  
      case 5:
          if(t->hour<23)
            t->hour+=dir;
          else
            t->hour=0;
          break;
      case 6:
          if(t->minute<59)
            t->minute+=dir;
          else
            t->minute=0;
          break;
      case 7:
          if(t->second<59)
            t->second+=dir;
          else
            t->second=0;
          break;                           
      }
    else
   switch(m)
      {
      case 1:
          if(t->year>2000)
            t->year+=dir;
          else
            t->year=2099;
          break;
      case 2:
          if(t->month>1)
            t->month+=dir;
          else
            t->month=12;      
          break;
      case 3:
          if(t->date>1)
            t->date+=dir;
          else
            t->date=31;   
          break;
      case 4:
          if(t->week>0)
            t->week+=dir;
          else
            t->week=6;      
          break;                  
      case 5:
          if(t->hour>0)
            t->hour+=dir;
          else
            t->hour=23;
          break;
      case 6:
          if(t->minute>0)
            t->minute+=dir;
          else
            t->minute=59;
          break;
      case 7:
          if(t->second>0)
            t->second+=dir;
          else
            t->second=59;
          break;                           
      }   
      
      if(m>=1&&m<=3)
      ds.setDate(t->year,t->month,t->date);
      else if(m==4)
      ds.setWeek(t->week);
      else if(m>=5&&m<=7)
      ds.setTime(t->hour,t->minute,t->second);      
}

这是效果:



下面是用到的库:

弘毅 发表于 2012-11-18 12:21:32

{:soso_e154:}求重新更新下附件。。前两天给论坛打安全补丁。。。没有修复附件上传部分。。结果文件没传到服务器上@@

ttyp 发表于 2012-11-18 14:10:28

已经更新,可以下载了

迷你强 发表于 2012-11-18 14:21:57

程序好长。。。。。
页: [1]
查看完整版本: 我的红外遥控时钟