Hello, I have an interesting project i am currently working on and need some help or a little advice.
So i have a standard servo that sweeps from 0 to 180 degrees. What i would like to do is have a way of changing the angles, the servo sweeps through. I'll give an example.
The servo starts sweeping from 0 to 180 degrees and those are its limits
When a button is pressed at first (say when the servo is at 50 degrees) it recognises that as its new limit
On its way back on the reverse sweep, a different button or the same button is pressed and the servo recognises that as its other new limit (say at 150 degrees)
So the servo is now rotating between 50 and 150 degrees instead of 0 and 180 degrees.
When the buttons are pressed again, it goes back to its original sweep.
I had an interesting solution but i'm thinking using the arduino might be better. What i wanted to do was attach a pot to the shaft of the servo and measure the voltage level at any point the button is pushed . The voltage levels are recognised as the new limits.
If anyone has any idea, that would be really helpful. Thanks
If you are doing a slow sweep, as in the File->Examples->Servo->Sweep example, you can just put the button check inside the loop so you have the loop variable handy to tell you where you are at.
#include <Servo.h>
const int buttonPin = 2;
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 start = 0;
int end = 180;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
for(pos = start; pos < end; 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'
if (digitalRead(buttonPin) == LOW)
if (start == 0)
start = pos;
else
start = 0; // Reset to 0
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = end; pos>=start; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
if (digitalRead(buttonPin) == LOW)
if (end == 180)
end = pos;
else
end = 180; // Reset to 180
delay(15); // waits 15ms for the servo to reach the position
}
}
I was able to try it out but theres still some problems.
What happens is the servo starts sweeping and when the button is pressed, it stops the sweep at the point where the button was pressed and starts from the beginning
I would like to mark a beginning and an end and rotate between those angles.
kelvin8:
Still not functioning properly and giving out error:
no matching function for servo::read(int&
I added the servoread() to the end of my test code below and a value is returned.
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7, 500, 2500); //the pin for the servo control, and range if desired
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
Serial.print("Last servo command position: ");
Serial.println(myservo.read());
readString=""; //empty for next input
}
}
#include <Servo.h>
const int buttonPin = 2;
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 start = 0;
int end = 180;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
for(pos = start; pos < end; 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'
if (digitalRead(buttonPin) == LOW)
if (end == 180)
end = pos;
else
end = 180; // Reset to 180
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = end; pos>=start; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
if (digitalRead(buttonPin) == LOW)
if (start == 0)
start = pos;
else
start = 0; // Reset to 0
delay(15); // waits 15ms for the servo to reach the position
}
}