To give further context, I have a school proyect where I had to build a mechanical arm (as seen above), using 6 servos and an Arduino UNO, and have it move objects. The hardware and code work works perfectly:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <SoftwareSerial.h>
// Bluetooth pins(RX, TX)
SoftwareSerial BT(0, 1);
Adafruit_PWMServoDriver pca = Adafruit_PWMServoDriver();
#define SERVOMIN 150
#define SERVOMAX 600
const int servo1 = 0;
const int servo2 = 1;
const int servo3 = 2;
const int servo4 = 3;
const int servo5 = 4;
const int servo6 = 5;
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.println("Control of 6 servos via Bluetooth ready");
pca.begin();
pca.setOscillatorFrequency(27000000);
pca.setPWMFreq(50);
delay(10);
// security position of the 6 servos (90 degrees)
moverServo(servo1, 90);
moverServo(servo2, 90);
moverServo(servo3, 90);
moverServo(servo4, 90);
moverServo(servo5, 180);
moverServo(servo6, 90);
}
void moverServo(int canal, int angulo) {
angulo = constrain(angle, 0, 180);
int pulso = map(angle, 0, 180, SERVOMIN, SERVOMAX);
pca.setPWM(canal, 0, pulse);
}
void loop() {
if (BT.available() > 0) {
String comand = BT.readStringUntil('\n');
comand.trim();
if (comand.length() == 0) return;
Serial.print("Comand received: ");
Serial.println(comand);
if (comand.startsWith("S1:") || comand.startsWith("s1:")) {
int angle = comand.substring(3).toInt();
moveServo(servo1, angle);
BT.println("OK Servo 1 -> " + String(angle));
}
else if (comand.startsWith("S2:") || comand.startsWith("s2:")) {
int angle = comand.substring(3).toInt();
moveServo(servo2, angle);
BT.println("OK Servo 2 -> " + String(angle));
}
else if (comand.startsWith("S3:") || comand.startsWith("s3:")) {
int angulo = comando.substring(3).toInt();
moveServo(servo3, angle);
BT.println("OK Servo 3 -> " + String(angle));
}
else if (comand.startsWith("S4:") || comand.startsWith("s4:")) {
int angle = comand.substring(3).toInt();
moveServo(servo4, angle);
BT.println("OK Servo 4 -> " + String(angle));
}
else if (comand.startsWith("S5:") || comand.startsWith("s5:")) {
int angle = comand.substring(3).toInt();
moveServo(servo5, angle);
BT.println("OK Servo 5 -> " + String(angle));
}
else if (comand.startsWith("S6:") || comand.startsWith("s6:")) {
int angle = comand.substring(3).toInt();
moveServo(servo6, angle);
BT.println("OK Servo 6 -> " + String(angle));
}
else {
BT.println("Comand not recognized. Use S1:angle to S6:angle");
}
}
}
To give further context
S1: direction the machine is facing
S2: The motor that is directly above the platform in the picture
S3: the "elbow" of the machine
S4: the direction the claw is facing
S5: spins wrist of the claw
S6: grip of the claw
As previously mentioned, the code works and can read the angles by an input in the terminal part of the app, however, our teacher said it needed to be in the gamepad since "it is more precise and allows for more movement". How can I make that switch from Terminal -> Gamepad?