Servo motor with Adafruit_PWMServoDrive Library

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
In one of the codes, the above two commands were written. I know servo motors(analogue) function at pulses with 1 to 2 ms duration. But what do the above commands mean?
Also the following commands:

pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);

I guess it is something to do with conversion, but I am not sure.

Further the command:
pwm.setPWM(motorOut, 0, pulse_width);
what is the '0', I mean what is the syntax for this function.

I am a beginner to Arduino and learning new and interesting things. I shall be grateful, if anyone helps me.
Thank, you

The #defines mean that what you think you know is wrong and that many servos operate on a wider range of pulse widths for example from 0.65ms to 2.35ms.

Explaining the lines of code would be much easier if you post the complete code that contained them.

The map command is easy and is explained quite well in the language Reference. You can always get to the references pages by clicking on Resources at the top of the page then on Reference then look for the keyword that's confusing you.

Steve

You can find the code in the following link:Using Servo Motors with the Arduino | DroneBot Workshop
Help me if possible
/*
PCA9685 PWM Servo Driver Example
pca9685-servomotor-demo.ino
Demonstrates use of 16 channel I2C PWM driver board with 4 servo motors
Uses Adafruit PWM library
Uses 4 potentiometers for input

DroneBot Workshop 2018

*/

// Include Wire Library for I2C Communications
#include <Wire.h>

// Include Adafruit PWM Library
#include <Adafruit_PWMServoDriver.h>

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define FREQUENCY 50

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Define Potentiometer Inputs

int controlA = A0;
int controlB = A1;
int controlC = A2;
int controlD = A3;

// Define Motor Outputs on PCA9685 board

int motorA = 0;
int motorB = 4;
int motorC = 8;
int motorD = 12;

void setup()
{
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
}

void moveMotor(int controlIn, int motorOut)
{
int pulse_wide, pulse_width, potVal;

// Read values from potentiometer
potVal = analogRead(controlIn);

// Convert to pulse width
pulse_wide = map(potVal, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);

//Control Motor
pwm.setPWM(motorOut, 0, pulse_width);

}

void loop() {

//Control Motor A
moveMotor(controlA, motorA);

//Control Motor B
moveMotor(controlB, motorB);

//Control Motor C
moveMotor(controlC, motorC);

//Control Motor D
moveMotor(controlD, motorD);

}

Does the program do what you expect when you test it? If so that's a good start.

If you want to know more about the library commands you really do need to look at the documentation for the Adafruit_PWMServoDriver library. Adafruit are good about documenting their products so there's loads of useful information here It's not an easy read but it is worthwhile at least trying then you can ask more detailed questions about anything you have trouble with.

To get you started the first 2 lines you highlighted change the 0-1023 that comes in from the analogRead() of the pot into a suitable value to use in the pwm.setPWM() command. That command sets up the pulse sent to servo. The 0 means the pulse goes high at tick 0 (i.e. immediately) and then it goes back low pulse_width ticks later and those pulses repeat at the frequency set by setPWMFreq() earlier.

Steve

Thanks, Steve, your reply helped me a lot.
But I still have one doubt in the following command:
pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Since I am not a student of electronics, I am not sure how pulse width is specified and converted into different units like bits or seconds. I shall be grateful if anyone can explain the above command and something about the representation of pulses.

That Adafruit driver splits one complete pulse (high part plus low part) into 4096 "ticks". So in setPWM() command you have to tell it when to go up (in ticks) and when to go back down as I said.

The map() gives you the required pulse width in microseconds (650 to 2350). Then that confusing looking calculation converts the microseconds (in pulse_wide) to the number of ticks that correspond to the same pulse width. If you try it out with a calculator (or by putting some Serial.print()s in the program) you should find that e.g. 1000 microseconds converts to 204 ticks. So the signal is high for 204 and low for (4096 - 204).

Really this is something you don't need to know in detail because the examples already give you the code you need. But I like to know the detail of what's going on with things too, which is why I'm spending my time trying to explain this stuff even more simply than the Adafruit documentation does. Hope it helps.

Steve

Thanks a lot, Steve! Your lucid explanation intrigues and encourages me!