Skip to content

Instantly share code, notes, and snippets.

@a3510377
Last active October 22, 2023 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a3510377/2a4bbe6dbf9fa083751092499a860f27 to your computer and use it in GitHub Desktop.
Save a3510377/2a4bbe6dbf9fa083751092499a860f27 to your computer and use it in GitHub Desktop.
獲取紅外線訊號(使用 IRremote v4)
#include <Arduino.h>
#include <IRremote.hpp>
#define ADDRESS 0x01
void forward(void);
void left(void);
void right(void);
void back(void);
void stops(void);
uint16_t last_command;
unsigned int last_time;
void setup() {
Serial.begin(115200);
IrReceiver.begin(2);
}
void loop() {
if (IrReceiver.decode()) {
IRData data = IrReceiver.decodedIRData;
uint16_t command = data.command;
// 是否為一直按下,如果是將舊的數值套用成新的
if (data.flags & IRDATA_FLAGS_IS_REPEAT) command = last_command;
else last_command = command;
Serial.print("address: ");
Serial.println(data.address, HEX);
Serial.print("command: ");
Serial.println(command, HEX);
Serial.println("------");
if (data.address == ADDRESS) {
switch (command) {
case 0x01:
forward();
break;
case 0x02:
left();
break;
case 0x03:
right();
break;
case 0x04:
back();
break;
}
}
IrReceiver.resume();
last_time = millis();
} else if (millis() - last_time > 100) {
stops();
}
}
void forward() {}
void left() {}
void right() {}
void back() {}
void stops() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment