Controlling a dc motor direction with a Pot

Hey completely new, Arduino code completely over my head so far

Im working on a sculpture project and I am trying to control a dc motor direction with a potentiometer. I dont care about speed I just want to make it change directions by twisting the pot left or right. I have a SN7544?? h-bridge to control the direction. I have searched the forums to no avail and please be easy on me I am trying to figure this stuff out no knowing much about electronics whatsoever. If somebody has source code already that would be great.

Thanks

From what I understand from your description should be pretty simple. You hook the pot up so you can read it's current value through one of the analog pins (analogRead), then if the value is < 512 drive the motor one way by settings the motor driver pins LOW/HIGH, and if the pot value is great than 512 then drive the motor pins HIGH/LOW.

I did exactly this (well almost) on the weekend with a Wii Nunchuck and a robot I was testing. Can post you some code this evening if you like.

How will you know to stop (LOW/LOW) the motor?

Cheers,

what I wanted was to turn the knob and the motor will turn in that direction and the motor will stop if the knob is in the middle position. I actually just found a site that did exactly that with very minimal effort AND controlled the speed in which the motor turned according to the feedback from the potentiometer.

its at the bottom of this page:
http://lizarum.com/assignments/htink/#

Like a model train speed controller...

The code example on that page has a hard cutover from forward to backward - to stop the motor you'd want to leave some room in that middle section - eg 120-140 is stop, and you'd send LOW/LOW to the motor pins, or just LOW to enable.

G.

what I wanted was to turn the knob and the motor will turn in that direction and the motor will stop if the knob is in the middle position.

Depending on the size of the motor, you may can do that with the below switchs and no programming required.

http://www.radioshack.com/product/index.jsp?productId=2062536
http://www.radioshack.com/product/index.jsp?productId=2062531
http://www.radioshack.com/product/index.jsp?productId=2062530

Trying to do something similar with a joystick and hacking the AFMotor example Motor Test

I'm sure there are better ways of doing this but having some syntax issues with String substitution and getting this error

MotorTest_2.cpp: In function 'void loop()':
MotorTest_2:42: error: no matching function for call to 'AF_DCMotor::run(String&)'
C:\Documents and Settings\handyman\My Documents\downloads\arduino-0021\arduino-0021\libraries\AFMotor/AFMotor.h:84: note: candidates are: void AF_DCMotor::run(uint8_t)

I'm trying to set dir variable to RELEASE, FORWARD, or BACKWARD based on xa which is 0 at center position and -127 to +127 at the end positions.

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>
#include <math.h>

AF_DCMotor motor(4);
//AF_DCMotor motor(4);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  // turn on motor
  motor.setSpeed(200);
 
  motor.run(RELEASE);
}

void loop() {
  
   int x = analogRead(3);
  int y = analogRead(5);
  
  int xa = ((x/4)-128);    // gives you range of -127 to +127 and 0 at center stick
  int ya = ((y/4)-128);
  
  int pos1 = (128 + xa);    //  gives you range of 0 to 256 with 128 at center stick
  int pos2 = (128 + ya);
  String dir = "RELEASE";
  
  if (xa >= 5) {
        String dir = "FORWARD";
              }
    
  if (xa <= -5)  {
    String dir = "BACKWARD";
    int fabs ( xa );
                 }
    
  motor.run(dir);

    motor.setSpeed(xa);  
   delay(10);
 
  
  uint8_t i;
  
  Serial.println("tick");
  Serial.println(pos1);
  Serial.println(pos2);
  Serial.println(xa);
  Serial.println(ya);
  delay(500);
}

I'm trying to set dir variable to RELEASE, FORWARD, or BACKWARD based on xa which is 0 at center position and -127 to +127 at the end positions.

No, you're not. You're trying to set dir to "RELEASE", "FORWARD", or "BACKWARD". Not the same thing at all.

Why are you trying to use a String, when the function wants a byte?

byte dir = FORWARD;
or
byte dir = RELEASE;
or
byte dir = BACKWARD;
followed by
motor.run(dir);

I'm more hacker than programmer and new to Arduino as well. That code doesnt seem to work either. It does compile but not setting dir to anything that works.

The original code...

void loop() {
  uint8_t i;
  
  Serial.print("tick");
  
  motor.run(FORWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }

FORWARD, BACKWARD, AND RELEASE are keywords for direction and stop in the example and declared as LITERAL1

FORWARD and BACKWARD are #define'd into existence.

If you have code like:

motor.run(BACKWARD);
byte dir = FORWARD;
motor.run(dir);

The preprocessor will change that to:

motor.run(2);
byte dir = 1;
motor.run(dir);

before the compiler is invoked.

Thanks for your help... for the record I'm using the motor shield and AFMotor example from here...

http://www.ladyada.net/make/mshield/use.html

motor.run(BACKWARD);
byte dir = FORWARD;
motor.run(dir);

could you also use

int dir = FORWARD;

Before seeing your replies I got it to work by modifying code to..

  if (xa >= 10) {
        motor.run(FORWARD);
        motor.setSpeed(xa);
         delay(10);
              }

could you also use
int dir = FORWARD;

You could, but the range of values that are possible (FORWARD, RELEASE, BRAKE, and BACKWARD) fits in a byte, and a byte is what motor.run() expects as input.

Using a larger type than needed is a waste of memory.

Also, if the Arduino team ever wises up and decides that turning off all compiler warnings is not a good thing to do, the compiler WILL tell you all about the type mis-matches that are occurring.

Even though you don't hear about them, the compiler has extra work to do every time there is a type mis-match.