Go Down

Topic: Help with controlling servo with Slider potentiometer (Read 1 time) previous topic - next topic

Hello everyone!
Im using an Arduiono Uno to control a servo with a Slider Potentiometer.
Everything seems to be hooked up properly. But, for some reason the potentiometer doesnt
work what so ever. As if its not getting a signal.
Im using the basic "knob" example sketch. But, i changed the slider from pin 0 to pin 7. Heres the code


#include <Servo.h>

Servo myservo;

int potpin = 7; 
int val;   

void setup()
{
  myservo.attach(9);   
}

void loop()
{
  val = analogRead(potpin);           
  val = map(val, 0, 1023, 0, 179);     
  myservo.write(val);                 
  delay(15);                           
}

Will a slider work with this? Or do I need to basic clockwise/counter clockwise turning potentiometer.
Either way, any help would be awesome!
Also the servo works and nothings wrong with the board! Ill upload the "blink sketch" or the "sweep" sketch and it works fine.

johnwasser

The Arduino Uno only has analog inputs 0 through 5.  There is no analog input 7.

Connect your pot to one of the six analog inputs.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

JimboZA

Moral of the story: hook things up exactly as the recipe says, before you change things for local requirements.

Wait.... disregard that comment... the instructions tell you to power the servo from the Arduino's 5V pin....don't do that...
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

wildbill

Just for a bit of extra clarity, if you found a pin labelled 7 to connect to on an uno, that was a digital pin - you need to use analog pins which are on the other side of the board.

okay lol! i got them working. Now heres another question. I coded and plugged in another servo. So the slider is controlling two servos.
How do i get the second servo to spin counter clockwise while the first one spins  clockwise. Heres what i have, I thought it was something to do with the number values. But i couldnt find any tutorials or anything on them.

val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 20, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(5);                           // waits for the servo to get there
 
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val2 = map(val2, 0, 1023, 20, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo2.write(val2);                  // sets the servo position according to the scaled value
  delay(5);

JimboZA

Swapping one of the mappings around from 20 ,180 to 180, 20 should do the trick.
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

Go Up