Topic: Bluetooth commands don't move the servo
hi guys, I'm trying to set up a simple application to be able to control a servomotor through the esp32 via an on and off switch. The servo motor should move from 0 to 180 degrees and back in a loop while the switch is on, and stop when the switch is off. the problem is that the servomotor doesn't move.
The connections with the wires are correct because I tried to move it by commanding it from the PC and it works; at the same time, the bluetooth connection also works, because I tried to print on the serial monitor the bluetooth commands that I send from the phone to the board and it was succesfull. what could be the problem at this point? Thank you!
//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP32CORE_BLE
#include <BLEDevice.h>
#include <ESP32Servo.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 27 bytes
{ 255,1,0,0,0,20,0,16,16,1,2,0,18,39,30,13,4,27,31,31,
79,78,0,79,70,70,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t switch_1; // =1 if switch ON and =0 if OFF
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#define PIN_SWITCH_1 18
Servo myServo;
int servoPin = 18;
void setup()
{
Serial.begin(9600); // Inizializza la comunicazione seriale
RemoteXY_Init ();
myServo.attach(servoPin);
pinMode (PIN_SWITCH_1, OUTPUT);
}
void loop()
{
RemoteXY_Handler ();
digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH);
// Check the switch state
if (RemoteXY.switch_1 == 1) {
// If the switch is ON, activate the servo motor
Serial.println("Switch is ON. Moving the servo...");
Serial.println("Moving the servo...");
// Move the servo only if the switch is ON
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
Serial.println("Angle: " + String(angle)); // Stampa il valore di angle
RemoteXY_delay(3);
}
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
Serial.println("Angle: " + String(angle)); // Stampa il valore di angle
RemoteXY_delay(3);
}
} else {
// If the switch is OFF, stop the servo motor
myServo.write(90); // Assuming 90 is the center position
Serial.println("Switch is OFF. Stopping the servo.");
RemoteXY_delay(300); // Aggiungi un ritardo dopo aver posizionato il servo
}
}