The purpose should be to execute an action once every interVal1a or interVal1B milliseconds, i.e. advance and then retreat (as in "sweep") a servo (SG90) in 1-increments between servoMin=250 and servoMax=250.
However the "incr=true" cycle results in irregular advance of the servo; the "incr=false") cycle executes smoother (not perfect yet).
What could be wrong in this code?
/* https://mytectutor.com/how-to-use-pca9685-16-channel-12-bit-pwm-servo-driver-with-arduino/
PCA9685 without delay, with millis()
default PCA9685 I2C slave address = 40
*/
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver myServo = Adafruit_PWMServoDriver();
#define servoMin 250
#define servoMax 500
unsigned long previousMillis1a = 0;
unsigned long previousMillis1b = 0;
// unsigned long previousMillis2a = 0;
// unsigned long previousMillis2b = 0;
unsigned long currentMillis;
int interVal1a = 70;
int interVal1b = 100;
// int interVal2a = 30;
// int interVal2b = 60;
uint8_t servonum = 0;
uint8_t numberOfServos = 6;
int pulseLgth = servoMin;
bool incr = true; //
void setup() {
Serial.begin(9600);
myServo.begin();
myServo.setPWMFreq(60);
delay(10);
}
void loop() {
currentMillis = millis();
if (incr == true && currentMillis - previousMillis1a >= interVal1a) {
myServo.setPWM(servonum, 0, pulseLgth);
Serial.println(pulseLgth);
pulseLgth = pulseLgth + 1;
if (pulseLgth == servoMax) {
incr = false;
}
previousMillis1a = currentMillis;
}
if (incr == false && currentMillis - previousMillis1a >= interVal1b) {
myServo.setPWM(servonum, 0, pulseLgth);
Serial.println(pulseLgth);
pulseLgth = pulseLgth - 1;
if (pulseLgth == servoMin) {
incr = true;
}
previousMillis1b = currentMillis;
}
servonum ++;
if (servonum > numberOfServos - 1)
servonum = 0;
}