Created
February 28, 2016 10:20
-
-
Save smching/5d0214888187501b3b86 to your computer and use it in GitHub Desktop.
L298N Motor Driver Controller Board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define FORWARD 8 | |
#define BACKWARD 2 | |
#define STOP 5 | |
#define PWM_PIN_1 9 //speed control pin for motor1 | |
#define PWM_PIN_2 10 ///speed control pin for motor2 | |
int Motor[2][2] = //two dimensional array | |
{ | |
{4 , 5}, //input pin to control Motor1--> Motor[0][0]=4, Motor[0][1]=5 | |
{6 , 7}, //input pin to control Motor2--> Motor[1][0]=6, Motor[1][1]=7 | |
}; | |
void setup() { | |
pinMode(PWM_PIN_1, OUTPUT); | |
pinMode(PWM_PIN_2, OUTPUT); | |
pinMode(Motor[0][0], OUTPUT); | |
pinMode(Motor[0][1], OUTPUT); | |
pinMode(Motor[1][0], OUTPUT); | |
pinMode(Motor[1][1], OUTPUT); | |
motor_run(0, FORWARD); //motor1 forward | |
motor_run(1, BACKWARD); //motor2 backward | |
} | |
void loop() { | |
//decelerate | |
for (int motor_speed=255; motor_speed>=0; motor_speed--) { | |
analogWrite(PWM_PIN_1, motor_speed); | |
analogWrite(PWM_PIN_2, motor_speed); | |
delay(20); //delay 20 milliseconds | |
} | |
//accelerate | |
for (int motor_speed=0; motor_speed<=255; motor_speed++) { | |
analogWrite(PWM_PIN_1, motor_speed); | |
analogWrite(PWM_PIN_2, motor_speed); | |
delay(20); //delay 20 milliseconds | |
} | |
} | |
void motor_run(int motor, int movement) { | |
switch (movement) { | |
case FORWARD: | |
digitalWrite(Motor[motor][0], HIGH); | |
digitalWrite(Motor[motor][1], LOW); | |
break; | |
case BACKWARD: | |
digitalWrite(Motor[motor][0], LOW); | |
digitalWrite(Motor[motor][1], HIGH);; | |
break; | |
case STOP: | |
digitalWrite(Motor[motor][0], LOW); | |
digitalWrite(Motor[motor][1], LOW);; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment