Pages: [1]   Go Down
Author Topic: Help with controlling servo with Slider potentiometer  (Read 1521 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 112
Posts: 4926
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3756
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 112
Posts: 4926
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Swapping one of the mappings around from 20 ,180 to 180, 20 should do the trick.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: