Help with averaging a motor position

Hi, I am messing around with re-purposing an A/C vent door actuator.
It is just a DC motor, with a metal wiper, that drags along a resistive strip to give you some positional feedback.
I tried the code below and it does what I ask, although It does not do what I want.

 if (sensorValue != 500){
     if(sensorValue <= 495) {
       digitalWrite(9, HIGH);
       digitalWrite(8,LOW);
     }
     if(sensorValue >= 500) {
       digitalWrite(9, LOW);
       digitalWrite(8,HIGH);
     }
 }

Now I realize I will have to run the DC motor off of an H-Bridge for the final project, but the DC motor is actually very low power and the Arduino is driving it just fine for testing right off of the pins.

Any way, the value of “500” is about dead center on the integral pot. The code works fine in that if I rotate the shaft position to about 900, it will run the motor the proper direction until it gets to “500”, same with the other direction. The problem is, Once it gets to “500” it hunts back and forth continuously making corrections to try and maintain that reading of “500”.
I don’t need it to be that accurate, nor do I want the DC motor constantly switching forward and backward. I just want the motor to run to the approximate position of 500 and stop. How do I change my code, so that the hunting stops, and the Arduino will turn off the motor as long as it is in range of “500”.

I think you want something more like:

     if(sensorValue <= 495) {
       digitalWrite(9, HIGH);
       digitalWrite(8,LOW);
     } else if (sensorValue >= 505) {
       digitalWrite(9, LOW);
       digitalWrite(8,HIGH);
     } else {
         digitalWrite(9, LOW);
         digitalWrite(8, LOW);
     }


Need a custom shield? Let us design and build one for you.

Thanks Rugged Circuits, that works perfectly. I can't believe I didn't try that. :-[

but the DC motor is actually very low power and the Arduino is driving it just fine for testing right

Really? I've yet to come across a DC motor capable of useful work that will safely run off an AVR's output pin. Even phone vibras typically take twice the maximum current. Have you measured the current draw?

Also have you put back EMF protection diodes on it?

That is a useful piece of code. Might work well with operating large motors with relays.

Sorry Everyone. Once Rugged Circuits fixed my code I did not come back to look at the post.

The actuator is from a 2000 or so Jaguar. So they are Ford Actuators Part#YW4H-19E616-EA. They are a dime a dozen in Bone Yards. I bought 5 for $30.00. They are very strong, because they are gear motors, and will hold a position without any power. The motors are Johnson I believe, I forget the part number but they are no longer manufactured by them. The Motor has a cap across it's pins inside the actuator housing. No I did not measure the Amps I was pulling. But I was running two motors at the same time, off an Arduino Duemilanove using the USB as a power supply.

Maybe I was just getting lucky, but nothing was getting hot or acting weird and I was messing with it for over an hour.

Being that the Arduino pins don't put out much current I'm assuming the motors were drawing very little current. My final design I am using the L293 or 298 not sure yet.

The code is simple and works great. At least for my purposes, which is changing my older Jaguar XJ6 to use electronic HVAC controls instead of the pain in the butt Vacuum electric system which is plagued with problems. Hope that helps.