实现米思齐按键控制LED

一曲冷凌霜 提交于 2019-12-02 09:05:03

实现米思齐按键控制LED

代码实现如下:

#include <IRremote.h>

IRrecv irrecv_10(10);
decode_results results_10;

long ir_value;
volatile int led;
volatile boolean ledstate;

void setup(){
  Serial.begin(9600);
  irrecv_10.enableIRIn();
  led = 0;
  ledstate = 0;
}

void loop(){
  if (irrecv_10.decode(&results_10)) {
    ir_value=results_10.value;
    String type="UNKNOWN";
    String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
    if(results_10.decode_type>=1&&results_10.decode_type<=13){
      type=typelist[results_10.decode_type];
    }
    Serial.print("IR TYPE:"+type+"  ");
    Serial.println(ir_value,HEX);
    if (ir_value == 0xFF629D) {
      ledstate = !ledstate;
      led = ledstate * 128;

    }
    if (1 == ledstate) {
      if (ir_value == 0xFFE21D) {
        led = constrain(led + 20, 0, 255);

      } else if (ir_value == 0xFFA25D) {
        led = constrain(led - 20, 0, 255);
      }
      Serial.println(led);
      analogWrite(6,led);

    }
    irrecv_10.resume();
  } else {
  }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!