I am using servos in my science fair project this year an I found this:
I was wondering if it was possible to use a potentiometer to control the servo as well as the application. I am less that relatively new at this stuff and tried and failed numerous times to make this work. Any help is appreciated. Thanks!
As have not explained how that project works I can't say if you could use a potentiometer as an alternative but the answer probably - or maybe just maybe.
Post your working code and describe in more detail what changes you want to make.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int motor = 0;
void setup()
{
Serial.begin(9600); // initialize serial:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.print("Arduino control Servo Motor Connected OK");
Serial.print('\n');
}
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
motor = Serial.parseInt();
// do it again:
pos = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '\n') {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
// print the three numbers in one string as hexadecimal:
Serial.print("Data Response : ");
Serial.print(motor, DEC);
Serial.print(pos, DEC);
}
}
}
//for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
//{ // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
//{
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 179); // 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(15);
This is an example in the Servo library that comes with the IDE;
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // 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(15); // waits for the servo to get there
}
FYI,
Try using the CODE button when posting your code....
I put in the pot servo control but it still seems not to work.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int motor = 0;
int potpin = 0;
int val;
void setup()
{
Serial.begin(9600); // initialize serial:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.print("Arduino control Servo Motor Connected OK");
Serial.print('\n');
}
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
motor = Serial.parseInt();
// do it again:
pos = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '\n') {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
// print the three numbers in one string as hexadecimal:
Serial.print("Data Response : ");
Serial.print(motor, HEX);
Serial.print(pos, HEX);
}
}
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(3000);
}
//for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
//{ // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
//{
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 179); // 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(15);
yes. I have a bluetooth module hooked up to Tx, Rx, 3.3v and GND(wich has worked with the servo without the pot), a servo hooked up to 5v, ground and pin9 and I have the pot hooked up to analog 0, 5v, and ground.
The pot is working my problem now is that I'm not quite sure how to use the serial input alongside the pot input. I can control the servo with the pot and the serial separately, but not together.
531
532
1023
541
238
0
72
1023
1023
870
722
635
568
522
191
0
185
121
376
383
382
382
695
694
695
695
695
Moving just fine. Servo works with the pot. When I insert the pot code into the original servo BT control code I think the problem is that the analog updates even if it hasn't moved.
0
710
1023
842
639
565
919
0
0
374
559
824
1023
When I was using BT and the pot it would go to one sporadically and then it would jitter and shake and sometimes it would stay but it was never when I actually intended it to, even when I turned up the delay.
OK
well this attempts to smooth out the potentiometer reading and also only updates the servo if the potentiometer gets moved.
I think this may have been an issue that you had (ie, potentiometer would over rule the blutooth even when the pot ISN'T moved)
I've just chucked this together so I daresay it isn't the complete solution.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int motor = 0;
int potpin = A0;
int val;
void setup()
{
Serial.begin(9600); // initialize serial:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.print("Arduino control Servo Motor Connected OK");
Serial.print('\n');
}
int oldPotval=90;
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
motor = Serial.parseInt();
// do it again:
pos = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '\n') {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
// print the three numbers in one string as hexadecimal:
Serial.print("Data Response : ");
Serial.print(motor, HEX);
Serial.print(pos, HEX);
}
}
int val = getPotval();
if (abs(val-oldPotval)>1)
{myservo.write(val);
oldPotval=val;
}
}
//for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
//{ // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
//{
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
//}
//val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//val = map(val, 0, 1023, 0, 179); // 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(15);
int getPotval()
{
int potVal=0;
//take the sum of 32 readings
for(int n=0;n<32;n++)
potVal=potVal+analogRead(potpin);
//get their average
potVal=potVal/32;
potVal = map(potVal, 0, 1023, 0, 179);
return potVal;
}
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
motor = Serial.parseInt();
// do it again:
pos = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '\n') {
That is a very risky way to collect data from the PC (and I know it is not @KenF's code). What's going to happen if only one item is sent, or if there is no \n. There is a better way. See the Arduino code in this Demo, for example.
Also, and maybe more importantly, how is the Arduino to know whether it should act on the data from the PC or on the value from the potentiometer?
Hi, how are you powering the servo, not with 5V from the arduino I hope.
The current needed to power even the smallest servo is beyond the current supply capacity of the 5V from the arduino.