Iis there an Arduino with more pins??

// a simple Arduino Sketch to control 6 servos for point or signal control
// using 6 toggle switches.
// each servo moves between one of two position depending on the switch setting
// each servo can have different limits to it's movement

// the servos must not be powered from the Arduino, it can't supply enough current
// typical wiring for the servos is like this
//
// Arduino servo pin -------------------Servo Signal wire (Orange or white perhaps)
// Power supply Positive -------------- Servo Positive (Centre wire - red)
// Power supply Ground -------------- Servo Ground (Black or Brown wire)
// |
// Arduino Ground ----------- (NB Arduino Ground must be connected to power supply ground)

// The toggle switches should be connected so that when switched they connect the Arduino switch pin to ground
//
// Arduino Switch pin ---------- ---------- Arduino Ground
//
// If the switch moves the servo in the wrong direction turn the switch around in it's mounting

// The high and low positions can be set separately for each servo by editing the data in the
// arrays servoLowPos and servoHighPos. In the program as written they are all set to 50 and 150 degrees.

#include <Servo.h>

Servo pointServo[9];

byte servoPin[] = { 2, 3, 4, 5, 6, 7}; // pin numbers for servo signals
byte switchPin[] = { 19, 18, 17, 16, 15, 14}; // pin numbers for switch connections
// Uno analog pins A5 A4 A3 A2 A1 A0
byte servoLowPos[] = { 50, 50, 50, 50, 50, 50}; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150}; // degrees for high servo position
byte servoPos[6];
int Relay1 = 8;
int Relay2 = 9;
int Relay3 = 10;
int Relay4 = 11;
int Relay5 = 12;
int Relay6 = 13;

int Relay1state = 0;
int sw24state = 0;
int sw24 = 0;

void setup() {
Serial.begin(9600); //start Serial in case we need to print debugging info
setupServos();
setupSwitches();

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);

}

void loop() {

byte swA = digitalRead(switchPin[14]);

if (swA == 1)
{
digitalWrite (Relay1, 1);
}
if (swA != 1)
{
digitalWrite (Relay1, 0);
}

for (byte n = 0; n < 6; n++) {
boolean servoMove = false;
byte sw = digitalRead(switchPin[n]);
if (sw == HIGH) {
if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoHighPos[n];
}
}
else { // if sw == LOW
if (servoPos[n] != servoLowPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoLowPos[n];
}
}
if (servoMove) { // only move the servo if the switch has changed
pointServo[n].write(servoPos[n]);
}
}

}

void setupServos() {
for (byte n = 0; n < 6; n++) {
pointServo[n].attach(servoPin[n]);
servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
}
}

void setupSwitches() {
for (byte n = 0; n < 6; n++) {
pinMode(switchPin[n], INPUT);
}
}

Also, what does this mean?? - #include <Servo.h>

The line #include <Servo.h> brings the Servo library into the sketch.

byte servoPin[] =     {  2,   3,   4,   5,   6,   7,   8,   9,  10}; // pin numbers for servo signals
byte switchPin[] =    { 19,  18,  17,  16,  15,  14,  13,  12,  11}; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0

The sketch uses all of the Uno pins but the Uno has just enough pins to do the job. Note that the author uses the analog pins by calling them by their digital pin numbers (A0 = 14, A1 = 15, ans so on).

Hi,
How many servos do you wish to control?

Can you post a link to where you got the code?
Did the link supply a circuit diagram?

Thanks.. Tom.. :slight_smile:

Many thanks to you both. So the number of pins is not an issue. This sketch was designed for 9 servos, controlled by 9 switches. ( Not sure where it came from)

I want to modify it to operate 4 servos and 4 relay coils. I will have a go at that and come back to you if it all goes bottoms up.

I am still not clear about the mean?? " #include <Servo.h> ".

Do I have to do anything, and why exactly is it there.

If I remove it and Verify I get this for the next line down:-

exit status 1
'Servo' does not name a type

I am assuming that now it does not know what a servo is??, but I am told never to assume...

I live in hope.... BC

#include <Servo.h>

See reply #1

That line of code causes the compiler to include (get it ?) the code that allows you to control servos. ~~ You will also see~~
Servo pointServo[9];
this creates a servo object that you can send commands to when a servo is connected to pin 9. If you don't include the library then the code cannot create the servo object
EDITED : corrected spelling

Ignore my struck through ramblings above. I am not having a good day !

A Mega has a great many more I/O pins than an Uno.

...R

people tend to forget that at one time they did not know, just like you

somebody went to the trouble of writing code to solve a problem, so everyone who came after does not have to. if a device needs a complex command sent to it, the library writer writes code that lets you enter plain language commands, and the library converts PLAY_FILE_FROM_FOLDER, 1, 2 into the command code 0x7E 06 0F 00 01 02 EF that the module understands

