Not receiving error but servos will not move

Hello, I'm very new to programming and I'd like some help with my code. I've create a sketch to control 6 servos using a PCA9685. I attempted to use "If Statements" to prevent damaging the servos if one fails. The code I've created is not showing any errors when I verify it but if I upload it none of the servos move after I trigger the hall sensor. Help would be greatly appreciated as I'm down to 48hrs to finish this. Here is the code:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

//Constants
#define nbPCAServo 16
#define Hall_Sensor_D 2         //D2 with digital output

//Parameters
int MIN_IMP [nbPCAServo] = {155, 155, 155, 180, 85, 85};
int MAX_IMP [nbPCAServo] = {625, 625, 625, 385, 495, 495};
int MIN_ANG [nbPCAServo] = {0, 0, 0, 0, 0, 0};
int MAX_ANG [nbPCAServo] = {120, 120, 90, 75, 160, 160};

#define FACEPLATE_CLOSED 0
#define FACEPLATE_OPEN 1
int facePlateCurMode = FACEPLATE_OPEN; // Keep track if the faceplate is open or closed
int Val2 = 0;

Adafruit_PWMServoDriver pca = Adafruit_PWMServoDriver(0x40);

uint8_t servonum = 6;
void setup() {
  Serial.begin(9600);
  pinMode(Hall_Sensor_D, INPUT_PULLUP);
  Serial.println("Initialize System");
  pca.begin();
  pca.setPWMFreq(60);
}


void pcaScenarioOpen() {


  pca.setPin(0, 0, false); // activate pin 0
  pca.setPin(1, 0, false); // activate pin 1
  pca.setPin(2, 0, false); // activate pin 2
  pca.setPin(3, 0, false); // activate pin 3
  pca.setPin(4, 0, false); // activate pin 4
  pca.setPin(5, 0, false); // activate pin 5


  for (int i = 0; i < nbPCAServo; i++) {
    for (int pos = MAX_IMP[0]; MAX_IMP[0] > MIN_IMP[0]; MAX_IMP[i] -= 10)
      for (int pos = MIN_IMP[1]; MIN_IMP[1] < MAX_IMP[1]; MIN_IMP[1] += 10) {//switch min and max of this servo
        pca.writeMicroseconds(0, MAX_IMP[0]);
        pca.writeMicroseconds(1, MIN_IMP[1]); delay(10);
      }
    servonum++;            // Proceeds to next servo.
    if (int pos = MAX_IMP[0]);
    if (int pos = MIN_IMP[1]);
  }
  pca.setPin(0, 0, true); // deactivate pin 0
  pca.setPin(1, 0, true); // deactivate pin 1

  for (int pos = MAX_IMP[2]; MAX_IMP[2] > MIN_IMP[2]; MAX_IMP[2] -= 10) {
    pca.writeMicroseconds(2, MAX_IMP[2]); delay(10);
  }
  servonum++;            // Proceeds to next servo.
  if (int pos = MAX_ANG[2]) {
    pca.setPin(2, 0, true); // deactivate pin 2
  }
  for (int pos = MAX_IMP[3]; MAX_IMP[3] > MIN_IMP[3]; MAX_IMP[3] -= 10) {
    pca.writeMicroseconds(3, MAX_IMP[3]); delay(10);
  }
  servonum++;            // Proceeds to next servo.
  if (int pos = MAX_IMP[3]) {
    pca.setPin(3, 0, true); // deactivate pin 3
  }

  for (int i = 0; i < nbPCAServo; i++) {
    for (int pos = MAX_IMP[4]; MAX_IMP[4] > MIN_IMP[4]; MAX_IMP[i] -= 10)
      for (int pos = MAX_IMP[5]; MAX_IMP[5] > MIN_IMP[5]; MAX_IMP[5] -= 10) {
        pca.writeMicroseconds(4, MAX_IMP[4]);
        pca.writeMicroseconds(5, MAX_IMP[5]); delay(10);
      }
    servonum++;            // Proceeds to next servo.
    if (int pos = MAX_IMP[4]);
    if (int pos = MAX_IMP[5]);
  }
  pca.setPin(4, 0, true); // deactivate pin 4
  pca.setPin(5, 0, true); // deactivate pin 5

}
int jointToImp(double x, int i) { /* function jointToImp */
  ////Convert joint angle into pwm command value
  int imp = (x - MIN_ANG[i]) * (MAX_IMP[i] - MIN_IMP[i]) / (MAX_ANG[i] - MIN_ANG[i]) + MIN_IMP[i];
  imp = max(imp, MIN_IMP[i]);
  imp = min(imp, MAX_IMP[i]);
  return imp;

  int facePlateCurMode = FACEPLATE_OPEN;
}

