Objective.
Drive small inexpensive hobby servos to switch the rails on a model train layout. I an using the SoftwareServo library and Arduino 0018, running on a duemilanove.
A bank of switches and resistor work as voltage splitters so that 3 switches can provide 8 alternative analog inputs onto an analog pin. The 8 analog inputs could dictate 2 positions to each of 3 servos using if else statements. I intended to have 11 servos controlled through the Arduino, 10 having 2 alternate positions (full left and full right) and one variable, controlled by a pot.
What happened.
Starting from zero knowledge of arduino after a few months I got as far as controlling one servo faultlessly using the if/else statements, when I increased to 3 servos and they started chattering and the separate voltage regulator started getting hot, put on 6 servos and some started acting erratically while most shivered and the voltage regulator got very hot and I quickly disconnected it
What I have tried.
I have had 3 servos physically connected to the Arduino but with the “servo,write” lines //commented out. I get 1 servo = no chatter, 2 servos = infrequent chatter , 3 servos gives a constant shiver.
I have altered the code so the if/else statements control 1 servo to a number of positions while 2 other servos are connected but the “servo,write” lines //commented out.
The single servo worked smoothly bit every few minutes I hear it give a twitch.
I conclude from the above that the Arduino digital outputs are changing when multiple servos are controlled because I think I have eliminated everything else.
Questions?
Have I been using the wrong I/O pins I just started with 1-3 then 1-6?
Is there some other error in my sketch?
Should I have used “case” instead of if/else? ( Hope not because I can't understand them)
Do I need a different approach?
Below is the code that didn't work
/* Vere Nicolson, 25,06,2010. Sketch to control 10 servos (to act as model train track switch changers)controlled by voltage splitters.
Each of 3 voltage splitters can control 3 servos each and a 4th controls the 10th servo. A potentiometer controls an 11th servo driving a turntable.
*/
// progress, passes compiling
//Next step, upload to arduino and run with switches/3 servos before saving and progressing as 1-3
#include <SoftwareServo.h>
SoftwareServo Servo1,Servo2,Servo3, Servo4, Servo5, Servo6, Servo7, Servo8, Servo9, Servo10, Servo11; //Declare Servos
int InPin123 = 1; //
int InPin456 = 2;
int InPin789 = 3;
int InPin10 = 4;
int InPinPot = 0;
int val123 = 0;
int val456 = 0;
int val789 = 0;
int val10 = 0;
int val11 = 0;
void setup ( )
{
Servo1.attach(1); //
Servo2.attach(2);
Servo3.attach(3);
Servo4.attach(4);
Servo5.attach(5);
Servo6.attach(6);
Servo7.attach(7);
Servo8.attach(8);
Servo9.attach(9);
Servo10.attach(10);
Servo11.attach(11);
}
void loop()
{
val123 = analogRead(InPin123); //123 is orange wire > Analog pin1
val456 = analogRead(InPin456); //456 is yellow wire > pin2
val789 = analogRead(InPin789); //789 is green wire > pin3
val10 = analogRead(InPin10); //10 is blue wire > pin4
val11 = analogRead(InPinPot); //11 is red wire >pin0
if (val123 > 900 ) //Switches LLL 1023 //First bank of 3 switches Orange wire
{
Servo1.write(2); //
Servo2.write(2);
Servo3.write(2); //
}
else if (val123 > 510 && val123 < 590 ) //Switches LLR 551
{
Servo1.write(2); //
Servo2.write(2);
Servo3.write(178);
}
else if (val123 > 650 && val123 < 710 ) //Switches LRL 692
{
Servo1.write(2); //
Servo2.write(178);
Servo3.write(2);
}
else if (val123 > 200 && val123 < 250 ) //Switches LRR 219
{
Servo1.write(2); //
Servo2.write(178);
Servo3.write(178);
}
else if (val123 > 780 && val123 < 820 ) //Switches RLL 803
{
Servo1.write(178); //
Servo2.write(2);
Servo3.write(2);
}
else if (val123 > 300 && val123 < 350 ) //Switches RLR 329
{
Servo1.write(178); //
Servo2.write(2);
Servo3.write(178);
}
else if (val123 > 450 && val123 < 490 ) //Switches RRL 471
{
Servo1.write(178); //
Servo2.write(178);
Servo3.write(2);
}
else if ( val123 < 20 ) //Switches RRR 0
{
Servo1.write(178); //
Servo2.write(178);
Servo3.write(178);
}
// Works to here before paste 21]07]2010
// paste in here text file for the yellow wire once have Corrected all servo numbers from 1,2 &3 to 4,5,&6
if (val456 > 900 ) //Switches LLL 1023 //Second bank of 3 switches/yellow wire
{
Servo4.write(2); //
Servo5.write(2);
Servo6.write(2); //
}
else if (val456 > 510 && val456 < 570 ) //Switches LLR 551
{
Servo4.write(2); //
Servo5.write(2);
Servo6.write(178);
}
else if (val456 > 750 && val456 < 789 ) //Switches LRL 773
{
Servo4.write(2); //
Servo5.write(178);
Servo6.write(2);
}
else if (val456 > 400 && val456 < 450 ) //Switches LRR 416
{
Servo4.write(2); //
Servo5.write(178);
Servo6.write(178);
}
else if (val456 > 790 && val456 < 820 ) //Switches RLL 803
{
Servo4.write(178); //
Servo5.write(2);
Servo6.write(2);
}
else if (val456 > 300 && val456 < 350 ) //Switches RLR 329
{
Servo4.write(178); //
Servo5.write(2);
Servo6.write(178);
}
else if (val456 > 591 && val456 < 620 ) //Switches RRL 606
{
Servo4.write(178); //
Servo5.write(178);
Servo6.write(2);
}
else if ( val456 < 260 ) //Switches RRR 249
{
Servo4.write(178); //
Servo5.write(178);
Servo6.write(178);
}
//Paste in here for the 3rd wire Green which has the same analog and servo values, just change 123 to 789 and servos from 12&3 to //7,8,&9
//Paste in here for 4th wire Servo 10 Blue
//Past in here for pot, servo 11 Red
//re number servos/re label if else val789s
delay(15); // waits for the servo to get there
SoftwareServo::refresh();
}