# tells the program to recover the subroutines you need from the Servo library, so every single programmer out there does not need to be an expert on servos
if you want to see how much work, and how much learning curve you did not have to duplicate, read the .h and .cpp files in a library

Robin2:
A Mega has a great many more I/O pins than an UNO.

But is a poor approach to the problem.

It has been determined that a UNO is sufficient (but a Nano would as usual, be far more practical) for this task.

If you did need to control more servos - or indeed to more efficiently implement the current task - you should use something designed specifically for the task. The PCA9685 module.

Firstly, thank you all for taking the time to reply. I am learning slowly.

I have some Nanos on order (3off at £1.67 each from Banggood)

As I know know that I have 18 i/o to play with, I have now gone for 6 servos, and 6 relays rather than 4.
I have allocated the output pins for the relays, but no logic as yet.

For the model railway people among you, these are for controlling electrofrog points. The servo will do the mechanical work, and the relay will change over the electrics.

If possible I would like to slow down the operation of the servo. I am asuming that at the moment, the servo position is change from 50 to 150 instantly. Is there a "Simple" way for making this happen over say 1/2 second. ??

// a simple Arduino Sketch to control 6 servos for point or signal control
// using 6 toggle switches.
// each servo moves between one of two position depending on the switch setting
// each servo can have different limits to it's movement

// the servos must not be powered from the Arduino, it can't supply enough current
// typical wiring for the servos is like this
//
//   Arduino servo pin -------------------Servo Signal wire (Orange or white perhaps)
//   Power supply Positive -------------- Servo Positive (Centre wire - red) 
//   Power supply Ground   -------------- Servo Ground (Black or Brown wire)
//                             |
//   Arduino Ground -----------     (NB Arduino Ground must be connected to power supply ground)

// The toggle switches should be connected so that when switched they connect the Arduino switch pin to ground
//
//  Arduino Switch pin ---------- \---------- Arduino Ground
//
//  If the switch moves the servo in the wrong direction turn the switch around in it's mounting

//  The high and low positions can be set separately for each servo by editing the data in the
//    arrays servoLowPos and servoHighPos. In the program as written they are all set to 50 and 150 degrees.


#include <Servo.h>

Servo pointServo[9];

byte servoPin[] =     {  2,   3,   4,   5,   6,   7}; // pin numbers for servo signals
byte switchPin[] =    { 19,  18,  17,  16,  15,  14}; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0
byte servoLowPos[] =  { 50,  50,  50,  50,  50,  50}; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150}; // degrees for high servo position
byte servoPos[6];
int Relay1 = 8;
int Relay2 = 9;
int Relay3 = 10;
int Relay4 = 11;
int Relay5 = 12;

void setup() {
setupServos();
setupSwitches();
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);

}

void loop() {
  for (byte n = 0; n < 6; n++) {
     boolean servoMove = false;
     byte sw = digitalRead(switchPin[n]);
     if (sw == HIGH) {
        if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
          servoMove = true;
          servoPos[n] = servoHighPos[n];
        }
     }
     else {  // if sw == LOW
       if (servoPos[n] != servoLowPos[n]) {  // check if the position has changed
          servoMove = true;
          servoPos[n] = servoLowPos[n];
       }
     }
     if (servoMove) {  // only move the servo if the switch has changed
        pointServo[n].write(servoPos[n]);
     }
  }
}

void setupServos() {
  for (byte n = 0; n < 6; n++) {
    pointServo[n].attach(servoPin[n]); 
    servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
  }
}

void setupSwitches() {
  for (byte n = 0; n < 6; n++) {
     pinMode(switchPin[n], INPUT);
  }
}

The VarSpeedServo library might be a way to try.

It might be able to move fast but nothing moves instantly.

You could funeral walk the servo (step, pause, step, pause, ...) from 50 to 150.

Just a quick update. I am making lots of mistakes, but learning along the way. I will keep trying for a while and then come back for more help when I really hit the wall.

BC

Right, I have hit the wall:-

The original sketch, had switch inputs on Pins 14, 15, 16, 17, 18, 19.
These controlled servos on pins 7, 6, 5, 4, 3, 2.

I have tested this and it works OK.

I now want the switch inputs to operate relay coils connected to Pins 8, 9, 10, 11, 12, 13.

I have probably made a complete hash of it, but I tried.

Please have a look and tell me where I am going wrong.

Many Thanks:-

// a simple Arduino Sketch to control 6 servos for point or signal control
// using 6 toggle switches.
// each servo moves between one of two position depending on the switch setting
// each servo can have different limits to it's movement

