|
用一块L298N,控制两个直流电机,正转反转什么的都能实现,现在的问题是,用PWM来控制速度,如果两个电机的速度一样,那么一切正常,如果两个电机的PWM值不同,则两个电机都不动。而如果我给其中一个电机PWM为0,另外一个就可以正常调速。就不能让他们转速不同吗?
代码如下:
[pre]int MOTOR1EN = 8;
int MOTOR2EN = 9;
int MOTOR1IN1 = 22;
int MOTOR1IN2 = 23;
int MOTOR2IN1 = 24;
int MOTOR2IN2 = 25;
void setup()
{
Serial.begin(9600);
pinMode(MOTOR1EN,OUTPUT);
pinMode(MOTOR1IN1,OUTPUT);
pinMode(MOTOR1IN2,OUTPUT);
pinMode(MOTOR2EN,OUTPUT);
pinMode(MOTOR2IN1,OUTPUT);
pinMode(MOTOR2IN2,OUTPUT);
}
void loop()
{
analogWrite(MOTOR1EN, 150);
delay(50);
analogWrite(MOTOR2EN, 100);
delay(50);
digitalWrite(MOTOR1IN1,LOW);
digitalWrite(MOTOR1IN2,HIGH);
digitalWrite(MOTOR2IN1,LOW);
digitalWrite(MOTOR2IN2,HIGH);
delay(3000);
digitalWrite(MOTOR1IN1,LOW);
digitalWrite(MOTOR1IN2,LOW);
digitalWrite(MOTOR2IN1,LOW);
digitalWrite(MOTOR2IN2,LOW);
delay(500);
digitalWrite(MOTOR1IN1,HIGH);
digitalWrite(MOTOR1IN2,LOW);
digitalWrite(MOTOR2IN1,HIGH);
digitalWrite(MOTOR2IN2,LOW);
delay(3000);
digitalWrite(MOTOR1IN1,LOW);
digitalWrite(MOTOR1IN2,LOW);
digitalWrite(MOTOR2IN1,LOW);
digitalWrite(MOTOR2IN2,LOW);
delay(500);
}[/pre]
现在的100和150就不动,如果都换成同样的就能转,这是为什么呢? |
|