Hi
I am trying to make a servo move to a given position when a switch is flipped and to move back when the switch is flipped off.
This all works fine, BUT! when the switch is flipped off it takes the arduino uno about 5 seconds before it starts rotating the servo.
Why is this and what can I do about it? I need the servo to move immediately when the switch is flipped off.
My code: #include <Servo.h>
Servo myservo; // create servo object to control a servo
int pin = 9; // digital pin used for activation
int servostart=0; // servo starting position
int servoend=60; // servo end position
void setup()
{
myservo.attach(8); // attaches the servo to pin 8
myservo.write(servostart); // set servo to start position when powered on
pinMode(13,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
}
void loop() {
if(digitalRead(pin)==HIGH){ // if pin is high move servo
myservo.write(servoend); // sets the servo position
digitalWrite(13,HIGH); // Turns ON LED on board for verification
}
else // else move servo to start position
{
myservo.write(servostart); // sets the servo position
digitalWrite(13,LOW); // Turns OFFLED on board for verification
}
}
Grateful for any help, I have tried many ways to get the servo to react faster.
Hi, what are you using to supply power to the servo?
Depending on servo, the arduino cannot supply the current a servo needs, it requires its own supply.
If you are using a separate supply, do you have the gnd of the supply and the gnd of the arduino connected together, you will not be passing control pulses properly if they aren't.
A diagram of your circuit will help, CAD or picture of hand drawn circuit diag, picture of your project too.
The LED turns on and off with the same delay as the servo. It turns on immediately but when the switch is released there is a 4 second delay.
Also attached a circuit drawing, the power supply is a 6v, 1.5a dc power supply.
HI, you don't have a pull down resistor on pin9, try a 10K resistor from pin 9 to gnd.
When you close the switch you put 5V on pin 9, when you release it the 5V charge stays on it and slowwly discharges, probably takes about 5 seconds. So fit a 10K and see what happens.
Or place the switch from pin 9 to gnd and turn on the internal pull up resistors, you will have to change your sketch for the reversed input logic.