Model railroad crossing

Could someone please help. I would like to be able to change the angle of each servo independently but cant do it.

Thanks, Wally.

#include<Servo.h>;
int pos = 45;

Servo servo1;
Servo servo2;
void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  servo1.attach(10);
  servo2.attach(9);

}
void loop() {
  while (digitalRead(2) == LOW && pos < 45) {
    pos++;
    servo1.write(pos);
    delay(40);
    servo2.write(pos);
    delay(40);
  }
  while (digitalRead(2) == HIGH && pos > 15) {
    pos--;
    servo1.write(pos);
    delay(40);
    servo2.write(pos);
    delay(40);
  }
}

If you want the servo positions to be independently controlled then you will need separate loops for each servo.

Steve

You probably just need two separate pos variables - something like

int maxPos1 = 45;
int maxPos2 = 60;
int pos1 = 0;
int pos2 = 0;

byte upPin = 2;

and code like this

while digitalRead(upPin) == LOW) {
 pos1++
 if (pos1 >= maxPos1) {
 pos1 = maxPos1
 }
 pos2++
 if (pos2 >= maxPos2) {
 pos2 = maxPos2
 }
 servo1.write(pos1);
    servo2.write(pos2);
    delay(40);
}

...R

Thanks for the help, I modified the program using your code example and I can now adjust the servo angles independently.