讓兩顆馬達不斷前進、後退、右轉和左轉,每個方向各跑一秒鐘
// OTCarTest
// A simple test program for OTCar, will make the OTCar move forward, backward,
// turn right and turn left for specific seconds.
// http://www.osslab.com.tw/Hardware/Open_Embedded_System/MCU/AVR/Arduino/Remote_Car
/* Input for motorA:
IN1 IN2 Action
LOW LOW Motor Stop
HIGH LOW Motor moves forward
LOW HIGH Motor moves backward
HIGH HIGH Motor Stop
*/
const int motorIn1 = 5;
const int motorIn2 = 6;
const int motorIn3 = 10; // 這塊 L298N module IN3 和 IN4 電路好像顛倒了
const int motorIn4 = 9; // 所以程式把 IN3, IN4 腳位對調
const int DELAY = 1000;
void setup()
{
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
}
void loop()
{
selfTest();
}
void selfTest()
{
forward();
delay(DELAY);
motorstop(); delay(500);
backward();
delay(DELAY);
motorstop(); delay(500);
right();
delay(DELAY);
motorstop(); delay(500);
left();
delay(DELAY);
motorstop(); delay(500);
}
void motorstop()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}
void forward()
{
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}
void backward()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
}
// Let right motor keep running, but stop left motor
void right()
{
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}
// Let left motor keep running, but stop right motor
void left()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}
Images 0 | ||
---|---|---|
No images to display in the gallery. |