当你按下手中的红外遥控器时,小车就会按照预先的程序,前进、后退、左转、右转,是不是很神奇呢?下面是我的程序代码。
#include <IRremote.h>
int RECV_PIN = 7;
int pinLB=14;
int pinLF=15;
int pinRB=16;
int pinRF=17;
long advence = 0x00FF629D;
long back = 0x00FFA857;
long stop = 0x00FF02FD;
long left = 0x00FFA25D;
long right = 0x00FFE21D;
IRrecv irrecv(RECV_PIN);
decode_results results;
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.println("Could not decode message");
}
else {
if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
}
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++)
{
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else
{
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
}
void setup(){
pinMode(RECV_PIN, INPUT);
pinMode(pinLB,OUTPUT);
pinMode(pinLF,OUTPUT);
pinMode(pinRB,OUTPUT);
pinMode(pinRF,OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}
int on = 0;
unsigned long last = millis();
void loop() {
if (irrecv.decode(&results)) {
if (millis() - last > 250) {
on = !on;
digitalWrite(13, on ? HIGH : LOW);
dump(&results);
}
if (results.value == advence ) {
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW);
}
if (results.value == back ) {
digitalWrite(pinRB,LOW);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW);
digitalWrite(pinLF,HIGH);
}
if (results.value == left ) {
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW);
}
if (results.value == right ) {
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
}
if (results.value == stop ) {
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
}
last = millis();
irrecv.resume();
}
}
上传代码后,就可以遥控你的智能小车了。