void pcaScenarioClose() {

  pca.setPin(0, 0, false); // activate pin 0
  pca.setPin(1, 0, false); // activate pin 1
  pca.setPin(2, 0, false); // activate pin 2
  pca.setPin(3, 0, false); // activate pin 3
  pca.setPin(4, 0, false); // activate pin 4
  pca.setPin(5, 0, false); // activate pin 5


  for (int pos = MIN_IMP[0]; MIN_IMP[0] < MAX_IMP[0]; MIN_IMP[0] += 10)
    for (int pos = MAX_IMP[1]; MAX_IMP[1] > MIN_IMP[1]; MAX_IMP[1] -= 10) {
      pca.writeMicroseconds(0, MIN_IMP[0]);
      pca.writeMicroseconds(1, MAX_IMP[1]); delay(10);
    }
  servonum++;            // Proceeds to next servo.
  if (int pos = MIN_IMP[0]);
  if (int pos = MAX_IMP[1]);
  //if statement using the servos 4 & 5 IMP postion or use scenarios

  pca.setPin(0, 0, true); // deactivate pin 0
  pca.setPin(1, 0, true); // deactivate pin 1

  for (int pos = MIN_IMP[2]; MIN_IMP[2] < MAX_IMP[2]; MIN_IMP[2] += 10) {
    pca.writeMicroseconds(2, MIN_IMP[2]); delay(10);
  }
  servonum++;            // Proceeds to next servo.
  if (int pos = MIN_ANG[2]) {

    pca.setPin(2, 0, true); // deactivate pin 2
  }
  for (int pos = MIN_IMP[3]; MIN_IMP[3] < MAX_IMP[3]; MIN_IMP[3] += 10) {
    pca.writeMicroseconds(3, MIN_IMP[3]); delay(10);
  }
  servonum++;            // Proceeds to next servo.
  if (int pos = MIN_IMP[3])
    pca.setPin(3, 0, true); // deactivate pin 3

  for (int pos = MIN_IMP[4]; MIN_IMP[4] < MAX_IMP[4]; MIN_IMP[4] += 10)
    for (int pos = MIN_IMP[5]; MIN_IMP[5] < MAX_IMP[5]; MIN_IMP[5] += 10) {
      pca.writeMicroseconds(4, MIN_IMP[0]);
      pca.writeMicroseconds(5, MIN_IMP[1]); delay(10);
    }
  servonum++;            // Proceeds to next servo.
  if (int pos = MIN_IMP[4]);
  if (int pos = MIN_IMP[5]);

  pca.setPin(4, 0, true); // deactivate pin 4
  pca.setPin(5, 0, true); // deactivate pin 5

  int facePlateCurMode = FACEPLATE_CLOSED;
}

void loop() {

  Val2 = digitalRead(Hall_Sensor_D)
         ; Serial.println(Hall_Sensor_D);


  if (Val2 == LOW) {
    pcaScenarioOpen();
  }
  else {
    pcaScenarioClose();
  }
  if (int facePlateCurMode = FACEPLATE_OPEN) {
    pcaScenarioClose();
  }
  if (int facePlateCurMode = FACEPLATE_CLOSED) {
    pcaScenarioOpen();
  }
}



Welcome to the forum

Thanks for using code tags in your first post, but the opening tag should be at the start of a line. I have edited your post to move the opening tag to a new line so that the code tags are activated

How are the servos powered ?

Thank you for the reponse. It's a DC power supply.

I have it set to 5V

I think the problem is here... You are saying "check this NEW local integer val if is egual to FACEPLATE_OPEN ".
But it's a NEW local variable. Remove Int word and declare it in global secion

Hi,
Have you developed your code in stages?

Have you got code that JUST controls ONE Servo, to prove your hardware and basic coding?
If not, start there.

Then code that JUST reads your hall effect device and output to you serial monitor?
If you don't have code then that is the next step.

Tom..... :grinning: :+1: :coffee: :australia:

In your code you initialise servonum to 6 then increment it in several places in the code but you never use the variable.

What is its purpose ?

I'm under the impression that it sets the amount of servos used by pca in the sketch. I incremented it due to the differing servos have different min & max pwm and angles.

I doubt that as the value is never used in the sketch or passed to the library. In any case, the value is only ever incremented so will go from 6 to 255 then reset back to 0 then continuously cycle between 0 and 255

Does the library come with any examples ?

Hi,

It sounds like you wrote your code in one big hit.
Can I suggest you go back to the start and get a servo working first?

Can you post a circuit diagram please?
A hand drawn picture will do, especially include power supplies, component labels and pin names.
What model Arduino are you using?

Tom... :grinning: :+1: :coffee: :australia:

Both for conditions are always false, a value is never bigger or less than itself.

Yes, It does but it only gives examples on how find the MIN and MAX pwm and cycle the servo back and forth

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.