代碼
#include <IRremote.h>
#include <IRremoteInt.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,10,9,8,7,6);
char *sec[60]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
char *minute[60]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"};
char *hour[12]={"00","01","02","03","04","05","06","07","08","09","10","11"};
const int irReceiverPin = 3;
IRrecv irrecv(irReceiverPin);
decode_results results;
int redNum[2] = {0,0};
int redDigit = 0;
int flag = 0;
int symbolSelect;
float t = 0;
long val;
int j = 8, k = 59;
int potPin = 5, tfExit = 0, second = 0, fCount;
int length;
int tonePin = 5;
void NewTone(byte tonePin, int frequency, int duration);
void Setup()
{
lcd.begin(16, 2);
pinMode(tonePin, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}
void DisplayTime()
{
int i;
while(1)
{
lcd.setCursor(0, 0);
lcd.print("Time = ");
lcd.setCursor(0, 1);
lcd.print("Temp = ");
val = analogRead(potPin);
t = (val * 0.48828125);
lcd.print(t);
lcd.print("C");
lcd.setCursor(7,0);
lcd.print(hour[j]);
lcd.print(':');
lcd.setCursor(10,0);
lcd.print(minute[k]);
lcd.print(':');
second = second % 60;
for(i = second; i < 60; i++)
{
lcd.setCursor(13, 0);
lcd.print(sec[i]);
delay(1000);
if( k == 0 && i < 10)
{
for(fCount = 0; fCount < 80; fCount++)
{
digitalWrite(tonePin, HIGH);
delay(1);
digitalWrite(tonePin,LOW);
delay(1);
}
for(fCount=0;fCount<100;fCount++)
{
digitalWrite(tonePin,HIGH);
delay(2);
digitalWrite(tonePin,LOW);
delay(2);
}
}
if (irrecv.decode(&results))
{
if(results.value == 0xFF22DD)
{
tfExit = 1;
break;
}
if(results.value == 0xFFC23D)
{
k++;
if(k == 60)
{
k = 0;
j++;
lcd.setCursor(7,0);
lcd.print(hour[j]);
}
lcd.setCursor(10,0);
lcd.print(minute[k]);
lcd.print(':');
}
if(results.value == 0xFF906F)
{
k--;
if(k == -1)
{
k = 59;
j--;
lcd.setCursor(7, 0);
lcd.print(hour[j]);
}
lcd.setCursor(10,0);
lcd.print(minute[k]);
lcd.print(':');
}
}
irrecv.resume();
}
second = i;
if(second >= 60)
k++;
if(k == 60)
{
k = 0;
j++;
}
if(j > 11)
{
j = 0;
}
if(tfExit == 1)
{
break;
}
}
}
void Calculate()
{
redNum[0] = 0;
redNum[1] = 0;
flag = 0;
lcd.clear();
lcd.print("welcome to ");
lcd.setCursor(0,1);
lcd.print(" calculator");
delay(4000);
lcd.clear();
while (1)
{
if (irrecv.decode(&results))
{
Serial.print("irCode: ");
Serial.print(results.value, HEX);
Serial.print('\n');
if (0xFF30CF== results.value)
{
redDigit = 1;
if (flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('1');
}
if (0xFF18E7 == results.value)
{
redDigit = 2;
if (flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('2');
}
if (0xFF7A85== results.value)
{
redDigit = 3;
if (flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('3');
}
if(0xFF10EF== results.value)
{
redDigit = 4;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('4');
}
if(0xFF38C7== results.value)
{
redDigit = 5;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('5');
}
if(0xFF5AA5== results.value)
{
redDigit = 6;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('6');
}
if(0xFF42BD== results.value)
{
redDigit = 7;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('7');
}
if(0xFF4AB5== results.value)
{
redDigit = 8;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('8');
}
if(0xFF52AD== results.value)
{
redDigit = 9;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('9');
}
if(0xFF6897== results.value)
{
redDigit = 0;
if(flag == 0)
redNum[0] = redNum[0]*10 + redDigit;
else
redNum[1] = redNum[1]*10 + redDigit;
lcd.print('0');
}
if(0xFFA857 == results.value)
{
lcd.print('+');
flag = 1;
symbolSelect = 1;
}
if(0xFFE01F == results.value)
{
lcd.print('-');
flag = 1;
symbolSelect = 2;
}
if(0xFF9867 == results.value)
{
lcd.print('*');
flag = 1;
symbolSelect = 3;
}
if(0xFFB04F == results.value)
{
lcd.print('/');
flag = 1;
symbolSelect = 4;
}
if(0xFFE21D == results.value)
{
lcd.print('=');
Serial.print(redNum[0]);
Serial.print('\n');
Serial.print(redNum[1]);
if(symbolSelect == 1)
lcd.print(redNum[0] + redNum[1]);
else if(symbolSelect == 2)
lcd.print(redNum[0] - redNum[1]);
else if(symbolSelect == 3)
lcd.print(redNum[0] * redNum[1]);
else if(symbolSelect == 4)
lcd.print(redNum[0] / redNum[1]);
}
if(results.value == 0xFF02FD)
{
return;
}
delay(600);
irrecv.resume();
}
}
}
void Loop()
{
second = millis() / 1000;
lcd.clear();
lcd.print("2016/12/16");
if (irrecv.decode(&results))
{
if(results.value == 0xFFA25D)
{
DisplayTime();
}
else if(results.value == 0xFF629D)
{
Calculate();
}
irrecv.resume();
}
delay(2000);
}