极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16584|回复: 5

=w=新手上路,关于VEX+Arduino做道闸的问题

[复制链接]
发表于 2015-5-22 18:06:57 | 显示全部楼层 |阅读模式
代码在这里~只用了VEX的那个直流电机、两个限位开关和两个FC-04的超声波……
  1. #include <Servo.h>
  2. #define MOTOR_PIN 7 // define the PWM range for the VEX speed controll
  3. #define MIN_POS 1000 // 0
  4. #define MAX_POS 2000 // 255
  5. #define CENTER_POS 1500 // 127
  6. Servo myservo;
  7. const int TrigPin = 2;
  8. const int EchoPin = 3;
  9. const int SecondTPin = 4;
  10. const int SecondEPin = 5;
  11. const int bumperup = 8;
  12. const int bumperdown = 9;
  13. bool motor;
  14. int uval,dval;
  15. float cm1,cm2;
  16. void setup()
  17. {
  18. Serial.begin(9600);
  19. pinMode(TrigPin, OUTPUT);
  20. pinMode(EchoPin, INPUT);
  21. pinMode(SecondTPin, OUTPUT);
  22. pinMode(SecondEPin, INPUT);
  23. pinMode(7, OUTPUT);
  24. pinMode(8, OUTPUT);
  25. pinMode(bumperup,INPUT);
  26. digitalWrite(bumperup,HIGH);
  27. pinMode(bumperdown,INPUT);
  28. digitalWrite(bumperdown,HIGH);
  29. myservo.attach(MOTOR_PIN);
  30.     motor = false;
  31. }

  32. void loop()
  33. {
  34.    myservo.attach(MOTOR_PIN);
  35.     digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
  36.     delayMicroseconds(2);
  37.     digitalWrite(TrigPin, HIGH);
  38.     delayMicroseconds(10);
  39.     digitalWrite(TrigPin, LOW);
  40.     cm1 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
  41.     cm1 = (int(cm1 * 100.0)) / 100.0; //保留两位小数
  42.          delay(100);
  43.     digitalWrite(SecondTPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
  44.     delayMicroseconds(2);
  45.     digitalWrite(SecondTPin, HIGH);
  46.     delayMicroseconds(10);
  47.     digitalWrite(SecondTPin, LOW);
  48.     cm2 = pulseIn(SecondEPin, HIGH) / 58.0; //将回波时间换算成cm
  49.     cm2 = (int(cm2 * 100.0)) / 100.0; //保留两位小数
  50. delay(100);
  51.     uval = digitalRead(bumperup);


  52.     Serial.print(cm1);
  53.     Serial.print("cm1");
  54.     Serial.println();
  55.     Serial.print(cm2);
  56.     Serial.print("cm2");
  57.     Serial.println();
  58.     uval = digitalRead(bumperup);
  59.     dval = digitalRead(bumperdown);
  60.     delay(10);
  61.     if(0<cm1<=30)
  62.     {
  63.      uval = digitalRead(bumperup);
  64.      while(uval == HIGH && motor == false)
  65.      {
  66.         myservo.writeMicroseconds(MIN_POS);
  67.         uval = digitalRead(bumperup);
  68.      }
  69.      myservo.writeMicroseconds(CENTER_POS);
  70.      motor = true;
  71.     }
  72.      delay(10);
  73.     dval = digitalRead(bumperdown);
  74.     myservo.writeMicroseconds(CENTER_POS);   
  75. if(cm1>=30 && cm2>=30)
  76.   {
  77.     while(dval == HIGH && motor == true)
  78.     {
  79.         myservo.writeMicroseconds(MAX_POS);
  80.         dval = digitalRead(bumperdown);
  81.     }
  82.     myservo.writeMicroseconds(CENTER_POS);
  83.     motor = false;
  84.   }
  85. }
复制代码


然而症状是……无论超声波的值是虾米,道闸只会一直向上和向下……求救QAQ
回复

使用道具 举报

发表于 2015-5-22 23:26:48 | 显示全部楼层
道闸一般选择伺服电机,两个终端之前要减速,提供阻尼,否则那根杆很容易折断
回复 支持 反对

使用道具 举报

发表于 2015-5-22 23:28:09 | 显示全部楼层
if语句写的不对。。  要A>100 || A<50  (且),这种写法  明显的语法错误
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-22 23:45:45 | 显示全部楼层
迷你强 发表于 2015-5-22 23:26
道闸一般选择伺服电机,两个终端之前要减速,提供阻尼,否则那根杆很容易折断

已经配上差速器了~但是我的两个超声波是装在道闸前端和后端的……这样的写法也不对吗?
回复 支持 反对

使用道具 举报

发表于 2015-5-23 22:26:18 | 显示全部楼层
  if(0<cm1<=30) 这个就是错误语句
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-6-1 20:17:34 | 显示全部楼层
迷你强 发表于 2015-5-23 22:26
if(0

感谢大神!这问题搞了一星期没明白……
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 15:11 , Processed in 0.066838 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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