The below program sets servo's to predetermined positions: servoMin[] and ServoMax[].
The position change can be done either directly, or gradually in 3ms steps with delay() and a for loop.
When both increase angle and decrease angle are done in gradual steps there is an issue.
On startup, all servo's, one by one, are reverting back to the servoMin[] directly (not gradual), and then changing back to their servoMax[] positions gradually (for loop).
If the gradual angle-increase for loop (the increasing angle) is omitted and replaced by a direct angle-increase then there is no issue and the servo's remain stable in the servoMax[] position.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <CMRI.h>
#include <Auto485.h>
#define CMRI_ADDR 1 // this also is the JMRI CMRI SMINI node address
// In JMRI this gives an address 1001 to the first servo on the PCA9685 address 0
// Configure a turnout as 1 bit and steady state
// Meaning: bit = 0 = (for example) thrown and bit = 1 = closed
#define DE_PIN 2
#define numServos 4 //The number of servos connected
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); //setup the board address 0
Auto485 bus(DE_PIN); // Arduino pin 2 -> MAX485 DE and RE pins
CMRI cmri(CMRI_ADDR, 24, 48, bus);
int Status[numServos]; //Create a table to hold the status of each turnout, signal, etc.
int Throw[numServos]; //Create a table to hold the throw value for each servo
int Close[numServos]; //Create a table to hold the close value for each servo
int i;
uint16_t pulselgt;
unsigned int servoMin[] = {300, 300, 200, 246, 246, 172, 246, 200, 200, 200}; //servo lower setting to suit individual servo
unsigned int servoMax[] = {450, 450, 500, 492, 492, 565, 492, 550, 550, 525}; //servo upper setting to suit individual servo
void setup() {
Serial.begin(9600);
bus.begin(9600);
pwm.begin();
pwm.setPWMFreq(50); // This is the maximum PWM frequency
}
void loop() {
cmri.process();
for (i = 0; i < numServos; i++) {
Status[i] = (cmri.get_bit(i));
if (Status[i] == 1) { // decreasing angle
for (pulselgt = servoMax[i]; pulselgt > servoMin[i]; pulselgt--) {
pwm.setPWM(i, 0, pulselgt);
//pwm.setPWM(i, 0, servoMin[i]);
delay(3);
}
}
if (Status[i] == 0) { // increasing angle
for (pulselgt = servoMin[i]; pulselgt < servoMax[i]; pulselgt++) {
pwm.setPWM(i, 0, pulselgt);
// pwm.setPWM(i, 0, servoMax[i]);
delay(3);
}
}
else {
}
}
}
With the last for loop changed to below, there is no issue:
if (Status[i] == 0) { // increasing angle
// for (pulselgt = servoMin[i]; pulselgt < servoMax[i]; pulselgt++) {
// pwm.setPWM(i, 0, pulselgt);
pwm.setPWM(i, 0, servoMax[i]);
delay(3);
// }
}
What is the reason?