Created
July 5, 2016 03:02
-
-
Save Mango-kid/391d555d83d08b03b0da4ab51a3324d6 to your computer and use it in GitHub Desktop.
Basic motor control with the Bammer Motor driver
This file contains hidden or 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
/* | |
* This program is used to control a Bammer motor driver. This code may also | |
* be compatable with other motor drivers with direction and enable pins | |
*/ | |
// define the pins that are connected to the Bammer | |
const int motor1_direction = 2; | |
const int motor1_pwm = 9; | |
const int motor1_sleep = 4; | |
/* this function sets the speed and direction of the motor | |
* Param 1: Direction (1 = FWD, 2 = RVS) | |
* Param 2: Speed (0 - 255, where 255 is max) | |
*/ | |
void setMotor(char dir, char pwm){ | |
if (dir) digitalWrite(motor1_direction, HIGH); | |
else digitalWrite(motor1_direction, LOW); | |
analogWrite(motor1_pwm, pwm); | |
} | |
void setup(){ | |
pinMode(motor1_direction, OUTPUT); | |
pinMode(motor1_sleep, OUTPUT); | |
digitalWrite(motor1_sleep, HIGH); | |
} | |
void loop(){ | |
//slowly accelerate the motor until it is at full speed | |
for (int i = 0; i < 255; i++){ | |
setMotor(1, i); | |
delay(10); | |
} | |
//decelerate the motor quickly | |
for (int i = 255; i > 0; i--){ | |
setMotor(1, i); | |
delay(2); | |
} | |
//spin the motor in the opposite direction at a low speed | |
setMotor(0, 40); | |
//wait for a while | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment