Go Down

Topic: How to stop tilt servo centering while booting up? (Read 1 time) previous topic - next topic

yair

i ripped the thread topic from a post on a related forum.
my servo centers on boot. its a problem as every time i reset/boot the arduino the servos jump  to mid-tilt position.

i tried
Code: [Select]
#include <Servo.h>

Servo panS;  
Servo tiltS;    

int tiltMin=60;
int panMin=0;


void setup()
{

 panS.attach(9);  // attaches the servo on pin 9 to the servo object
 tiltS.attach(10);

 //on wake up smoothly bring pan and tilt to start
 for(int pos = 93; pos>=panMin; pos--) {
   panS.write(pos);
   delay(33);
 }
 for(int pos = tiltS.read(); pos>=tiltMin; pos--) {
   tiltS.write(pos);
   delay(33);
 }
}
void loop()
{}


but if i try to servo.read() i always get 93. no matter the location.
i found this bit on an old documentation page
Quote
Safety Quirk

Even though you attach a servo, it won't receive any control signals until you send its first position with the write() method to keep it from jumping to some odd arbitrary value.

but what help is that?
im surprised it hadn't been mentioned.
if i haven't searched enough please enlighten.
keywords: servo, jerk, startup, boot

johnwasser

You can set the initial position of a servo before you attach it.  If you don't set the initial position I believe it defaults to centered. 

It's not clear from your code where you want the servos to start. 

Code: [Select]

#include <Servo.h>
Servo panS; 
Servo tiltS;
...

void setup()
{
  panS.write(panInitialPosition);
  panS.attach(9);  // attaches the servo on pin 9 to the servo object

  tiltS.write(tiltInitialPosition);
  tiltS.attach(10);
...



If you don't want them making any sudden moves you will need to remember where they were when you turned off the power. For that you will have to use some form of non-volatile memory like the built-in EEPROM.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

yair

the code logic is:
on startup, read last resting place of servo position and from there write slowly to fixed start position

i will try the EEPROM way, thanks

Go Up