Servos and push-buttons

Hello, my first post here…and probably as usual for you guys - a help request.

What i’m trying to do is to control model railway points/turnouts/switches using RC-servos driven by arduino. I have no problems with the mechanical or electrical parts but the code is driving me nuts.

Basically, what i’m trying to accomplish is using one momentary switch(or two wires, as of now) to change the position of the servo - from one angle to another(~15-20 degrees apart) and return to the first position on second push of the same button. It works like a charm with one bi-colour led(for the turnouts that are not visible i need to know the position they are in) and one servo. But more than that is a no-go for me.

As the LEDs and servos take up 2 pins each and my UNO only has 12(?) sacrifices need to be made - i only have the extreme need for 2 sets of LEDs so i could use 4 servos per board - two with indicators and 2 without. Unless, of course, analog pins can somehow be made useful.

Is this even possible? And of course, i’m using a separate power source for the servos.

The code I “borrowed” from teh interwebs for this:

#include <Servo.h> 

int inPin = 2;         // the number of the input pin
int outPin1 = 3;       // the number of the output pin for red light
int outPin2 = 4;       // the number of the output pin for green light

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

Servo myservo;


void setup()
{
  myservo.attach(9);
  pinMode(inPin, INPUT);
  pinMode(outPin1, OUTPUT);
  pinMode(outPin2, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin1, state);
  digitalWrite(outPin2, state-1);
  
    if (state == HIGH) {
  myservo.write(37);
  }  // sets the servo position to 37, change this to required setting
  else    {                       // waits for the servo to get there 
  myservo.write(20);
  } // sets the servo position to 20, change this to required setting
  

  previous = reading;
}

Hi and welcome.

How many servos would you like to use ? And are you planning on pushing multiple buttons at the same time ? You can use 6 buttons on a single analog input pin. But then you'd have to only have a single button pushed at a time. A0 to A5 can be used as digital inputs or outputs without any problem.

How many servos would you like to use ?

As many as possible on one board - with the current code/setup 4 pins would be used with each led-servo and 2 for servos without led indicators. The total amount of servos/turnouts will be over 20.

And are you planning on pushing multiple buttons at the same time ?

No, one button/switch at a time.

You can use 6 buttons on a single analog input pin.

By using different resistor on each button/switch or some other way?

But nevertheless, the code is the problem at the moment until I have exhausted all the available digital pins.

You might use pots as variable voltage dividers for individual push buttons into an analog input pin. You might also use multiplex chips like the 4051 and similar to multiplex the servo pulses to the appropriate servos. I think most hobby servos will stay in position when the control pulses are stopped.

zoomkat: You might use pots as variable voltage dividers for individual push buttons into an analog input pin. You might also use multiplex chips like the 4051 and similar to multiplex the servo pulses to the appropriate servos. I think most hobby servos will stay in position when the control pulses are stopped.

as the distance between the board and servos will be quite big(40-100cm, depending) and the track will have ac voltage with dcc there will probably be too much interference for the servos to truly stay still and not move at the most inappropriate moment. This idea would then also require to shut off the power to all servos exept the one being switched at the moment.

This idea would then also require to shut off the power to all servos exept the one being switched at the moment.

Why do you think that?

Hi,

Adafruit are good as they supply library and example code.

Tom… :slight_smile:

zoomkat: Why do you think that?

I think there might be interference from the ac voltage. Without a pull-up the servos move like crazy even without any ac-source present.

TomGeorge: Hi,

http://www.adafruit.com/products/815

Adafruit are good as they supply library and example code.

Tom..... :)

This seems like a good option, price-wise.

Can anyone to help me out with the code for adding a second(and so on) servo to the board?

I think there might be interference from the ac voltage. Without a pull-up the servos move like crazy even without any ac-source present.

That sounds like something besides ac electrical noise is causing your issues. Improper grounding between a servo and an arduino can cause erratic servo behavior.

so, i still haven't found or had the luck to "create" a code for controlling the servos the way i need to...

anyone?..