Go Down

Topic: Make a POT turn a servo in one direction (Read 49 times) previous topic - next topic


The code i have been working with will be listed below. I have been having trouble relating the pot to only turn the servo in one direction. My mission is to implement a switch that will distinguish between whether or not to spin left or right. At the moment i can not seem to get the servo to just move in the right direction. Ive tried making a statement to subtract the old and new value of the pot to help distinguish direction, but once the loop goes again, it ends at zero.

My question overall is how can I use the pot as the rate of rotation rather than the actual positioning?

 Arduino Starter Kit example
Project 5 - Servo Mood Indicator
This sketch is written to accompany Project 5 in the
Arduino Starter Kit
Created 13 September 2012
by Scott Fitzgerald

This example code is part of the public domain
// include the servo library
#include <Servo.h>
Servo myServo; // create a servo object
int const potPin = A0; // analog pin used to connect the potentiometer
int potVal; // variable to read the value from the analog pin
int angle; // variable to hold the angle for the servo motor
void setup() {
 myServo.attach(9); // attaches the servo on pin 9 to the servo object
 Serial.begin(9600); // open a serial connection to your computer
void loop() {
 potVal = analogRead(potPin); // read the value of the potentiometer
 // print out the value to the serial monitor
 Serial.print("potVal: ");
 // scale the numbers from the pot
 angle = map(potVal, 0, 1023, 0, 179);
 // print out the angle for the servo motor
 Serial.print(", angle: ");
 // set the servo position
 // wait for the servo to get there


If you have a hobby servo it won't be long before your servo hits a mechanical stop.

If you have a modified "continuous rotation" 'servo' then think of it like a gearmotor with a speed control.  When you write a value near the midpoint, like myServo.write(90) or myServo.writeMicroseconds(1500), you should be able to find a value or range of values for which the motor stops.  Higher values (like 100 or 1800 microseconds) will rotate the motor in one direction.  Lower values (like 75 or 1200 microseconds) will rotate the motor in the other direction.

Start by mapping the pot input to a range like 0-180.  Pass that value to .write() to control the speed and direction.  If you only want one direction, map to a smaller range that keeps the pot from using the unwanted end of the range.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp



Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What values are you getting on your monitor when you turn the pot from 0 to 1023?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131