// the servos must not be powered from the Arduino, it can't supply enough current
// typical wiring for the servos is like this
//
//   Arduino servo pin -------------------Servo Signal wire (Orange or white perhaps)
//   Power supply Positive -------------- Servo Positive (Centre wire - red) 
//   Power supply Ground   -------------- Servo Ground (Black or Brown wire)
//                             |
//   Arduino Ground -----------     (NB Arduino Ground must be connected to power supply ground)

// The toggle switches should be connected so that when switched they connect the Arduino switch pin to ground
//
//  Arduino Switch pin ---------- \---------- Arduino Ground
//
//  If the switch moves the servo in the wrong direction turn the switch around in it's mounting

//  The high and low positions can be set separately for each servo by editing the data in the
//    arrays servoLowPos and servoHighPos. In the program as written they are all set to 50 and 150 degrees.


#include <Servo.h>

Servo pointServo[9];

byte servoPin[] =     {  2,   3,   4,   5,   6,   7}; // pin numbers for servo signals
byte switchPin[] =    { 19,  18,  17,  16,  15,  14}; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0
byte servoLowPos[] =  { 50,  50,  50,  50,  50,  50}; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150}; // degrees for high servo position
byte servoPos[6];
int Relay1 = 8;
int Relay2 = 9;
int Relay3 = 10;
int Relay4 = 11;
int Relay5 = 12;
int Relay6 = 13;

int Relay1state = 0; 
int sw24state = 0; 
int sw24 = 0;

void setup() {
Serial.begin(9600);  //start Serial in case we need to print debugging info
setupServos();
setupSwitches();

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);

}

void loop() {

Relay1state = digitalRead(sw24);   

byte sw24 = digitalRead(switchPin[14]);
byte sw25 = digitalRead(switchPin[15]);
byte sw26 = digitalRead(switchPin[16]);
byte sw27 = digitalRead(switchPin[17]);
byte sw28 = digitalRead(switchPin[18]);
byte sw29 = digitalRead(switchPin[19]);
  
  for (byte n = 0; n < 6; n++) {
     boolean servoMove = false;
     byte sw = digitalRead(switchPin[n]);
     if (sw == HIGH) {
        if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
          servoMove = true;
          servoPos[n] = servoHighPos[n];
        }
     }
     else {  // if sw == LOW
       if (servoPos[n] != servoLowPos[n]) {  // check if the position has changed
          servoMove = true;
          servoPos[n] = servoLowPos[n];
       }
     }
     if (servoMove) {  // only move the servo if the switch has changed
        pointServo[n].write(servoPos[n]);
     }
  }



if (sw24 == 1)  {digitalWrite (Relay1, 1);}
if (sw24 != 1)  {digitalWrite (Relay1, 0);}
if (sw25 == 1)  {digitalWrite (Relay2, 1);}
if (sw25 != 1)  {digitalWrite (Relay2, 0);}
if (sw26 == 1)  {digitalWrite (Relay3, 1);}
if (sw26 != 1)  {digitalWrite (Relay3, 0);}
if (sw27 == 1)  {digitalWrite (Relay4, 1);}
if (sw27 != 1)  {digitalWrite (Relay4, 0);}
if (sw28 == 1)  {digitalWrite (Relay5, 1);}
if (sw28 != 1)  {digitalWrite (Relay5, 0);}
if (sw29 == 1)  {digitalWrite (Relay6, 1);}
if (sw29 != 1)  {digitalWrite (Relay6, 0);}



 
}

void setupServos() {
  for (byte n = 0; n < 6; n++) {
    pointServo[n].attach(servoPin[n]); 
    servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
  }
}

void setupSwitches() {
  for (byte n = 0; n < 6; n++) {
     pinMode(switchPin[n], INPUT);
  }
}
  if (sw24 == 1)
  {
    digitalWrite (Relay1, 1);
  }
  if (sw24 != 1)
  {
    digitalWrite (Relay1, 0);
  }

You can simplify this. sw24 will always have a value of HIGH or LOW. Why not just write this value to the relay pin ?

Once you have got that working note that the switch pins are in an arrays so why not put the corresponding relay pins in an array too ?

Thanks Bob, I will give that a try later...

How do I do this??:- "why not put the corresponding relay pins in an array too ?"

Where do you do your Hellying??

BC

How do I do this??:- "why not put the corresponding relay pins in an array too ?"

When I went to look at your code to answer this I saw a fatal flaw in what you had written

 byte sw24 = digitalRead(switchPin[14]);

The problem is that the switchPin array only has 6 elements numbered 0 to 5. You must fix that before moving on. Use the array to set up the pinMode() of the inputs

  for (int pin = 0; pin < 6; pin++)
  {
    pinMode(switchPin[pin], INPUT);  //maybe better as INPUT_PULLUP if you have no pullup or pulldown resistors in place
  }

How do I do this??:- "why not put the corresponding relay pins in an array too ?"

Put the relay pins in an array

