极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15653|回复: 3

【求大神】Arduino控制两个舵机反向转动问题

[复制链接]
发表于 2018-4-9 23:51:36 | 显示全部楼层 |阅读模式
如题,以下是我自己写的控制两个舵机转动的程序,但是这样运行的结果是两个舵机同步转动, 我想实现两个舵机反向转动,如何修改,求大神帮忙,跪谢!

#include <Servo.h>
Servo myservo1;  //Create a steering control object
Servo myservo2;  //Create a steering control object                        
int pos = 0;    // The variable is used to store the angle position of the rudder
void setup()
{
  myservo1.attach(9);  // The rudder is controlled by the Arduino ninth pin
  myservo2.attach(10);  // The rudder is controlled by the Arduino ninth pin
}

void loop()
{
  
  for(pos = 0; pos < 180; pos += 1)   // From 0 to 180 degrees
  {                                                     // Every step of step
     myservo1.write(pos);        // Steering angle of the steering gear   
     myservo2.write(pos);        //
    delay(15);                       // Wait for 15ms to get the steering gear to the designated position
  }
  for(pos = 180; pos>=1; pos -= 1)   //From 180 to 0 degrees
  {                                
     myservo1.write(pos);        // Steering angle of the steering gear   
     myservo2.write(pos);         //
    delay(15);                        //  Wait for 15ms to get the steering gear to the designated position
  }
}
  
回复

使用道具 举报

 楼主| 发表于 2018-4-10 00:20:09 | 显示全部楼层
求大神帮忙啊~!!!
回复 支持 反对

使用道具 举报

发表于 2018-4-10 06:02:16 | 显示全部楼层
for(pos = 0; pos < 180; pos += 1)   // From 0 to 180 degrees
  {                                                     // Every step of step   
     myservo2.write(pos);        //
    delay(15);                       // Wait for 15ms to get the steering gear to the designated position
  }
  for(pos = 180; pos>=1; pos -= 1)   //From 180 to 0 degrees
  {                                
     myservo1.write(pos);        // Steering angle of the steering gear             //
    delay(15);                        //  Wait for 15ms to get the steering gear to the designated position
  }
回复 支持 反对

使用道具 举报

发表于 2018-4-11 10:58:03 | 显示全部楼层
#include <Servo.h>
Servo myservo1;  //Create a steering control object
Servo myservo2;  //Create a steering control object                        
int pos = 0;    // The variable is used to store the angle position of the rudder
void setup()
{
  myservo1.attach(9);  // The rudder is controlled by the Arduino ninth pin
  myservo2.attach(10);  // The rudder is controlled by the Arduino ninth pin
  myservo1.write(0);        // Steering angle of the steering gear   
  myservo2.write(180);
}

void loop()
{
  
  for(pos = 0; pos < 180; pos += 1)   // From 0 to 180 degrees
  {                                                     // Every step of step
     myservo1.write(pos);        // Steering angle of the steering gear   
     myservo2.write(180-pos);        //
    delay(15);                       // Wait for 15ms to get the steering gear to the designated position
  }
  for(pos = 180; pos>=1; pos -= 1)   //From 180 to 0 degrees
  {                                
     myservo1.write(pos);        // Steering angle of the steering gear   
     myservo2.write(180-pos);         //
    delay(15);                        //  Wait for 15ms to get the steering gear to the designated position
  }
}
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-6-8 20:11 , Processed in 0.052685 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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