找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2873|回复: 0
收起左侧

arduino控制单舵机(180度)2连续运动

[复制链接]
ID:312827 发表于 2018-4-21 08:28 | 显示全部楼层 |阅读模式
金工实习     控制小车

单片机源程序如下:
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial mySerial(0, 1);       // RX, TX
  3. #include <Servo.h>
  4. Servo myservo;                       // create servo object to control a servo
  5. int pos=60;  
  6. int p1=2;                       // variable to store the servo position
  7. char val='/';
  8. void setup()
  9. {
  10.     Serial.begin(9600);
  11. myservo.attach(p1);       // attaches the servo on pin 2 to the servo object                     
  12.     myservo.write(pos);       //初始化所有舵机位置,防止通电抖动。
  13.     delay(1000);
  14.     myservo.detach();

  15. }
  16. void loop()                     // run over and over
  17. {
  18.     val=Serial.read();
  19.     if(val=='1')
  20.     {  
  21.       myservo.attach(p1);       // attaches the servo on pin 2 to the servo object
  22.        for(inti=1;i<=30;i++)     // goes from 60 degrees to 30 degrees
  23.        {               
  24.          pos--;                 
  25.          myservo.write(pos);    // tell servo to go to position in variable 'pos'
  26.          delay(15);           // waits 15ms for the servo to reach the position
  27.        }
  28.        If(pos==0)pos=180;
  29.        myservo.detach();
  30.        val='11';
  31.     }
  32.     if(val=='2')
  33.    {
  34.        myservo.attach(p1);
  35.        for(pos=30;pos<=60;pos+= 1)  // goes from 30 degrees to 60 degrees
  36.        {                            // in steps of 1 degree
  37.          myservo.write(pos);       // tell servo to go to position in variable 'pos'
  38.          delay(15);                // waits 15ms for the servo to reach the position
  39.        }
  40.        myservo.detach();
  41.        val='22';
  42.    }
  43. }
复制代码

所有资料51hei提供下载:
遥控单舵机(180度)2连续运动20171227.docx (16.04 KB, 下载次数: 6)
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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