Controlling servos by switches

Hi everybody, I am new on this forum, but am using Arduino to control my DCC network for model trains.

The Subject chosen indicate a simple task, which is even more frustrating not to succeed with.

For this project I am using a ProMini; What I want to do is control 8 servos by means of

8 switches(“8” simply since ProMini has 16 usable pins). I can do this with ONE servo/switch, so I think I

understand how Software Servo works, but when introducing For-loops it doesn’t work anymore. The

code compiles ok, but the connected servo does nothing. I am more or less convinced there is some kind

of indexing error, or even simpler than that, so I humbly ask for a look at the code, with more

experienced eyes than mine.

The code:

//Code to control Servos

#include <SoftwareServo.h>

SoftwareServo servo1; // create servo object to control servos
int sw_val1 = 0;
int to_left = 50;
int to_right = 150;

int swpins= {11,12,13,14,15,16,17,18}; // define switchpins
int fpins= {3,4,5,6,7,8,9,10}; // define servopins

int index;
void setup()
{
for (int index = 0; index < 8; index++)
{servo1.attach(fpins[index]);
{pinMode(swpins[index], INPUT);
digitalWrite(swpins[index],HIGH);
}
}
}

void loop()
{ for (int index = 0; index < 8; index++)
{ int val = digitalRead(swpins[index]);
if (val = LOW)
{
servo1.write(index),(to_left);
}
else
{
servo1.write(index),(to_right);
}
}
SoftwareServo::refresh();
}

Regards
Peter in Sweden

You have one instance of the SoftwareServo class. You need one per servo.

 if (val = LOW)

Here, you are assigning the value of LOW to val, not testing that val equals LOW. Use == to compare.

How ARE your switches wired? You are not using the internal pullup resistors, so you need external resistors.

Thanks for a fast answer !

So, one SoftwareServo class for each servo; this would mean SoftwareServo servo1, SoftwareServo

servo2 etc. ?

And could these be defined in a for...loop too ? This wasn't clear to me from what I have read about

Software Servo.

I did go through the code, but obviously missed "val = LOW".

And pull-ups, the code is from an Arduino instruction example, what do you think is wrong with

it ?

Regards
Peter

will have to recheck syntax for that.

Why not use the standard Servo library ?

You can create an array of servo objects and an array of input pins attached to the switches. Read each input in turn and move the associated servo accordingly.

Untested code for 3 servos

#include <Servo.h>

Servo servos[3];
const byte servoPins[] = {10, 11, 12};
const byte inputPins[] = {7, 8, 9};

void setup()
{
  for (int s = 0 ; s < 3; s++)
  {
    servos[s].attach(servoPins[s]);
    pinMode(inputPins[s], INPUT_PULLUP);
  }
  Serial.begin(115200);
  Serial.println();
}

void loop()
{
  for (int s = 0; s < 3; s++)
  {
    if (digitalRead(inputPins[s]) == HIGH)
    {
      servos[s].write(180);
    }
    else
    {
      servos[s].write(0);
    }
  }
}

UKHeliBob;

Yes, why not use this simple, clear solution ? I guess I found my version in some example

and found it useful. I am not quite sure what the practical difference is between Software Servo and

standard Servo you suggest.

Will try out your suggestion, thanks !

PaulS;

Will see which solution works best for me....

Thanks to you both !
Regards
Peter

See http://forum.arduino.cc/index.php?topic=449112.0

Will the Arduino be doing anything besides reading the switches and controlling the servos ?

UKHeliBob-

I thought I did answer before, but do not see the post...:

No, the Arduinos will only drive servos, 8 per PRO Mini, to control turnouts on my model railroad.

I will need 16-17 servos, so two PRO Minis and one or two manual controls will do. Basically this is only

using Arduino as a PWM-generator (with many individual outputs).

Since my last post your suggested code is now tested, and works just like it should. What remains is

finetuning servo angles, and preferably slowing down servo movement.

Thanks again !

Peter

finetuning servo angles, and preferably slowing down servo movement.

Servo angles - easy peasy
Slowing down servo movement - more "interesting". I can foresee the millis() function in your future

Slowing down servos is easy. Just use VarSpeedServo library instead of Servo library. It has an extra parameter on the write() which sets the speed of movement.

Steve

I am building an Arduino system to control servos on a model railway. I have built a few "slave" boards with an Atmega 328 on stripboard (veroboard) and I am using a Mega as the "brains" of the system. The control panel switches will go to the Mega and it will send a serial message to all of the slaves telling them what to do.

The slave boards are identical and will have identical programs - the only difference will be the board ID which can be set with some jumpers.

I chose the Mega because it has lots of I/O pins and because it has multiple HardwareSerial ports. It will also be controlling the speed of the trains and taking wireless data from the hand-controllers for the trains.

For a smaller system you could use an Uno or Leonardo or Micro or Pro-Micro in place of the Mega. The Leonardo has the advantage of a Leonardo or Micros is that they have a spare HardwareSerial port.

...R

Slipstick;
Will try out Var Speed Servo library too; turnouts should have a slow movement. With that working only

noise is still a problem, the small servos I use do cause noise, screwed to the layout as they are. I have

searched for rubberbushings, but haven't found it.

Still, my main issue has been solved, thanks everyone.

Peter

I have searched for rubberbushings, but haven’t found it.

My local hardware store sells sheets of rubber. I’d get some, and cut out my own.

Or, maybe some o-rings would be sufficient - one between the servo and the mounting surface and one between the servo and the mounting screw.

I have used short pieces cut from small bore plastic tubing to make stand-offs for PCBs. That may also be a convenient approach for mounting your servos. A local hardware store sells several different sizes of plastic tubing. People use it for aquariums and beer making.

...R

PaulS and Robin2;
thanks for creative suggestions. My turnouts will look/sound much better quiet...
Peter