控制两个直流电动机和伺服与Arduino的code [英] controlling two dc motors and servo with 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);
} 无效moveForward()
{
servoDir.write(0);
延迟(240000);
servoLeft.write(180);
servoRight(0);
} 无效turnLeft()
{
servoDir.write(0.6);
延迟(300000);
servoLeft.write(180);
servoRight.write(0);
} 无效moveForward()
{
servoDir.write(0);
延迟(240000);
servoLeft.write(180);
servoRight(0);
}
june_4_car.ino:在函数'无效moveForward():
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:在函数'无效moveForward():
june_4_car.ino:36:6:错误:重新定义无效moveForward()
june_4_car.ino:20:6:错误:无效moveForward()'previously这里定义
june_4_car.ino:41:15:错误:不对应的呼叫'(伺服)(INT)'
错误编译。
若干问题在那里。
让我们先从编译错误:
- 您有两个功能
turnLeft
和两个功能moveForward
。我假设第二对应右拐
和moveBackwards
。 - 在调用
moveForward
函数servoRight(0)
这也许应该是servoRight .WRITE(0)
。
修复这应该让你的code编译,但它仍然无法工作:
- 您已经定义针脚,但他们没有连接到伺服器(以
没有呼叫连接
)。 - 您提到了一个伺服和两个直流电机为什么贵code有三个舵机? (只有伺服三个销中的一个连接到一个数字端口,另外两个是用于功率)。
- 与
延迟是什么
,写(180)
和写(0)
什么是你想在那里做? - 写(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:
- You have two functions
turnLeft
and two functionsmoveForward
. I assume the second pair should beturnRight
andmoveBackwards
. - In the
moveForward
function you callservoRight(0)
this should probably beservoRight.write(0)
.
Fixing this should allow your code to compile, but it will still not work:
- You have defined pins but they are not attached to servos (no call to
attach
). - 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).
- What's with the
delay
,write(180)
andwrite(0)
what are you trying to do there? - 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 theservo
.
In short read some tutorials (like this), experiment and have fun.
这篇关于控制两个直流电动机和伺服与Arduino的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!