极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12114|回复: 3

一个arduinof的程序问题

[复制链接]
发表于 2012-1-24 21:13:46 | 显示全部楼层 |阅读模式
  1. #include <Servo.h>

  2. Servo myservo;  // create servo object to control a servo
  3. // a maximum of eight servo objects can be created

  4. int pos = 0;    // variable to store the servo position

  5. void setup()
  6. {
  7.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  8.   pinMode(2,INPUT);
  9.   pinMode(3,INPUT);

  10. }


  11. void loop()
  12. {
  13.   int val1;
  14.   int val0;
  15.   val0=digitalRead(2);
  16.   val1=digitalRead(3);
  17.   if(val0==0)
  18.   {
  19.     for(pos = 0; pos < 160; pos += 1)  // goes from 0 degrees to 180 degrees
  20.     {                                  // in steps of 1 degree
  21.       myservo.write(pos);              // tell servo to go to position in variable 'pos'
  22.       delay(15);                       // waits 15ms for the servo to reach the position
  23.     }
  24.   }
  25.   if(val1==0)
  26.   {
  27.     for(pos =180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  28.     {                                
  29.       myservo.write(pos);              // tell servo to go to position in variable 'pos'
  30.       delay(15);                       // waits 15ms for the servo to reach the position
  31.     }
  32.   }
  33. }
复制代码

为什么无论2,3脚接vcc还是地,舵机都一直在转?
回复

使用道具 举报

发表于 2012-1-25 19:48:24 | 显示全部楼层
弱弱的问一下,为什么一个是160,一个是180啊?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-1-29 09:59:47 | 显示全部楼层
zhanggang1971 发表于 2012-1-25 19:48
弱弱的问一下,为什么一个是160,一个是180啊?

饿,这个是瞎写的
回复 支持 反对

使用道具 举报

发表于 2012-3-27 15:12:19 | 显示全部楼层
motor和arduino共地么?
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-29 19:44 , Processed in 0.049116 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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