程序很简单,如下:
int speedPin_M1 = 5; //M1 Speed Control
int speedPin_M2 = 6; //M2 Speed Control
int directionPin_M1 = 4; //M1 Direction Control
int directionPin_M2 = 7;
void setup() {
// put your setup code here, to run once:
pinMode(speedPin_M1, OUTPUT);
pinMode(speedPin_M2, OUTPUT);
pinMode(directionPin_M1, OUTPUT);
pinMode(directionPin_M2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
carBack();
delay(1000);
carStop();
delay(100);
carAdvance();
delay(1000);
//carTurnLeft(100,100);
//delay(1000);
//carTurnRight(100,100);
//delay(1000);
}
void carStop(){ // Motor Stop
digitalWrite(speedPin_M2,LOW);
digitalWrite(directionPin_M1,LOW);
digitalWrite(speedPin_M1,LOW);
digitalWrite(directionPin_M2,LOW);
}
void carTurnLeft(int leftSpeed,int rightSpeed){ //Turn Left
analogWrite (speedPin_M2,leftSpeed); //PWM Speed Control
digitalWrite(directionPin_M1,HIGH);
analogWrite (speedPin_M1,rightSpeed);
digitalWrite(directionPin_M2,LOW);
}
void carTurnRight(int leftSpeed,int rightSpeed){ //Turn Right
analogWrite (speedPin_M2,leftSpeed);
digitalWrite(directionPin_M1,LOW);
analogWrite (speedPin_M1,rightSpeed);
digitalWrite(directionPin_M2,HIGH);
}
void carBack(){ //Move backward
digitalWrite(directionPin_M1,LOW);
digitalWrite(directionPin_M2,LOW);
digitalWrite(speedPin_M1,HIGH);
digitalWrite(speedPin_M2,HIGH);
}
void carAdvance(){ //Move forward
digitalWrite(directionPin_M1,HIGH);
digitalWrite(directionPin_M2,HIGH);
digitalWrite(speedPin_M1,HIGH);
digitalWrite(speedPin_M2,HIGH);
} 复制代码
板子和电机都是共同通过Bluno Remeo板的vin口接电池盒供电(5个5号电池)。因为小车现在在家里,实物图没法上,示意图如下:
唯一不同是目前测试只接了两个电机。