Help with setting up servos.

Hello,

I have a problem here. I want to make servos start at a predefined position BEFORE i start the program that I've written (in python) which then in turn controls the servos.

Because when I start the program, they move to what ever default position they have (90 degrees i think) and only after boot I have to press a "home" button that ive made to send them wherever i need.

Please help as I'm totally new to programming with arduino.

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int minPulse = 600;
int maxPulse = 2400;

int userInput[3];
int startbyte;
int servo;      
int pos;      
int i;      

void setup()
{
  servo1.attach(3, minPulse, maxPulse);
  servo2.attach(4, minPulse, maxPulse);
  servo3.attach(5, minPulse, maxPulse);
  servo4.attach(6, minPulse, maxPulse);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 2) {
    startbyte = Serial.read();
    if (startbyte == 255) {
      for (i=0;i<2;i++) {
        userInput[i] = Serial.read();
      }
      servo = userInput[0];
      pos = userInput[1];
      if (pos == 255) { servo = 255; }

      switch (servo) {
        case 1:
          servo1.write(pos);
          break;
        case 2:
          servo2.write(pos);
          break;
        case 3:
          servo3.write(pos);
          break;
        case 4:
          servo4.write(pos);
          break;
      }
    }
  }
}

Any help would be appreciated. ::slight_smile:

Because when I start the program, they move to what ever default position they have (90 degrees i think) and only after boot I have to press a "home" button that ive made to send them wherever i need.

I don't see any code for dealing with a "home" button.

Why not simply put that code in setup(), so you don't need to press the button?

The "Home" is realized via Python program.
And what should I put in in setup() as it just attaches servo object to a digital pin ?

You are sending some instructions to move the servos when the "home" button is pressed, right?

That might result in something like these instructions being executed:

servo1.write(145);
servo2.write(56);
servo3.write(0);
servo4.write(45);

Whatever instructions get executed as a result of pressing the "home" button can be put in setup, so that it is not necessary to press the "home" button.

Thanks Man! Worked perfectly!