极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12735|回复: 2

红外控制舵机

[复制链接]
发表于 2015-7-21 23:19:49 | 显示全部楼层 |阅读模式
本帖最后由 [email protected] 于 2015-7-21 23:22 编辑

使用IRremote红外库控制舵机的角度,目前可以通过+-调整10度,0-9对应0-180度

  1. #include <IRremote.h>
  2. #include <Servo.h>

  3. int RECV_PIN = 11;
  4. int pos = 0;

  5. IRrecv irrecv(RECV_PIN);
  6. decode_results results;
  7. Servo myservo;

  8. void setup()
  9. {
  10.   Serial.begin(9600);
  11.   irrecv.enableIRIn(); // Start the receiver
  12.   myservo.attach(9);
  13. }

  14. void loop() {
  15.   if (irrecv.decode(&results)) {
  16.     Serial.println(results.value, HEX);
  17.     irrecv.resume(); // Receive the next value
  18.     if (results.value == 0xA3C8EDDB) {//+
  19.       pos += 10;
  20.       if (pos >= 180) {
  21.         pos = 180;
  22.       }
  23.     }
  24.     if (results.value == 0xF076C13B) {//-
  25.       pos -= 10;
  26.       if (pos <= 0) {
  27.         pos = 0;
  28.       }
  29.     }

  30.     if (results.value == 0xC101E57B) {//0
  31.       pos = 0;
  32.     }

  33.     if (results.value == 0x9716BE3F) {//1
  34.       pos = 20;
  35.     }

  36.     if (results.value == 0x3D9AE3F7) {//2
  37.       pos = 40;
  38.     }

  39.     if (results.value == 0x6182021B) {//3
  40.       pos  = 60;
  41.     }

  42.     if (results.value == 0x8C22657B) {//4
  43.       pos  = 80;
  44.     }
  45.     if (results.value == 0x488F3CBB) {//5
  46.       pos = 100;
  47.     }
  48.     if (results.value == 0x449E79F) {//6
  49.       pos = 120;
  50.     }
  51.     if (results.value == 0x32C6FDF7) {//7
  52.       pos = 140;
  53.     }
  54.     if (results.value == 0x1BC0157B) {//8
  55.       pos = 160;
  56.     }
  57.     if (results.value == 0x3EC3FC1B) {//9
  58.       pos = 180;
  59.     }
  60.     myservo.write(pos);
  61.   }
  62.   delay(100);
  63. }
复制代码

不会画图,大家都用什么软件画图的?
另外家里的屌丝空调遥控器每次按下同一个键反会的数据不一样,这是什么情况,怎么获取正确的值?大家碰到过没有?
回复

使用道具 举报

发表于 2015-7-22 09:57:43 | 显示全部楼层
推荐 tiny cad 效果还可以
回复 支持 反对

使用道具 举报

发表于 2015-9-1 23:22:13 | 显示全部楼层
LZ你好,我近期在做蓝牙小车,想用舵机控制小车的方向,可是我思考不出来一个办法用无线操作方法控制舵机,我就想比如我这边给一个指令,或者一直按着某个按键,舵机就正转或者反转,我松开,他就停止!还有能不能请求楼主把你这个实验具体用到的东西做个介绍或者列个清单我也想试试!求楼主解惑答疑!谢谢了!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 05:41 , Processed in 0.060309 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表