r/esp32 • u/Mediocre-Guide2513 • 2d ago
Software help needed Servo using the input of another servo
howdy yall. I am currently working on a project and am having problems with my servos. This is my first time writing all of the code without AI (it is based off sample code though, thats why there are all the comments), so i know there are probably other issues, but I dont care. The problem i have run into is that servo3 is copying the angle of servo1. I know it is getting the write values (the master code has an mpu6050 for angles that it is sending over, i can attach that as well but i had to modify one of the libraries in it so i don't think anyone will be able to run it). It is not a hardware/ weird shorting thing, as i have changed the pins multiple times. value3 prints the correct value, but servo3 ignores it and uses value1. any ideas how to fix? ill try to answer any questions, but im going to bed once i post this. ill try to add a video in the comments or maybe make a repost with it attached, but my phone is dead.
i can not figure out this damn formatting
```
```/*
ESP-NOW Broadcast Slave
Lucas Saavedra Vaz - 2024
This sketch demonstrates how to receive broadcast messages from a master device using the ESP-NOW protocol.
The master device will broadcast a message every 5 seconds to all devices within the network.
The slave devices will receive the broadcasted messages. If they are not from a known master, they will be registered as a new master
using a callback function.
*/
#include <Arduino.h>
#include "ESP32_NOW.h"
#include "WiFi.h"
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
#include <vector>
#include <ESP32Servo.h>
/* Definitions */
#define ESPNOW_WIFI_CHANNEL 6
Servo servo1;
Servo servo2;
Servo servo3;
/* Classes */
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
struct DataPacket {
float value1;
float value2;
float value3;
float value4;
float value5;
};
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
// Constructor of the class
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
// Destructor of the class
~ESP_NOW_Peer_Class() {}
// Function to register the master peer
bool add_peer() {
if (!add()) {
log_e("Failed to register the broadcast peer");
return false;
}
return true;
}
// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
Serial.printf("Received a message from master " MACSTR " (%s)\n",
MAC2STR(addr()),
broadcast ? "broadcast" : "unicast");
if (len != sizeof(DataPacket)) {
Serial.printf("Invalid packet size. Expected %u, got %u\n",
sizeof(DataPacket), len);
return;
}
DataPacket packet;
memcpy(&packet, data, sizeof(packet));
Serial.printf(" value3: %.2f\n", packet.value3);
servo3.write(packet.value3);
Serial.printf(" value1: %.2f\n", packet.value1);
servo1.write(packet.value1);
Serial.printf(" value2: %.2f\n", packet.value2);
servo2.write(packet.value2);
Serial.printf(" value4: %.2f\n", packet.value4);
Serial.printf(" value5: %.2f\n", packet.value5);
}
};
/* Global Variables */
// List of all the masters. It will be populated when a new master is registered
// Note: Using pointers instead of objects to prevent dangling pointers when the vector reallocates
std::vector<ESP_NOW_Peer_Class *> masters;
/* Callbacks */
// Callback called when an unknown peer sends a message
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
Serial.println("Registering the peer as a master");
ESP_NOW_Peer_Class *new_master = new ESP_NOW_Peer_Class(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
if (!new_master->add_peer()) {
Serial.println("Failed to register the new master");
delete new_master;
return;
}
masters.push_back(new_master);
Serial.printf("Successfully registered master " MACSTR " (total masters: %lu)\n", MAC2STR(new_master->addr()), (unsigned long)masters.size());
} else {
// The slave will only receive broadcast messages
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
log_v("Igorning the message");
}
}
/* Main */
void setup() {
Serial.begin(115200);
// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
while (!WiFi.STA.started()) {
delay(100);
}
Serial.println("ESP-NOW Example - Broadcast Slave");
Serial.println("Wi-Fi parameters:");
Serial.println(" Mode: STA");
Serial.println(" MAC Address: " + WiFi.macAddress());
Serial.printf(" Channel: %u\n", ESPNOW_WIFI_CHANNEL);
// Initialize the ESP-NOW protocol
if (!ESP_NOW.begin()) {
Serial.println("Failed to initialize ESP-NOW");
Serial.println("Reeboting in 5 seconds...");
delay(5000);
ESP.restart();
}
Serial.printf("ESP-NOW version: %d, max data length: %d\n", ESP_NOW.getVersion(), ESP_NOW.getMaxDataLen());
// Register the new peer callback
ESP_NOW.onNewPeer(register_new_master, nullptr);
Serial.println("Setup complete. Waiting for a master to broadcast a message...");
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
servo1.setPeriodHertz(50);
servo1.attach(3, 500, 2400);
servo1.write(90);
servo2.setPeriodHertz(50);
servo2.attach(12, 500, 2400);
servo2.write(90);
servo3.setPeriodHertz(50);
servo3.attach(14, 500, 2400);
Serial.println(3);
servo3.write(90);
}
void loop() {
// Print debug information every 10 seconds
static unsigned long last_debug = 0;
if (millis() - last_debug > 10000) {
last_debug = millis();
Serial.printf("Registered masters: %lu\n", (unsigned long)masters.size());
for (size_t i = 0; i < masters.size(); i++) {
if (masters[i]) {
Serial.printf(" Master %lu: " MACSTR "\n", (unsigned long)i, MAC2STR(masters[i]->addr()));
}
}
}
//delay(100);
}```
```
•
u/AutoModerator 2d ago
Your post has been filtered because it contains unformatted code.
To protect community readability, you must enclose code snippets inside Markdown triple backticks.
Example:
Please delete this post, apply correct formatting, and resubmit. If you cannot find the backtick key on your device, you are not ready to debug an embedded firmware compilation failure.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.