在前一课中我们学会了如何控制电机,这一课将在前面一课基础上加上红外控制的功能,通过红外控制小车运动。
这一课只需要安装一个红外接收模块就可以了。
所需配件:
M2.5*10十字螺丝 x2颗
M2.5螺帽 x2颗
用2两颗M2.5*10十字螺丝加螺帽将红外接收模块安装在底盘表面(放照片!)
将红外接收模块按照下表顺序连接到arduino uno
Arduino UNO | 红外接收模块 |
S | D4 |
+ | 5V |
– | GND |
要使用红外功能,需要在arduino IDE中安装一个红外解码库IRremote
,将其解压后放到arduino IDE安装目录下的libraries文件夹中。下载示例代码并解压,用arduino IDE打开lesson-2.ino,下面对部分代码作简单说明。
#define IR_ADVANCE 0x00FF18E7 //code from IR controller "▲" button #define IR_BACK 0x00FF4AB5 //code from IR controller "▼" button #define IR_RIGHT 0x00FF5AA5 //code from IR controller ">" button #define IR_LEFT 0x00FF10EF //code from IR controller "<" button
上面4行代码分别表示遥控器上▲、▼、>、<的红外编码
每个按键的红外编码可以烧录下面的示例代码获得
IRrecv IR(IRPIN); // IRrecv object IR get code from IR remoter decode_results IRresults;
实例化一个IRrecv对象,用于解码红外信号;并将解码结果存放在IRresults中
void do_IR_Tick() { if(IR.decode(&IRresults)) { if(IRresults.value==IR_ADVANCE) { Drive_Num=GO_ADVANCE; } else if(IRresults.value==IR_RIGHT) { Drive_Num=GO_RIGHT; } else if(IRresults.value==IR_LEFT) { Drive_Num=GO_LEFT; } else if(IRresults.value==IR_BACK) { Drive_Num=GO_BACK; } IRresults.value = 0; IR.resume(); } }
解码红外信号,并将收到的红外信号与预定义的值比较,若相等则给Drive_Num赋相应值
void do_Drive_Tick() { switch (Drive_Num) { case GO_ADVANCE: go_ahead(20);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_ADVANCE code is detected, then go advance case GO_LEFT: turn_left(1);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_LEFT code is detected, then turn left case GO_RIGHT: turn_right(1);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_RIGHT code is detected, then turn right case GO_BACK: go_back(20);JogFlag = true;JogTimeCnt = 1;JogTime=millis();break;//if GO_BACK code is detected, then backward default:break; } Drive_Num=DEF; //keep current moving mode for 200 millis seconds if(millis()-JogTime>=200) { JogTime=millis(); if(JogFlag == true) { stopFlag = false; if(JogTimeCnt <= 0) { JogFlag = false; stopFlag = true; } JogTimeCnt--; } if(stopFlag == true) { JogTimeCnt=0; go_stop(); } } }
这段代码是红外控制的核心代码,按下对应按键小车执行相应动作,若所按按键未在程序中定义,小车不作回应。
用USB线将arduino 和PC连接起来,选择正确的板卡型号和端口将其烧录到arduino uno中。将遥控器塑料垫片扯掉,对着红外接收器按按键。
“▲”:前进
“▼”:后退
“>”:右转
“<“左转
如果小车不动,请检查如下事项
DownLoad Url osoyoo.com
You must be logged in to post a comment.
Can sketches be combined? Can the IR control be used to activate the Robot movement from lesson 1, for example? Or, can the IR remote be used at the same time as the line follow?
Can an obstacle avoidance module be added to the 2WD Robot Kit?
Thanks
the code on the cd does not work …. IRremote.H: no such file or directory
please help!
please read the software installation STEP 2, https://osoyoo.com/2017/09/21/2wd-robot-car-infrared-remote/#4
You need install the IRremote library first.
I’m having difficulty with this lesson. I have the library imported and code loaded with no errors. When I try the IR remote, I see the sensor blinking showing it is receiving the signal, but the car only sometimes goes and only for a split second. The batteries are new and fully charged and the IR Remote is close and unhindered. The code in lesson one worked perfectly, so that eliminates the wiring/boards with the exception of the IR receiver.
Any thoughts? Thank you!
hi, TheMuffinMan,
It seems that your IR receiver did not send the data to Arduino. I believe the wire from IR receiver S pin to extension S4 pin is loose or broken. Please change a new wire and try again. if it does not work, then let me know I will tell you how to make further test.
thanks
admin
Hello, and thanks for the quick response.
I went ahead and replaced all the wiring, assuring that they were all were snug.
The issue still persists though. Any suggestions would be much appreciated.
Thank you,
TheMuffinMan
Ok. Please keep all your current wire connection (make sure IR receiver S pin is connected to D4) and then download an IR test sketch from https://osoyoo.com/driver/IRrecvDemo.ino
Above sketch will tell you of the IR code of each key on a remote controller. After upload above sketch into Arduino, Open the serial monitor, then press ∧ V > < (the direction arrow keys) in the remote, it will show result in following sequence.
00FF18E7
00FF4AB5
00FF5AA5
00FF10EF
They are the IR codes of ∧ V > < four keys.
Please let me know :
1)Does the serial monitor show any message ?
2)if 1) answer is yes, then what is the IR code of your IR remote ∧ V > < four keys.
I will tell you solution after I got your answer.
Thank you for getting back with me. I did output in the serial monitor. Here’s the code output:
∧ = 3D9AE3F7
V = 1BC0157B
> = 449E79F
< = 8C22657B
now I understand what is your problem. Your remote sends different IR code to our receiver. Ok, that is easy to fix.
Please open your lesson2.ino file, you can see line 10,11,12,13 as follows:
#define IR_ADVANCE 0x00FF18E7 //code from IR controller “▲” button
#define IR_BACK 0x00FF4AB5 //code from IR controller “▼” button
#define IR_RIGHT 0x00FF5AA5 //code from IR controller “>” button
#define IR_LEFT 0x00FF10EF //code from IR controller “<" button
Change above code to follows which match your IR code :
#define IR_ADVANCE 0x3D9AE3F7 //code from IR controller “▲” button
#define IR_BACK 0x1BC0157B //code from IR controller “▼” button
#define IR_RIGHT 0x449E79F //code from IR controller “>” button
#define IR_LEFT 0x8C22657B //code from IR controller “<" button
Also please double check the key > , this key code should be 8 digits , but you only give me 7 digits. I double you might have typed wrong code. If I am correct, please put change line 12 with correct code.
Let me know if above code works.
I just assembled this kit and I’m trying lesson 2 (with the IR). The code itself seems to want to use pin 12 for the IR receiver and the code to simply print the codes received wants to use pin 2
I think these are for the line following sensor – no ? the IR is connected to Pin 10
Nothing works – not the original code or if I change your debugging code (to print just the codes) to use pin 10
any suggestions ?
hi, mattganis,
You are right, the code has some error, you need to change the receiver port to 10 instead of 12 in our code line 12.
If it still not working , then please read following link:
https://osoyoo.com/2017/09/21/2wd-robot-car-infrared-remote/#tbst
It will ask you to run a testing program irdemo.ino , open your Serial monitor and run irdemo.ino file, press ^ key in your IR controller, let us know if you can see any response, if not , please email to [email protected] and we will give you solution.
i followed instructions but when i reverse the car the right wheel doesnt turn, it works fine otherwise
Can your car run lesson 1 very well? or only in lesson 2 reverse movement has problem?