測試L298N
#define MotorA_in1 8 //定義 馬達A in 1
#define MotorA_in2 9 //定義 馬達A in 2
#define MotorB_in1 10 //定義 馬達B in 1 to L298N IN3
#define MotorB_in2 11 //定義 馬達B in 2 to L298N IN4
#define MotorA_PWNA 5 //定義 ENA (PWM) 接腳
#define MotorB_PWNB 6 //定義 ENB (PWM) 接腳
void setup()
{
pinMode(MotorA_in1,OUTPUT);
pinMode(MotorA_in2,OUTPUT);
pinMode(MotorB_in1,OUTPUT);
pinMode(MotorB_in2,OUTPUT);
pinMode(MotorA_PWNA,OUTPUT);
pinMode(MotorB_PWNB,OUTPUT);
}
void loop()
{
// 直走
analogWrite(MotorA_PWNA,200); //設定直流馬達轉速
analogWrite(MotorB_PWNB,200);
digitalWrite(MotorA_in1,HIGH); //直流馬達(右)順時針轉動
digitalWrite(MotorA_in2,LOW);
digitalWrite(MotorB_in1,HIGH); //直流馬達(左)逆時針轉動
digitalWrite(MotorB_in2,LOW);
delay(2000);
//後退
analogWrite(MotorA_PWNA,200); //設定直流馬達轉速
analogWrite(MotorB_PWNB,200);
digitalWrite(MotorA_in1,LOW); //直流馬達(右)逆時針轉動
digitalWrite(MotorA_in2,HIGH);
digitalWrite(MotorB_in1,LOW); //直流馬達(左)順時針轉動
digitalWrite(MotorB_in2,HIGH);
delay(2000);
//左轉
analogWrite(MotorA_PWNA,80); //設定直流馬達轉速
analogWrite(MotorB_PWNB,80);
digitalWrite(MotorA_in1,HIGH); //直流馬達(右)順時針轉動
digitalWrite(MotorA_in2,LOW);
digitalWrite(MotorB_in1,LOW); //直流馬達(左)順時針轉動
digitalWrite(MotorB_in2,HIGH);
delay(2000);
//右轉
analogWrite(MotorA_PWNA,80); //設定馬達轉速
analogWrite(MotorB_PWNB,80);
digitalWrite(MotorA_in1,LOW); //馬達(右)逆時針轉動
digitalWrite(MotorA_in2,HIGH);
digitalWrite(MotorB_in1,HIGH); //馬達(左)逆時針轉動
digitalWrite(MotorB_in2,LOW);
delay(2000);
//停止
digitalWrite(MotorA_in1,HIGH); //馬達(右)停止轉動
digitalWrite(MotorA_in2,HIGH);
digitalWrite(MotorB_in1,HIGH); //馬達(左)停止轉動
digitalWrite(MotorB_in2,HIGH);
delay(2000);
}
沒有留言:
張貼留言