Skip to content
Life is short Play more
  • Computer
    • Cloud
    • DataBase
      • Mysql
      • Oracle
    • Operation system
      • Android
      • Linux
      • Windows
    • Programming
      • C , C++
      • Java
      • Javascript
      • Linux shell
      • Php
      • python
      • Web Programing
    • Security
    • Virtulization
    • Web hosting
      • Ad
  • Life Testing
  • My work
    • wrapEasyDevice
  • Network
    • HTTP
  • Note
  • Other
    • Embedded
    • Hardware
  • Share
    • Articles
    • Music
    • Tools
    • Video
  • Software Testing
    • Automation Testing
    • Cloud Testing
    • Functional Testing
    • Performance Testing
Life is short Play more
Life is short play more

arduino 红外遥控陀机

Posted On 2015年11月30日

红外控制陀机向右(+号键),向左(-号键)旋转。

长按和短按时,遥控器发出的指令不同, 所以每次判断按键时, 记录上次短按的键,如果是长按,则认为是上次短按的继续。

+号键  NEC: 16754775

-号键  NEC: 16769055

长按时均为 NEC: 4294967295

decode_type为红外接受到的resutls的decode_type , 可以忽略。是由红外发送时,此按键写死的type。

 

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

const int RECV_PIN = 7; // the ir recieve pin

IRrecv irrecv(RECV_PIN);

decode_results results;



Servo myservo;  //创建一个舵机控制对象

 int lastbutton = 0;
 
void setup() {
  // put your setup code here, to run once:
//pinMode(7,INPUT);

  Serial.begin(9600);
  if(!Serial.available())
{

  Serial.println("hi coming recieing...");
}
    irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
  myservo.attach(2); 
  Serial.println(myservo.read());
 
}

void loop() {
  // put your main code here, to run repeatedly:


 if (irrecv.decode(&results)) {
    if (results.decode_type == NEC) {
      Serial.print("NEC: ");

      if(results.value == 16754775)
      {
        lastbutton = 1;
        right();
      }

      else if(results.value == 16769055)
      {
         lastbutton = 2;
        left();
      }

      else if(results.value == 4294967295)
      {
        if(lastbutton ==1) right();
        else if(lastbutton ==2 ) left();
      }
      
    } else if (results.decode_type == SONY) {
      Serial.print("SONY: ");
    } else if (results.decode_type == RC5) {
      Serial.print("RC5: ");
    } else if (results.decode_type == RC6) {
      Serial.print("RC6: ");
    } else if (results.decode_type == UNKNOWN) {
      Serial.print("UNKNOWN: ");
    }
    //Serial.println(results.value, HEX);
    Serial.println(results.value);
    irrecv.resume(); // Receive the next value
    
  }

}


void right()
{

  int pos = myservo.read();

   Serial.println(pos);
  if(pos < 174)
   myservo.write(pos+5);
  delay(15);    
}
void left()
{

  int pos = myservo.read();
   Serial.println(pos);
  if(pos > 5)
  myservo.write(pos-5);
  delay(50);    
}

红色为电源, 棕色为地线, 黄色为控制线。 这里链接了arduino的2号pin 针角。

此篇文章已被阅读4680 次

相关文章

  1. java的stack错误没有显示行号显示unknown source
  2. 记录arduino+ble调试成功(没有完整资料的日子真痛苦)
  3. android如何单元测试handler发消息
  4. 如何用java实现swap函数?
  5. adb logcat 如何针对应用包名进行过滤日志?

Related Posts

如何修改android 创建avd的路径
multicopter资料
记录arduino+ble调试成功(没有完整资料的日子真痛苦)

About The Author

The Tester

技术交流,生活学习

Add a Comment

取消回复

邮箱地址不会被公开。 必填项已用*标注

最热文章

  • No results available

近期文章

  • vue2 动态import导入vue提示报错
  • batch udpate sql批量更新数据库中所有表的特定字段值
  • Springboot application启动missing EmbeddedServletContainerFactory错误解决
  • Dubbo使用java api 配置开发禁止使用config center
  • 使用jacoco实时生成后端服务集成测试代码覆盖率报告

近期评论

  • ghj发表在《科学上网trojan的安装和使用》
  • The Tester发表在《cloudflare Rocket Loader建议中国使用者关闭》
  • zhoulujun发表在《cloudflare Rocket Loader建议中国使用者关闭》
  • The Tester发表在《vue+iviewui menu+tabs联动及tabs切换总结》
  • xiaodong.xuexd发表在《sonarqube 7.0 新功能及安装》
  • Josephnex发表在《使用github pages作图床》

标签云

adsense ajax android aws crawler css eclipse findbugs git goagent htmlunit iptables jacoco java javascript jmeter joolma joomla junit jvm lcd linux moco mongodb monkeyrunner mysql oled openvpn oracle pdp php puppet python rabbitmq sonar sonarqube spring springboot ubuntu vpn vue wordpress wrapeasymonkey yii yum

分类目录

© 2025 Life is short Play more | blog | Powered by WordPress & Customizable Blogily