diy/아두이노

아두이노 나노로 리모컨 카운터(계수기) 만들기

do121 2024. 1. 13. 08:41

리모컨으로 작동하는 계수기를 만들 일이 있어 아두이노 나노와 lcd를 조합해서 만들어봄

IRremote.h 라이브러리는 2.5.0 버전을 사용함, 2.8이후버전대를 사용할 경우 소스를 수정해야함

 

 

 

 

#include <IRremote.h>  //ver 2.5.0

#include "U8glib.h"

#include <Wire.h>               // I2C control library
#include <LiquidCrystal_I2C.h>  // LCD library

int RECV_PIN = 7;

long cnt = 0; 

IRrecv irrecv(RECV_PIN);

decode_results results;

//sda->a4, scl -> a5, vcc->5v
LiquidCrystal_I2C lcd(0x3f, 16, 2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

#define codeplus1 41565
#define codeminus1 25245
#define codeplus10 57885
#define codeminus10 8925

#define codeclear 14535
#define bkon 26775
#define bkoff 45135

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();


  lcd.init();  // initialize the lcd

  // Print a message to the LCD.
  lcd.backlight();  // turn on backlight

  lcd.setCursor(8, 1);
  lcd.print("Ready...");


  delay(100);
}



void loop() {
  if (irrecv.decode(&results)) {
    

    unsigned int value = results.value;  //value =>dec, 10십진수임
    Serial.println(String(value, HEX));
    
    switch (value) {
      case codeplus1:
        Serial.println("codeplus1");
        cnt = cnt + 1;
        Serial.println(String(cnt));
        lcd.clear();
        lcd.print("Count : ");  
        lcd.print(cnt);
        break;
      case codeplus10:
        Serial.println("codeminus1");
        cnt = cnt + 10;
        lcd.clear();
        lcd.print("Count : ");
        lcd.print(cnt);
        break;
      case codeminus1:
        Serial.println("codeminus1");
        cnt = cnt - 1;
        lcd.clear();
        lcd.print("Count : ");
        lcd.print(cnt);
        break;
      case codeminus10:
        cnt = cnt - 10;
        lcd.clear();
        lcd.print("Count : ");
        lcd.print(cnt);
        break;
      case codeclear:
        cnt = 0;
        lcd.clear();
        lcd.print("Count : ");
        lcd.print(cnt);
        break;
      case bkon:
        Serial.println("bkon");
        lcd.backlight();
        break;
      case bkoff:
        Serial.println("bkoff");
        lcd.noBacklight();
        break;
      
    }
    
    Serial.println(value);
    irrecv.resume();
  }
  delay(100);
  
}