In my code kindly edited by @camsysca, the set up code appears to be working but the servos do not move to position 90 as the code says.
I cannot see any reason for this
[code]
#include <Servo.h>
//constants
const byte InputPins[] = {8, 9, 10, 11,};
const byte NrServos = sizeof(InputPins);
const byte ServoPins[NrServos] = {3, 4, 5, 6};
//Variables
Servo servo;
bool previousButtonState1;
bool previousButtonState2;
int pos1 = 90; // variable to store the servo position // Crossing 1 - left gate servo
int pos2 = 90; // variable to store the servo position // Crossing 1 - right gate servo
int pos3 = 90; // variable to store the servo position // Crossing 2 - left gate servo
int pos4 = 90; // variable to store the servo position // Crossing 2 - right gate servo
void setup() {
Serial.begin(9600);
Serial.println("Start");
for (byte i = 0; i < NrServos; i++) {
pinMode(InputPins[i], INPUT_PULLUP); // set pins to INPUT with pullup
servo.attach(ServoPins[i]);
servo.write(90); // move servo to centre position
servo.detach();
Serial.println("done");
Serial.println(ServoPins[i]);
}
delay(500);
}
void loop()
{
[/code]
There does seem to be an anomaly, although I position the servo initially to a starting position, before it moves it jumps to 90.
Any reason for this? Is it a function of theUse code tags to format code for the forum attach
[code]
#include <Servo.h>
//constants
const byte InputPins[] = {8, 9, 10, 11,};
const byte NrServos = sizeof(InputPins);
const byte ServoPins[NrServos] = {3, 4, 5, 6};
const byte EndPositions[4] = {179, 1, 179, 1};
//Variables
Servo servo;
bool previousButtonState1;
bool previousButtonState2;
int pos1 = 90; // variable to store the servo position // Crossing 1 - left gate servo
int pos2 = 90; // variable to store the servo position // Crossing 1 - right gate servo
int pos3 = 90; // variable to store the servo position // Crossing 2 - left gate servo
int pos4 = 90; // variable to store the servo position // Crossing 2 - right gate servo
void setup() {
Serial.begin(9600);
Serial.println("Start");
for (byte i = 0; i < NrServos; i++) {
pinMode(InputPins[i], INPUT_PULLUP); // set pins to INPUT with pullup
servo.attach(ServoPins[i]);
servo.write(EndPositions[i]); // move servo to Initial position
delay(500);
servo.detach();
Serial.println("done");
Serial.println(ServoPins[i]);
}
}
[/code]
You are using 1 servo variable to control 4 real servo, it is ok for a test but...
User 4 servo variables and control your 4 servo ( and don't call detach... unless there is a reason )
Appreciate the input but don't really understand it. Could you please expand on the reason why I should not detach, also on my original question of why the servo moves to 90 before moving to the specified location.
90 is desirable because it is the power up position of the servo and if the first programmed move is anything else it can cause a violent swing.
But you didn't explain why I should not use detach
Yes. I also see "detach()" is used to conserve battery power (if battery only). What happens to the servo is; all holding torque is removed, so if there is any force on the horn (servo arm), the force will determine the position of the horn.