IR.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/unit/ir
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/ir
  8. *
  9. * Describe: ir.
  10. * Date: 2021/8/27
  11. *******************************************************************************
  12. Please connect to Port B,Use IR Unit to receive and test infrared receiving
  13. and transmitting 请连接端口B,使用红外单元接收和测试红外接收和发射.
  14. */
  15. #include <M5Stack.h>
  16. int ir_recv_pin = 36; // set the input pin. 设置引脚
  17. int ir_send_pin = 26;
  18. int last_recv_value = 0;
  19. int cur_recv_value = 0;
  20. void setup() {
  21. M5.begin();
  22. M5.Power.begin();
  23. pinMode(ir_recv_pin, INPUT);
  24. pinMode(ir_send_pin, OUTPUT);
  25. // send infrared light. 发送红外线
  26. // now, you can see the infrared light through mobile phone camera.
  27. // 现在,你可以通过手机摄像头看到红外光
  28. digitalWrite(ir_send_pin, 1);
  29. M5.Lcd.setTextSize(2);
  30. M5.Lcd.setCursor(0, 0);
  31. M5.Lcd.print("Test for IR receiver: ");
  32. }
  33. void loop() {
  34. // now, once you press the button on a remote controller to send infrared
  35. // light. 现在,一旦你按下遥控器上的按钮发送红外线 the screen will display
  36. // "detected!" 屏幕将显示“检测到!”
  37. cur_recv_value = digitalRead(ir_recv_pin);
  38. if (last_recv_value != cur_recv_value) {
  39. M5.Lcd.setCursor(0, 25);
  40. M5.Lcd.fillRect(0, 25, 150, 25, BLACK);
  41. if (cur_recv_value ==
  42. 0) { // 0: detected 1: not detected, 0检测到,1没有检测到
  43. M5.Lcd.print("detected!");
  44. }
  45. last_recv_value = cur_recv_value;
  46. }
  47. Serial.println(cur_recv_value);
  48. }