]//////////////////////L298N驱动////////////////////////////
#define motor1pin1 10 //定义IN1引脚
#define motor1pin2 9 //定义IN2引脚
#define motor1pwm 5 //定义ENA引脚
#define motor2pin1 8 //定义IN3引脚
#define motor2pin2 7 //定义IN4引脚
#define motor2pwm 6 //定义ENB引脚
void motospd( int sp1,int sp2,int de ) //电机速度控制函数。括号内分别为左右电机速度值,
{ //范围-255~+255,正值为正转,负值为反转。
if(sp1>0)
{
digitalWrite(motor1pin2,LOW);
digitalWrite(motor1pin1,HIGH);
}
else
{
digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,HIGH);
}
if(sp2>0)
{
digitalWrite(motor2pin2,LOW);
digitalWrite(motor2pin1,HIGH);
}
else
{
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,HIGH);
}
analogWrite(motor1pwm,abs (sp1));
analogWrite(motor2pwm,abs (sp2));
delay(de);
}