用arduino代码控制两个直流电机和伺服 [英] controlling two dc motors and servo with arduino code

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

问题描述

代码似乎不起作用,但我也刚刚开始学习如何编写 arduino.我正在尝试运行两个直流电机和一个伺服作为机器人汽车的转向.我应该如何编写它以使其有效.我想要做的就是使用两个电机驱动汽车前进,并使用伺服为汽车提供方向.如何改进此代码?

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.

推荐答案

有几个问题.

让我们从编译错误开始:

Let's start with the compilation errors:

  1. 您有两个函数 turnLeft 和两个函数 moveForward.我假设第二对应该是 turnRightmoveBackwards.
  2. moveForward 函数中,您调用 servoRight(0) 这可能应该是 servoRight.write(0).
  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. 您已经定义了引脚,但它们未连接到伺服系统(无需调用 attach).
  2. 您提到了一个伺服电机和两个直流电机,那么为什么您的代码有三个伺服电机?(舵机三个引脚中只有一个连接到数字端口,另外两个用于电源).
  3. delaywrite(180)write(0) 是怎么回事?你想在那里做什么?
  4. write(0.6) 不会将角度增加 0.6 度.您需要跟踪当前角度或从 servoread() 它.
  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代码控制两个直流电机和伺服的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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