I am testing my continuous rotation servo, and I am not quite sure what is going on.
Not only is the servo not stopping when I write it at 0, but it also does not go from stop to full speed like I need it to.
When I program it with just setting it at 90 (full stop) it turns at a slow speed instead.
#include <Servo.h>
Servo servo;
const int button = 2;
int buttonstate = 0;
int fsr = A0;
int fsrv;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo.attach(9);
pinMode(button, INPUT);
}
void loop() {
fsrv = analogRead(fsr);
buttonstate = digitalRead(button);
Serial.print(fsrv );
Serial.print(" ");
fsrv = analogRead(fsr);
servo.write(90);
delay(1000);
servo.write(0);
}
For a servo modified for continuous rotation the key values are
0 = max speed in one direction
180 = max speed in opposite direction
90= stop
You may have to fiddle with the 90 value a few counts +/- to obtain true stopped condition due to slight calibration variation for your specific servo. Also note that the speed variation will not be truly linear from 0-89 in one direction and 91-180 in the other, most of the usable speed variation will be closer to the center (90) value.
Servo test code you can try with your servo. writeMicroseconds usually provides the best level of control for continuous rotation servos. If you still have a pot in the servo, you might send the servo a 90 deg or 1500us command, then tweek the pot until the servo stopps rotating. Also, the myservo.detach(); function might be used to stop the servo rotation if the servo continues to drift.
// 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);
}
readString=""; //empty for next input
}
}
retrolefty:
For a servo modified for continuous rotation the key values are
0 = max speed in one direction
180 = max speed in opposite direction
90= stop
You may have to fiddle with the 90 value a few counts +/- to obtain true stopped condition due to slight calibration variation for your specific servo. Also note that the speed variation will not be truly linear from 0-89 in one direction and 91-180 in the other, most of the usable speed variation will be closer to the center (90) value.
Lefty
I was able to calibrate my servo, but I am still trying to figure out something. How can I make it rotate for a specific amount of time, and then stop? If it set it at a speed, put in a delay, and them set it to stop, it just seems to keep rotating.
retrolefty:
For a servo modified for continuous rotation the key values are
0 = max speed in one direction
180 = max speed in opposite direction
90= stop
You may have to fiddle with the 90 value a few counts +/- to obtain true stopped condition due to slight calibration variation for your specific servo. Also note that the speed variation will not be truly linear from 0-89 in one direction and 91-180 in the other, most of the usable speed variation will be closer to the center (90) value.
Lefty
I was able to calibrate my servo, but I am still trying to figure out something. How can I make it rotate for a specific amount of time, and then stop? If it set it at a speed, put in a delay, and them set it to stop, it just seems to keep rotating.
That sequence should work. Perhaps if you posted your code we might see something your missing or have wrong.
I was able to calibrate my servo, but I am still trying to figure out something. How can I make it rotate for a specific amount of time, and then stop? If it set it at a speed, put in a delay, and them set it to stop, it just seems to keep rotating.
For testing purposes, put the code that achieves this at the end of setup and comment out (or remove) everything in loop, so it only runs once.
I was able to calibrate my servo, but I am still trying to figure out something. How can I make it rotate for a specific amount of time, and then stop? If it set it at a speed, put in a delay, and them set it to stop, it just seems to keep rotating.
For testing purposes, put the code that achieves this at the end of setup and comment out (or remove) everything in loop, so it only runs once.
servo.writeMicroseconds(1500); //servo begins at full stop
delay(2000);
servo.write(0);//servo turns at full rotation\
delay(2000);
This does everything it needs to, but then:
servo.writeMicroseconds(1500); //servo begins at full stop
delay(2000);
servo.write(0);//servo turns at full rotation\
delay(2000);
servo.writeMicroseconds(1500);
This does absolutely nothing. It seems to skip over the rest of the code and goes straight to the final position, which is stopped.
Some servo flip-flop type test code that uses delays and servo detach/attach. I haven't tested with a servo yet.
//zoomkat servo flip-flop test code 6/14/13
//view output in serial monitor
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int a = 1400; //microsecond values
int b = 1500;
int c = 1600;
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(b); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test"); // so I can keep track of what is loaded
Serial.println();
}
void loop() {
myservo.writeMicroseconds(a);
Serial.println(a);
delay(5000);
//myservo.writeMicroseconds(b);
//Serial.println(b);
//delay(2000);
myservo.detach(); //alternative stop method
Serial.println("servo detached");
delay(2000);
myservo.attach(7);
Serial.println("servo attached");
myservo.writeMicroseconds(c);
Serial.println(c);
delay(5000);
myservo.writeMicroseconds(b);
Serial.println(b);
delay(2000);
//myservo.detach(); //alternative stop method
//Serial.println("servo detached");
//delay(2000);
//myservo.attach(7);
//Serial.println("servo attached");
Serial.println("done");
Serial.println();
}
Okay, so I got the code with the arduino working exactly the way I want, but now when it's on the attiny85 chip, the servo only goes in one direction, and it doesn't work quite like I need it to. I'm not sure if I need to up the power to more than a AA's, use an external clock, or what.
What it needs to do:
When force sensor is pressed, the servo needs to spin for two seconds and stop.
When the button is pressed, the servo needs to spin for two seconds in the opposite direction.
What it's doing:
The servo only spins when the force sensor is being pressed.
The servo spins in the same direction only when the button is pressed.
Any ideas?
#include "Servo8Bit.h"
//zoomkat servo flip-flop test code 6/14/13
//view output in serial monitor
Servo8Bit myservo; // create servo object to control a servo
int a = 1400; //microsecond values
int b = 1500;
int c = 1600;
const int button = A2;
int buttonstate = 0;
int fsr = A0;
int fsrv;
void setup() {
myservo.writeMicroseconds(b); //set initial servo position if desired
myservo.attach(1); //the pin for the servo control
pinMode(button, INPUT);
}
void loop() {
fsrv = analogRead(fsr);
buttonstate = digitalRead(button);
fsrv = analogRead(fsr);
if (fsrv >= 1) {
myservo.attach(1);
myservo.writeMicroseconds(a);
delay(2000);
//myservo.writeMicroseconds(b);
//Serial.println(b);
//delay(2000);
myservo.detach();
}
if (buttonstate == HIGH) {
myservo.attach(1);
myservo.writeMicroseconds(c);
delay(2000);
myservo.detach();
}
}
Why read the force sensor twice ?
What sort of values do you get for fsr ? From your use of analogRead() I presume that it is an analogue device and, as such, may always give a value of 1 or above.
Below is a simple button setup. The button pin is set HIGH and the button connects the button pin to ground causing the input to go LOW and the servo moves to position 160. When the button is released the button pin goes back HIGH due to the internal pullups and the servo moves to position 20. This could be used with the continuous rotation setup. Not sure what you button setup is.
//zoomkat servo button test 7-30-2011
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(160);
}
else {
servo1.write(20);
}
}
zoomkat:
Below is a simple button setup. The button pin is set HIGH and the button connects the button pin to ground causing the input to go LOW and the servo moves to position 160. When the button is released the button pin goes back HIGH due to the internal pullups and the servo moves to position 20. This could be used with the continuous rotation setup. Not sure what you button setup is.
//zoomkat servo button test 7-30-2011
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
Something very odd is going on and I don't know what.
No matter what pin on the ATTiny85 I set for the button, it only works at the D5 pin, and even when I hook it up there, the servo only rotates for as long as I hold the button. I have no idea what's going on here, and hopefully someone can shine some light on it.
The pin number should be a constant, and should be used instead of repeating the value within the sketch. The point of defining constants like this is that it avoids maintenance-induced errors where only some of the values get modified. If you have done that it would explain why the example doesn't work after you change the pin. I suggest you correct the sample, and try again.
I suggest you fix that call to digitalWrite to use the button1 variable, because that's going to bite you sooner or later. And also make button1 a const, because that's going to bite you eventually too. Neither of these explain your problem but both will save you grief in the long run.
It sounds as if your 'continuous rotation servo' is acting rather like a DC motor. Have you got a link to a spec for it? It may have some unusual timing requirements, or perhaps it's broken - or perhaps it is actually a DC motor.
I'm 100% sure it's a continuous rotation server. Everything worked flawlessly with the uno r3, but the problem is the attiny85. For some reason it doesn't work now. Could it be that it isn't powerful enough, maybe it needs an external clock?
I'm not sure. I'm not exactly 'fluent' in electronics, so I'm not sure if the chip is able to process the code, or if it has enough memory to store it. I'm pretty much out of ideas as to what could be wrong.
This thread suggests that the standard Servo library is not compatible with the ATtiny85, but it seems there may be a SoftwareServo library that you could use instead: