So I'm trying to control this 10k slide pot (Slide Pot - Motorized (10k Linear Taper) - COM-10976 - SparkFun Electronics) with a Texas Instruments SN754410 H-Bridge.
I've been using the DC Motor Control Using an H-Bridge tutorial from ITP as a starting point. As you can see below, I've tidied it up a bit to my liking.
http://itp.nyu.edu/physcomp/Labs/DCMotorControl
Basically, I'm looking to control the movements (motorForward and motorReverse) using OSC commands via TouchOSC on an iPad. But what I'm here for is asking:
(1) how can I control the speed of slide pot?
(2) how can I use the motorStop function I've created? It shoots back and forth way too fast and I'd like to have an option that has it stop in the middle.
I figure I may need to switch from digitalWrite to analogWrite and use PWM in order to achieve this but I'm not quite sure the right combination of frequencies between the two logic pins on the H-Bridge, which the tutorial only describes as a combo of highs and lows.
Any help appreciated.
const int switchPin = 7; // switch input
const int motorPinA = 6; // H-bridge leg 1 (pin 2, 1A) PWM
const int motorPinB = 5; // H-bridge leg 2 (pin 7, 2A) PWM
const int enablePin = 9; // H-bridge enable pin
const int lineTrack = 5; //line track for position reading
void setup(){
Serial.begin(9600);
// set the switch pin and line track as inputs:
pinMode(switchPin, INPUT);
pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs
pinMode(motorPinA, OUTPUT);
pinMode(motorPinB, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
//Obtain position reading
int pos = analogRead(lineTrack); //read the line positioning
pos = map(pos, 0, 1023, 0, 110); //remap positioning to scale of 0-100
//For proper readings, insert 2 to A5, 3 to 5V, 1 to GND
String stringA = "Sensor value: "; //add string for easier debugging
String stringA1 = stringA + pos;
Serial.println(stringA1); //print positioning information
//Move motor forward if switch is on
if(digitalRead(switchPin) == HIGH) {
motorForward();
}
//Move motor backward if switch is off
if(digitalRead(switchPin) == LOW) {
motorBackward();
}
void motorForward(){ //potentially add int PWM_val as parameter
digitalWrite(motorPinA, LOW);
digitalWrite(motorPinB, HIGH);
}
void motorBackward(){
digitalWrite(motorPinA, HIGH);
digitalWrite(motorPinB, LOW);
}
void motorStop(){
digitalWrite(motorPinA, HIGH);
digitalWrite(motorPinB, LOW);
}