Skip to content

Instantly share code, notes, and snippets.

@sachleen
Created May 16, 2014 00:17
Show Gist options
  • Save sachleen/762b2129658a8db929fa to your computer and use it in GitHub Desktop.
Save sachleen/762b2129658a8db929fa to your computer and use it in GitHub Desktop.
arduino motor examples for SN754410
First get the value of the pot.
http://www.arduino.cc/en/Tutorial/Potentiometer
int potValue = value of pot... current position from 0 to 1023
======================================================================
Set the direction based on pot value. If it's one way, it'll spin CW, if its another, it'll spin CCW
Based on example from http://web.ics.purdue.edu/~fwinkler/616/sn754410_Arduino.pdf
if (potValue > 512) {
digitalWrite(motor1APin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2APin, HIGH); // set leg 2 of the H-bridge high
} else {
digitalWrite(motor1APin, HIGH); // set leg 1 of the H-bridge low
digitalWrite(motor2APin, LOW); // set leg 2 of the H-bridge high
}
speed_value_motor1 = 127; // half speed
analogWrite(speedPin, speed_value_motor1);
======================================================================
Set the speed based on pot value.
// spin one way
digitalWrite(motor1APin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2APin, HIGH); // set leg 2 of the H-bridge high
// use the map() function to convert a 0-1023 number to a 0-255 number
// http://arduino.cc/en/reference/map
// map(value, fromLow, fromHigh, toLow, toHigh)
speed_value_motor1 = map(potValue, 0, 1023, 0, 255);
analogWrite(speedPin, speed_value_motor1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment