I have 2 Ohbots connected to a ESP32, each Ohbot has a PCA9685 servo board. One has the default address of 0x40, the other 0x41. I also have 7 potentiometers connected to the ESP32. By changing which servo driver I use I can control the selected Ohbot with the pots. Eventually there will be 5 Ohbots, this system is just to test that I can control each one using the pots, right now I only have 2 connected. What I am trying to do is to be able to enter the Ohbot I want to control by entering a number from 1 to 5 in the serial monitor and then have the pots control that Ohbot.
I assume I would first have to define the object for each servo board.
Adafruit_PWMServoDriver Head1 = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver Head2 = Adafruit_PWMServoDriver(0x41);
and begin each one in setup,
Head1.begin();
Head2.begin();
Head1.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
Head2.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
I would then read the number from the serial monitor and set a object place holder, Ohbot, to the selected head. Right now I'm using this statement, I'll replace that with the read logic later.
strcpy(Ohbot,"Head1");
In the call to the servo I would use this,
Ohbot.setPWM(0, 0, angleToPulse(Pot1Map));
Obviously this isn't working or I wouldn't be asking for help.
Here is my code
//Ohbot manual control with 7 pots
// Replaced Mega with a ESP32
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver Head1 = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver Head2 = Adafruit_PWMServoDriver(0x41);
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates
char BotNum = 1; //Ohbot number to be ontrolled
char Ohbot[6] = "Ohbot";
//char Head1[6] = "Head1";
//char Head2[6] = "Head2";
const int Pot1Pin = 34; // select the input pin for the potentiometer
int Pot1Value = 0; // potentiometer 1 value
int Pot1Map = 0; //mapped value of pot 1 for servo angle
const int Pot2Pin = 35; // select the input pin for the potentiometer
int Pot2Value = 0; // potentiometer 2 value
int Pot2Map = 0; //mapped value of pot 2 for servo angle
const int Pot3Pin = 32; // select the input pin for the potentiometer
int Pot3Value = 0; // potentiometer 3 value
int Pot3Map = 0; //mapped value of pot 3 for servo angle
const int Pot4Pin = 33; // select the input pin for the potentiometer
int Pot4Value = 0; // potentiometer 4 value
int Pot4Map = 0; //mapped value of pot 4 for servo angle
const int Pot5Pin = 25; // select the input pin for the potentiometer
int Pot5Value = 0; // potentiometer 5 value
int Pot5Map = 0; //mapped value of pot 5 for servo angle
const int Pot6Pin = 26; // select the input pin for the potentiometer
int Pot6Value = 0; // potentiometer 6 value
int Pot6Map = 0; //mapped value of pot 6 for servo angle
const int Pot7Pin = 27; // select the input pin for the potentiometer
int Pot7Value = 0; // potentiometer 7 value
int Pot7Map = 0; //mapped value of pot 7 for servo angle
void setup() {
Serial.begin(9600);
Serial.println("Ohbot control");
Head1.begin();
Head2.begin();
Head1.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
Head2.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
delay(10);
//************************************************************
// This line will be replaced by the read serial monitor logic
// read 1 = Head1, read 2 = Head2
strcpy(Ohbot,"Head1");
}
void loop() {
//Head Turn
// read the value from the Pots:
Pot1Value = analogRead(Pot1Pin);
Pot1Map = map(Pot1Value, 0, 4095, 0, 180);
Pot1Map = constrain(Pot1Map, 10, 155);
Ohbot.setPWM(0, 0, angleToPulse(Pot1Map));
//Serial.print(Pot1Value);
//Serial.print("\t");
//Serial.println(Pot1Map);
// Nod
Pot2Value = analogRead(Pot2Pin);
Pot2Map = map(Pot2Value, 1, 4095, 0, 180);
Pot2Map = constrain(Pot2Map, 30, 125);
Ohbot.setPWM(2, 0, angleToPulse(Pot2Map));
//Serial.print(Pot2Value);
//Serial.print("\t");
//Serial.println(Pot2Map);
// Eyes Left / Right
Pot3Value = analogRead(Pot3Pin);
Pot3Map = map(Pot3Value, 1, 4095, 0, 180);
Pot3Map = constrain(Pot3Map, 60, 145);
Ohbot.setPWM(4, 0, angleToPulse(Pot3Map));
//Serial.print(Pot3Value);
//Serial.print("\t");
//Serial.println(Pot3Map);
//Eyes Up / Down
Pot4Value = analogRead(Pot4Pin);
Pot4Map = map(Pot4Value, 1, 4095, 0, 180);
Pot4Map = constrain(Pot4Map, 65, 140);
Ohbot.setPWM(6, 0, angleToPulse(Pot4Map));
//Serial.print(Pot4Value);
//Serial.print("\t");
//Serial.println(Pot4Map);
// Eyelids
Pot5Value = analogRead(Pot5Pin);
Pot5Map = map(Pot5Value, 1, 4095, 0, 180);
Pot5Map = constrain(Pot5Map, 0, 50);
Ohbot.setPWM(8, 0, angleToPulse(Pot5Map));
//Serial.print(Pot5Value);
//Serial.print("\t");
//Serial.println(Pot5Map);
// Top Lip
Pot6Value = analogRead(Pot6Pin);
Pot6Map = map(Pot6Value, 1, 4095, 0, 180);
Pot6Map = constrain(Pot6Map, 35, 75);
Ohbot.setPWM(10, 0, angleToPulse(Pot6Map));
//Serial.print(Pot6Value);
//Serial.print("\t");
//Serial.println(Pot6Map);
// Bottom Lip
Pot7Value = analogRead(Pot7Pin);
Pot7Map = map(Pot7Value, 1, 4095, 0, 180);
Pot7Map = constrain(Pot7Map, 40, 80);
Ohbot.setPWM(12, 0, angleToPulse(Pot7Map));
//Serial.print(Pot7Value);
//Serial.print("\t");
//erial.println(Pot7Map);
/*
Serial.print(Pot1Value);
Serial.print("\t");
Serial.print(Pot2Value);
Serial.print("\t");
Serial.print(Pot3Value);
Serial.print("\t");
Serial.print(Pot4Value);
Serial.print("\t");
Serial.print(Pot5Value);
Serial.print("\t");
Serial.print(Pot6Value);
Serial.print("\t");
Serial.println(Pot7Value);
*/
delay(50);
}
int angleToPulse(int ang){
int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max
//Serial.print("Angle: ");Serial.print(ang);
//Serial.print(" pulse: ");Serial.println(pulse);
return pulse;
}
Thanks for all comments and suggestions.
John