|
用红外接收RC6遥控器的信号,控制舵机。
舵机的三个角度0,90,180分别对应变速箱的三个档位。
我东北口音比较重,大家可以放静音{:soso_e110:}
代码非常简单,我是超级外行,在群里一顿问,才改好了这几行代码。
程序根据红外接收库里面的例子程序和自带伺服库修改而来。- /*
- * 2012年12月20日
- * 变速箱程序
- * 变速箱的三个档位对应三个角度并且取后3位有效位&0xfff
- * 盛京夏洛克
- */
- #include <IRremote.h>
- #include <Servo.h>
- int RECV_PIN = 11;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- Servo myservo; // create servo object to control a servo
- // a maximum of eight servo objects can be created
- int pos = 90; // 初始状态停留在二档安全位置
- void setup()
- {
- Serial.begin(9600);
- irrecv.enableIRIn(); // Start the receiver
- myservo.attach(9); // attaches the servo on pin 9 to the servo object
- }
- void loop() {
- if (irrecv.decode(&results)) {
- // Serial.println(results.value, HEX);
- Serial.println(results.value&0xFFF, HEX); //取后3位有效位&0xfff
- if ((results.value&0xFFF) == 0x401){
- pos = 40;
- }
- if ((results.value&0xFFF) == 0x402){
- pos = 88;
- }
- if ((results.value&0xFFF) == 0x403){
- pos = 135;
- }
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(100); // waits 15ms for the servo to reach the position
- irrecv.resume(); // Receive the next value
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|