Skip to content

Instantly share code, notes, and snippets.

@wouterds
Last active December 24, 2015 04:09
Show Gist options
  • Save wouterds/6741840 to your computer and use it in GitHub Desktop.
Save wouterds/6741840 to your computer and use it in GitHub Desktop.
Arduino test with L298 H-Bridge motor driver
/*
* Author: WouterDS
* Project: Motor test
* Date: 28 september 2013
**/
// Pins
int motor_A_ENABLE = 2;
int motor_A_POWERC = 3;
int motor_A_POWERCC = 5;
int motor_B_ENABLE = 4;
int motor_B_POWERC = 6;
int motor_B_POWERCC = 9;
// Setup
void setup() {
pinMode(motor_A_ENABLE, OUTPUT);
pinMode(motor_A_POWERC, OUTPUT);
pinMode(motor_A_POWERCC, OUTPUT);
pinMode(motor_B_ENABLE, OUTPUT);
pinMode(motor_B_POWERC, OUTPUT);
pinMode(motor_B_POWERCC, OUTPUT);
}
// Main loop
void loop() {
delay(10000);
forward(2000);
delay(2000);
turnRight(2000);
delay(2000);
turnLeft(2000);
delay(2000);
backwards(2000);
}
void motorAOn() {
digitalWrite(motor_A_ENABLE, HIGH);
}
void motorBOn() {
digitalWrite(motor_B_ENABLE, HIGH);
}
void motorAOff() {
digitalWrite(motor_A_ENABLE, LOW);
}
void motorBOff() {
digitalWrite(motor_B_ENABLE, LOW);
}
void motorsOn() {
motorAOn();
motorBOn();
}
void motorsOff() {
motorsBrake();
motorAOff();
motorBOff();
}
void motorsBrake() {
motorABrake();
motorBBrake();
}
void motorABrake() {
digitalWrite(motor_A_POWERC, LOW);
digitalWrite(motor_A_POWERCC, LOW);
}
void motorBBrake() {
digitalWrite(motor_B_POWERC, LOW);
digitalWrite(motor_B_POWERCC, LOW);
}
void motorAForward() {
digitalWrite(motor_A_POWERCC, HIGH);
}
void motorBForward() {
digitalWrite(motor_B_POWERC, HIGH);
}
void motorABackwards() {
digitalWrite(motor_A_POWERC, HIGH);
}
void motorBBackwards() {
digitalWrite(motor_B_POWERCC, HIGH);
}
void forward(int time) {
motorsOn();
motorAForward();
motorBForward();
delay(time);
motorsOff();
}
void backwards(int time) {
motorsOn();
motorABackwards();
motorBBackwards();
delay(time);
motorsOff();
}
void turnRight(int time) {
motorsOn();
motorAForward();
motorBBackwards();
delay(time);
motorsOff();
}
void turnLeft(int time) {
motorsOn();
motorBForward();
motorABackwards();
delay(time);
motorsOff();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment