arduino 實現時鐘和計算器

代碼

#include <IRremote.h>
#include <IRremoteInt.h>
#include <LiquidCrystal.h> // 聲明 1602 液晶的函數庫

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;    // 紅外接收器的 OUT 引腳接在 PIN11 接口 定義irReceiverPin變量爲PIN11接口
IRrecv irrecv(irReceiverPin);   // 設置irReceiverPin定義的端口爲紅外信號接收端口
decode_results 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;   // 得用 5 號接口

// -----------------------------------------------------
void NewTone(byte tonePin, int frequency, int duration);

// ---------
void Setup()
{  
    lcd.begin(16, 2);         // 初始化1602液晶工作模式, 定義液晶顯示範圍爲 2 行 16 列字符 
    pinMode(tonePin, OUTPUT); // 定義模式爲輸出
    Serial.begin(9600);       // 開啓串口,波特率爲 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);   // 從模擬接口 A0 讀取溫度傳感器的信號
    t = (val * 0.48828125);
    lcd.print(t);
    lcd.print("C");  
    lcd.setCursor(7,0);         // 把光標定位在第 0 行,第 7 列
    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);                       // 延時 1ms
                digitalWrite(tonePin,LOW);      // 不發聲音
                delay(1);                       // 延時 1ms
            }
            for(fCount=0;fCount<100;fCount++)//輸出另一個頻率的聲音
            {
                digitalWrite(tonePin,HIGH);//發聲音
                delay(2);//延時2ms
                digitalWrite(tonePin,LOW);//不發聲音
                delay(2);//延時2ms
            }
        } 
        // 整點報時 if
        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);   //把光標定位在第0行,第7列
                    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);   //把光標定位在第0行,第7列
                    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;//跳出while
    }
  }
}

// -------------
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)) 
        {
            // 解碼成功,把數據放入results變量中
            // 把數據輸入到串口
            Serial.print("irCode: ");
            Serial.print(results.value, HEX); // 顯示紅外編碼
            Serial.print('\n');
            if (0xFF30CF== results.value)     // 按下 1
            {
                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)   // 按下 2
            {
                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)    // 按下 3
            {
                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)    // 按下 4
            {
                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)    // 按下 5
            {
                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)    // 按下 6
            {
                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)    // 按下 7
            {
                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)    // 按下 8
            {
                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)    // 按下 9
            {
                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)    // 按下 0
            {
                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);     // 延時 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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章