Arduino编码来控制电机
Aurdino coding to control a motor
//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
如果我们在串行监视器中输入 a,电机应该朝一个方向旋转,如果我们输入 s,电机应该朝另一个方向旋转,但它没有发生,电机处于空闲状态,但我得到这样的输出:
a
one way
s
other way
硬件连接没有问题
能不能提前帮我this.Thanks
逻辑上有小错误:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);
根据您代码中的注释,您正在将 arduino-pin-6
连接到 l293-pin-6
,将 arduino-pin-9
连接到 l293-pin-3
。
根据thisdatasheet,L293的控制引脚为:2
、7
、10
、15
。所以,我相信你把它连接错了。脉冲也以错误的顺序完成(从 HIGH
到 LOW
等等)。
这应该是正确的代码(请查看代码中的注释):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
请注意,我添加了缺少的 motorenablepin
。它必须连接到 l293-pin-1
.
此外,由于 LOW
和 LOW
状态对于代码是通用的,您可以像我一样简化它。
//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
如果我们在串行监视器中输入 a,电机应该朝一个方向旋转,如果我们输入 s,电机应该朝另一个方向旋转,但它没有发生,电机处于空闲状态,但我得到这样的输出:
a
one way
s
other way
硬件连接没有问题
能不能提前帮我this.Thanks
逻辑上有小错误:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);
根据您代码中的注释,您正在将 arduino-pin-6
连接到 l293-pin-6
,将 arduino-pin-9
连接到 l293-pin-3
。
根据thisdatasheet,L293的控制引脚为:2
、7
、10
、15
。所以,我相信你把它连接错了。脉冲也以错误的顺序完成(从 HIGH
到 LOW
等等)。
这应该是正确的代码(请查看代码中的注释):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
请注意,我添加了缺少的 motorenablepin
。它必须连接到 l293-pin-1
.
此外,由于 LOW
和 LOW
状态对于代码是通用的,您可以像我一样简化它。