const byte relayPins[] = {8, 9, 10, 11, 12, 13}; //relay pins

and set their pinMode() in a for loop as above (I will leave that to you)

Now in loop() you can read the inputs in a for loop like this

  for (int pin = 0; pin < 6; pin++)
  {
    digitalWrite(relayPins[pin], digitalRead(switchPin[pin]);
  }

You already have a for loop in loop() so you could put the digitalWrite() line inside that and kill 2 birds with one stone

Deleted, I missed page 2!!!!

Hi,
If you have your switches wired between input and gnd, have you got a 10K pullup resistor between input and 5V.
Otherwise declare your switch inputs as INPUT_PULLUP.

Tom... :slight_smile:

Still hitting a wall. Bob, I tried to include as you suggested, but total fail

I started again, and got this far

However when I introduce these lines:- ( as per ******)

void setup() {
setupServos();
setupSwitches();
// int swA = digitalRead(switchPin[14]); ***********
// int rlyA = digitalWrite(relayPin{13]; ***********

I get this error message:-

Multiple libraries were found for "Servo.h"
Used: C:\Users\Barry\Documents\Arduino\libraries\Servo
Not used: C:\Program Files (x86)\Arduino\libraries\Servo
exit status 1
'setupServos' was not declared in this scope

As for the (Input Pullup) issue. I prefer to have a pull down resistor, and make the input got "high" for the "On" condition. It's just what I am used to industrially, where switching to the 0 volt line was banned years ago.

Once again, thanks for the help...

BC

// a simple Arduino Sketch to control 6 servos for point or signal control
//   using 6 toggle switches.
// each servo moves between one of two position depending on the switch setting
// each servo can have different limits to it's movement

// the servos must not be powered from the Arduino, it can't supply enough current
//   typical wiring for the servos is like this
//
//   Arduino servo pin -------------------Servo Signal wire (Orange or white perhaps)
//   Power supply Positive -------------- Servo Positive (Centre wire - red) 
//   Power supply Ground   -------------- Servo Ground (Black or Brown wire)
//                             |
//   Arduino Ground -----------     (NB Arduino Ground must be connected to power supply ground)

// The toggle switches should be connected so that when switched they connect the Arduino switch pin to ground
//
//  Arduino Switch pin ---------- \---------- Arduino Ground
//
//  If the switch moves the servo in the wrong direction turn the switch around in it's mounting

//  The high and low positions can be set separately for each servo by editing the data in the
//    arrays servoLowPos and servoHighPos. In the program as written they are all set to 50 and 150 degrees.


#include <Servo.h>

Servo pointServo[9];

byte servoPin[] =     {  2,   3,   4,   5,   6,   7}; // pin numbers for servo signals
byte relayPin[] =     {  8,   9,  10,  11,  12,   13}; // pin numbers for relay signals
byte switchPin[] =    { 19,  18,  17,  16,  15,  14}; // pin numbers for switch connections
// Uno analog pins      A5   A4   A3   A2   A1   A0
byte servoLowPos[] =  { 50,  50,  50,  50,  50,  50}; // degrees for low servo position
byte servoHighPos[] = {150, 150, 150, 150, 150, 150}; // degrees for high servo position
byte servoPos[6];
byte Relay1 = 8;

void setup() {
setupServos();
setupSwitches();
// int swA = digitalRead(switchPin[14]);
// int rlyA = digitalWrite(relayPin{13]; 


}

void loop() {
pinMode(8, OUTPUT);

  
for (byte n = 0; n < 6; n++) {
boolean servoMove = false;
byte sw = digitalRead(switchPin[n]);
if (sw == HIGH) {
if (servoPos[n] != servoHighPos[n]) { // check if the position has changed
servoMove = true;
servoPos[n] = servoHighPos[n];
}
}
else {  // if sw == LOW
if (servoPos[n] != servoLowPos[n]) {  // check if the position has changed
servoMove = true;
servoPos[n] = servoLowPos[n];
}
}
if (servoMove) {  // only move the servo if the switch has changed
pointServo[n].write(servoPos[n]);
}
}
}

void setupServos() {
for (byte n = 0; n < 6; n++) {
pointServo[n].attach(servoPin[n]); 
servoPos[n] = servoLowPos[n]; // this is just a starting value and may be over-ridden immediately by a switch
}
}

void setupSwitches() {
for (byte n = 0; n < 6; n++) {
pinMode(switchPin[n], INPUT_PULLUP);
}
}

Used: C:\Users\Barry\Documents\Arduino\libraries\Servo

The Servo library is a library that is included with the IDE. There is no reason for it being in your "user libraries" folder. Remove it from C:\Users\Barry\Documents\Arduino\libraries.

The code compiles fine for me. Uno and ver 1.8.5 of the IDE.