Multi Servo Control with Lights

Hi, I am new to using arduino, so I am unsure how to program them. I have played about with some programs to get partly what I want but have it a brick wall. Code below

#include<Servo.h>

Servo myservo1;
Servo myservo2;

int pos = 30;

void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);

myservo1.attach(9);

}
void loop()
{
if (digitalRead(2) == HIGH && pos < 85) {
pos++;
myservo1.write(pos);
delay(4);
}
if (digitalRead(3) == HIGH && pos > 45) {
pos--;
myservo1.write(pos);
delay(4);
}
}

What I am doing is using a On-off-On switch to turn the servo left to right, but i would like to do this with mutli switches and servo's but can't get the code to work. Also I would like to have and LED light up when I switch left (possible red) and right (possible green) but can't work out how to code this.

I know that there is a lot of people that have a better brain than I have so I am asking for some help please. :astonished:

Where did you attach servo2 ?

At the moment it not attached to anything, but I will be attaching them ro a model railway layout.

Missed point error in reply #2

In your program myservo1 is attached to pin 9
Where in your program is myservo2 attached to a pin ?

By the way, how are the input pins wired ?

Mistdragon:
At the moment it not attached to anything, but I will be attaching them ro a model railway layout.

Guessing for points/turnouts?

If so, then I wonder if you really need the pos++ and pos-- to gradually move from open to closed (or whatever the terminology is), but can't you just do a servo.write(22) and servo.write(64) or whatever numbers work?

You might also find it useful to look here to see about using the internal pullup resistors. It changes your logic, test for ==LOW not ==HIGH but is a pretty standard way of doing things. (You do need a pulldown or pullup (on or the other, pullup's more common, and builtin) to make sure your pin is definitely at one value or other and not floating in aimlessly.

Sorry servo 2 is attached to pin 10.

The one thing that I am still wondering is how to get LEDs to work with the switch from left to right, can you think of a way to add LEDs to the system. I have the two servos working so it just the light now.

You want to make a RR crossing signal? I think that would be a perfect application of blink without delay.
Go google "blink without delay" and try it out.
The try adding a second LED attached to another pin.
Turn one on when you turn the other off and vice versa.
Then add it to this program.

Ask questions if you run into any problems.

Oh, and post a video once you get it all together.

an you think of a way to add LEDs to the system

Post the code that you are using now. Please use code tags when you do.

When you move a servo from one side to the other simply turn on one LED and turn off the other.

Thanks for the answers but the thing is after reading them I tried to get the three servos working off different switches, by copying the code changing the number but this does not seem to work as it should. Here is the code, can someone look over and tell me where I have gone wrong.

#include<Servo.h>

Servo Point01;
Servo Point02;
Servo Point03;


int pos = 30;

void setup()
{
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  
  Point01.attach(9);
  Point01.attach(10);
  Point01.attach(11);
}
void loop()
{
  if (digitalRead(2) == HIGH && pos <95) {
    pos++;
    Point01.write(pos);
    delay(4);
  }
  if (digitalRead(3) == HIGH && pos >55) {
    pos--;
    Point01.write(pos);
    delay(4);
  }
  if (digitalRead(4) == HIGH && pos <95) {
    pos++;
    Point02.write(pos);
    delay(4);
  }
  if (digitalRead(5) == HIGH && pos >55) {
    pos--;
    Point02.write(pos);
    delay(4);
    }
  if (digitalRead(6) == HIGH && pos <95) {
    pos++;
    Point03.write(pos);
    delay(4);
  }
  if (digitalRead(7) == HIGH && pos >55) {
    pos--;
    Point03.write(pos);
    delay(4);
   }
   }

How are the switches wired ?

Shouldn't there be a different position value for each servo ?

Shouldn't this:

Point01.attach(9);
 Point01.attach(10);
 Point01.attach(11);

Be something like:

 Point01.attach(9);
 Point02.attach(10);
 Point03.attach(11);

756E6C:
Shouldn't this:

 Point01.attach(9);

Point01.attach(10);
Point01.attach(11);



Be something like:


Point01.attach(9);
Point02.attach(10);
Point03.attach(11);

No, it should be like

 Point[0].attach(9);
 Point[1].attach(10);
 Point[2].attach(11);

(or similar) :wink:

Correct, and it would be easier if that lower board were made of glass.

Well I tried all your ideas, but this was the message that I was getting

exit status 1
'Point' was not declared in this scope

what would help is if you could you could add your ideas to my code and highlight the changes.

Servo Point [3];

If you see an error such as
'Point' was not declared in this scopeyou should immediately check :

1 - is Point declared anywhere in the program ? Maybe it is spelt wrongly/differently somewhere
2 - if it is declared then where ? What scope does the variable have