本帖最后由 ntwhq 于 2016-12-17 16:16 编辑
将红外接收模块装在小车的上面,并将接收面朝上,扩大其接收红外线的角度,如下图:
转弯时两个车轮旋转方向一致,采用差速的方式转弯,转弯速度较慢,便于控制。
程序下载:
红外遥控.rar
(892 Bytes, 下载次数: 250)
自动生成的文本代码:
[mw_shl_code=cpp,true]#include <IRremote.h>
void forward(long x) {
digitalWrite(3,LOW);
analogWrite(5,x);
digitalWrite(9,LOW);
analogWrite(6,x);
}
void back(long x) {
digitalWrite(3,HIGH);
analogWrite(5,(255 - x));
digitalWrite(9,HIGH);
analogWrite(6,(255 - x));
}
void stop() {
digitalWrite(3,LOW);
analogWrite(5,0);
digitalWrite(9,LOW);
analogWrite(6,0);
}
void turnright(long x) {
digitalWrite(3,LOW);
analogWrite(5,x);
digitalWrite(9,LOW);
analogWrite(6,(x - 50));
}
void turnleft(long x) {
digitalWrite(3,LOW);
analogWrite(5,(x - 50));
digitalWrite(9,LOW);
analogWrite(6,x);
}
long ir_item;
IRrecv irrecv_11(11);
decode_results results_11;
void setup()
{
pinMode(3, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
irrecv_11.enableIRIn();
}
void loop()
{
if (irrecv_11.decode(&results_11)) {
ir_item=results_11.value;
String type="UNKNOWN";
String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};
if(results_11.decode_type>=1&&results_11.decode_type<=13){
type=typelist[results_11.decode_type];
}
Serial.print("IR TYPE:"+type+" ");
Serial.println(ir_item,HEX);
if (ir_item == 0xFF629D) {
forward(160);
} else if (ir_item == 0xFFA857) {
back(120);
} else if (ir_item == 0xFF22DD) {
turnleft(120);
} else if (ir_item == 0xFFC23D) {
turnright(120);
} else if (ir_item == 0xFF02FD) {
stop();
}
irrecv_11.resume();
} else {
}
}[/mw_shl_code]
Altar小车Mixly图形化编程1_超声波避障
Altar小车Mixly图形化编程2_巡迹
|