Hello, everyone!
I'm developing a video art project and I need to have a camera (my old iPhone SE) remotely controlled by wifi -OSC. On the test bench everything seemed to go well but after setting everything up I'm having problems stabilizing the servo motors.
In this project I integrate a patch in Max/msp to control the mechanical movements, sending the data via OSC to a UNO R4 WIFI board connected to two servo motors (MG995-180 and SG90-180). The board and servo motors are powered by a 10,000mAh powerbank (Xiaomi: - USB-A port: 5 V 2.4 A / 9.0 V 2 A / 12.0 V 1.5 A MAX, USB-C port: 5 V 3 A -22.5 W MAX).
Unexpected servo movements occur even when the servos are not receiving commands. On the other hand, the servo movements, when controlled remotely, are very jerky. I set up the system and recorded a video to demonstrate the problem ([https://youtu.be/EpTot-duJsw?si=ezK3EoUrbaT0PFxU]). I connected a multimeter in parallel to the power supply and I see fluctuations in the values (voltage) whenever the servo motors move. The values sent by Max/msp (OSC/Wifi) appear stable on the serial monitor, allowing me to assume that this is not the problem.
I would like your help and thank you for taking the time to read my request.
I enclose the code.
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
const int ledPin = 2;
const char* ssid = "*******";
const char* password = "*******";
WiFiUDP Udp;
const IPAddress outIp(172, 20, 10, 2);
const unsigned int outPort = 9999;
const unsigned int localPort = 8888;
Servo servo1;
Servo servo2;
// Definições de pinos SPI
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Inicialização do TFT
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);
connectWiFi();
Udp.begin(localPort);
servo1.attach(5);
servo2.attach(3);
}
void connectWiFi() {
Serial.print("Conectando-se a ");
Serial.println(ssid);
WiFi.begin(ssid, password);
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 15000) {
delay(500);
Serial.print(".");
}
tft.fillScreen(ST7735_BLACK);
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("Conectado ao WiFi");
IPAddress ip = WiFi.localIP();
Serial.print("Endereço IP: ");
Serial.println(ip);
tft.setCursor(10, 30);
tft.print("IP: ");
tft.println(ip);
tft.setCursor(10, 50);
tft.print("ONLINE");
} else {
Serial.println("\nFalha na conexão");
tft.setCursor(10, 30);
tft.print("OFFLINE");
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
OSCMessage msgIN;
int size;
if ((size = Udp.parsePacket()) > 0) {
while (size--) {
msgIN.fill(Udp.read());
}
if (!msgIN.hasError()) {
msgIN.route("/toggleLED", toggleOnOff);
msgIN.route("/servo1", setServo1);
msgIN.route("/servo2", setServo2);
}
}
int servo1Value = servo1.read();
int servo2Value = servo2.read();
Serial.print("Servo 1: ");
Serial.println(servo1Value);
Serial.print("Servo 2: ");
Serial.println(servo2Value);
delay(10);
}
void toggleOnOff(OSCMessage &msg, int addrOffset) {
bool ledState = msg.getFloat(0);
digitalWrite(ledPin, ledState);
}
void setServo1(OSCMessage &msg, int addrOffset) {
int servo1Value = msg.getFloat(0);
servo1Value = constrain(servo1Value, 0, 180);
servo1.write(servo1Value);
}
void setServo2(OSCMessage &msg, int addrOffset) {
int servo2Value = msg.getFloat(0);
servo2Value = constrain(servo2Value, 0, 180);
servo2.write(servo2Value);
}
Thank you very much!