arduino uno
sg90 servo
I use
I use two servos 1. The servo rotates 90 degrees continuously.
2. Servo rotates 180 degrees when a button is pressed.
My problem is that I don't have delays when the button is pressed and how can I solve it.
You will need to change your code. Since I can't see your code I can't give any you more detail. Please post the code and a schematic/wiring diagram.
Steve
cenkgursu:
The servo rotates 90 degrees continuously.
What does that mean?- 0-90-0-90-0-90.... as long as there's power?
Servo rotates 180 degrees when a button is pressed
And does that mean 0-180 on button press and stay there forever?- or back to 0 on next press, then 180 on a 3rd press etc?
#include <VarSpeedServo.h>
VarSpeedServo myservo;
VarSpeedServo myservo2;
const int servoPin = 8;
int servoPin2 = 9;
int SwitchPin = 6;
int val=0;
int Stateswitch = 0;
int lastStateswitch = 0;
void setup() {
myservo.attach(servoPin);
myservo.write(180,0,true);
myservo.write(0,180,true);
myservo2.attach(servoPin2);
pinMode(SwitchPin, INPUT_PULLUP);
}
void loop() {
myservo2.write(0,90,true);
myservo2.write(90,0,true);
Stateswitch = digitalRead(SwitchPin);
if (Stateswitch != lastStateswitch) {
if (Stateswitch == LOW) {
val = val + 1;
myservo.write(180,0,true);
myservo.write(0,180,true);
}
lastStateswitch = Stateswitch;
}
}
Take a careful look at what the parameters to VarSpeedServo's write() command. Hint, the second one is NOT a position it's called 'speed'.
What exactly does "I don't have delays when the button is pressed" mean? What are you expecting to be delayed and by how much?
Steve
When the button is pressed, there is a delay due to the cycle. i dont want this.
Moving 90 degrees will not stop.
Pressing the 180 degree button will move.
but there is a delay
Seems you want to have your cake and eat it. I've never used that library, but my understanding of the "true" here:
myservo.write(180,0,true);
myservo.write(0,180,true);
... is to make it wait before performing the next action, and I guess you need that so that it arrives before coming back. But then to make the button responsive, you need it not to wait.
BTW, as I say i've never used that library, but I don't think you're using the 2nd paramater (underlined below) correctly. It's the speed according to the .h file, but you seems to be using 180,0 and 0,180 as a from/to?
myservo.write(180,[u]0[/u],true);
myservo.write(0,[u]180[/u],true);
write(value) - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster
write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete
Yes, when you use true in the write() you turn it into blocking code. Nothing else can happen until the servo has completed it's move. So if you press the button while all that's going on it will ignore you until the moves are completed.
If you want it to be genuinely responsive you're going to need to rewrite it completely using millis() (BlinkWithoutDelay) techniques.
Steve
The demo Several Things at a Time illustrates the use of millis() to move a servo without blocking. It may help with understanding the technique.
Have a look at Using millis() for timing. A beginners guide if you need more explanation.
...R
1.servo works like a vibration motor.
The second servo is used to quickly push an assembly.
When the button is pressed, there is only one movement and it comes back. Problem is sometimes not reacting when the button is pressed.
twinkleyan
the code I use for the library I use
I was able to produce such a solution.
at least 1.servo does not pause, but it does not matter even if the 2nd pause
#include <VarSpeedServo.h>
VarSpeedServo myservo;
VarSpeedServo myservo2;
const int servoPin = 8;
int servoPin2 = 9;
int SwitchPin = 6;
int Stateswitch = 0;
int lastStateswitch = 0;
void setup() {
myservo.attach(servoPin);
myservo.write(180,0,true);
myservo.write(0,180,true);
myservo2.attach(servoPin2);
pinMode(SwitchPin, INPUT_PULLUP);
}
void loop() {
int left = 0;
int right = 30;
int speed1 = 180;
int speed2 = 180;
myservo2.write(left, speed1);
myservo2.wait();
myservo2.write(right, speed1);
Stateswitch = digitalRead(SwitchPin);
if (Stateswitch != lastStateswitch) {
if (Stateswitch == LOW) {
myservo.write(180,0,true);
myservo.write(0,180,true);
}
lastStateswitch = Stateswitch;
}
}