控制两个直流电动机和伺服与Arduino的code [英] controlling two dc motors and servo with arduino code

查看:413
本文介绍了控制两个直流电动机和伺服与Arduino的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code似乎没有工作,但我也刚开始学习如何code Arduino的。我试图运行两个直流电动机和一个伺服转向作为一个机器人汽车。我应该如何赖特它,所以它的工作原理。所有我想要做的是使用两个马达,以推动汽车和伺服为汽车提供指导。我怎样才能改善这种code?

 的#include< Servo.h>
    INT servoRightPin = 2;
    INT servoLeftPin = 3;
    INT servoDirPin = 4;
    伺服servoRight;
    伺服servoLeft;
    伺服servoDir;
    无效turnLeft()
    {
    servoDir.write(0.6);
    延迟(300000);
    servoLeft.write(180);
    servoRight.write(0);
    }    无效moveFor​​ward()
    {
    servoDir.write(0);
    延迟(240000);
    servoLeft.write(180);
    servoRight(0);
    }    无效turnLeft()
    {
    servoDir.write(0.6);
    延迟(300000);
    servoLeft.write(180);
    servoRight.write(0);
    }    无效moveFor​​ward()
    {
    servoDir.write(0);
    延迟(240000);
    servoLeft.write(180);
    servoRight(0);
    }
    june_4_car.ino:在函数'无效moveFor​​ward():
    june_4_car.ino:25:15:错误:不对应的呼叫'(伺服)(INT)'
    june_4_car.ino:在函数'无效turnLeft():
    june_4_car.ino:28:6:错误:重新定义无效turnLeft()
    june_4_car.ino:12:6:错误:无效turnLeft()'$ P这里pviously定义$
    june_4_car.ino:在函数'无效moveFor​​ward():
    june_4_car.ino:36:6:错误:重新定义无效moveFor​​ward()
    june_4_car.ino:20:6:错误:无效moveFor​​ward()'previously这里定义
    june_4_car.ino:41:15:错误:不对应的呼叫'(伺服)(INT)'
    错误编译。


解决方案

若干问题在那里。

让我们先从编译错误:


  1. 您有两个功能 turnLeft 和两个功能 moveFor​​ward 。我假设第二对应右拐 moveBackwards

  2. 在调用 moveFor​​ward 函数 servoRight(0)这也许应该是 servoRight .WRITE(0)

修复这应该让你的code编译,但它仍然无法工作:


  1. 您已经定义针脚,但他们没有连接到伺服器(以没有呼叫连接)。

  2. 您提到了一个伺服和两个直流电机为什么贵code有三个舵机? (只有伺服三个销中的一个连接到一个数字端口,另外两个是用于功率)。

  3. 延迟是什么写(180)写(0)什么是你想在那里做?

  4. 写(0.6)是不会由0.6度,以增加的角度。您需要为保持目前的角度曲目或阅读()伺服它

在短期看了一些教程(这样) ,实验和乐趣。

The code does not seem to be working but I have also just started learning how to code arduino. I'm trying to run two dc motors and one servo as steering for a robot car. How should I wright it so it works. All I'm trying to do is use two motors to drive the car forward and the servo to provide direction for the car. How can I improve this code?

    #include <Servo.h>


    int servoRightPin = 2;
    int servoLeftPin = 3;
    int servoDirPin = 4;
    Servo servoRight;
    Servo servoLeft;
    Servo servoDir;


    void turnLeft()
    {
    servoDir.write(0.6);
    delay(300000);
    servoLeft.write(180);
    servoRight.write(0);
    }

    void moveForward()
    {
    servoDir.write(0);
    delay(240000);
    servoLeft.write(180);
    servoRight(0);
    }

    void turnLeft()
    {
    servoDir.write(0.6);
    delay(300000);
    servoLeft.write(180);
    servoRight.write(0);
    }

    void moveForward()
    {
    servoDir.write(0);
    delay(240000);
    servoLeft.write(180);
    servoRight(0);
    }




    june_4_car.ino: In function 'void moveForward()':
    june_4_car.ino:25:15: error: no match for call to '(Servo) (int)'
    june_4_car.ino: In function 'void turnLeft()':
    june_4_car.ino:28:6: error: redefinition of 'void turnLeft()'
    june_4_car.ino:12:6: error: 'void turnLeft()' previously defined here
    june_4_car.ino: In function 'void moveForward()':
    june_4_car.ino:36:6: error: redefinition of 'void moveForward()'
    june_4_car.ino:20:6: error: 'void moveForward()' previously defined here
    june_4_car.ino:41:15: error: no match for call to '(Servo) (int)'
    Error compiling.

解决方案

Several problems there.

Let's start with the compilation errors:

  1. You have two functions turnLeft and two functions moveForward. I assume the second pair should be turnRight and moveBackwards.
  2. In the moveForward function you call servoRight(0) this should probably be servoRight.write(0).

Fixing this should allow your code to compile, but it will still not work:

  1. You have defined pins but they are not attached to servos (no call to attach).
  2. You mentioned one servo and two dc motors so why does your code have three servos? (only one of the servo three pins is connected to a digital port the other two are for power).
  3. What's with the delay, write(180) and write(0) what are you trying to do there?
  4. write(0.6) is not going to increase the angle by 0.6 degrees. You need to either keep track of the current angle or read() it from the servo.

In short read some tutorials (like this), experiment and have fun.

这篇关于控制两个直流电动机和伺服与Arduino的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