I'm trying to control:
3 stepper motors via two joysticks
a servo motor using one pushbutton
a dc motor using two push buttons (of joysticks)
I'm doing this using ESPNOW protocol on two ESP32s.
The problem is the stepper motors are not working smoothly or sometimes making noises or ignoring the command.
I've double checked the wiring and tried the code without ESPNOW and it works fines. But with ESPNOW it doesn't work. Below are the codes for both sender and receiver ESP32.
Sender:
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
#define open 12
// Yrotation button pins
#define CW_BUTTON_PIN 32
#define ACW_BUTTON_PIN 33
//joystick pins
#define x1 14
#define y 27
#define x2 34
#define y2 35
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xC0, 0x49, 0xEF, 0xD0, 0xDB, 0xB8};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
bool o;
int cw;
int acw;
int m_x1;
int m_y;
int m_x2;
int m_y2;
} struct_message;
// Create a struct_message called myData
struct_message myData;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
pinMode(open,INPUT_PULLUP);
pinMode(CW_BUTTON_PIN, INPUT_PULLUP);
pinMode(ACW_BUTTON_PIN, INPUT_PULLUP);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
myData.o = digitalRead(open);
myData.cw = digitalRead(CW_BUTTON_PIN);
myData.acw = digitalRead(ACW_BUTTON_PIN);
myData.m_x1 = analogRead(x1);
myData.m_y = analogRead(y);
myData.m_x2 = analogRead(x2);
myData.m_y2 = analogRead(y2);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
//}
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
// delay(1000);
}
Code: Select all
#include <ESP32MX1508.h>
#include <ESP32Servo.h>
#include <esp_now.h>
#include <WiFi.h>
// Yrotation driver pins
#define ROTA 12
#define ROTB 14
#define CH1 0
#define CH2 1
#define RES 12
//link pins
const int dirx = 22; //x
const int stepx = 23;
const int diry = 19; //y
const int stepy = 18;
const int dirz = 25; //z
const int stepz = 26;
const int diryr = 32;
const int stepyr = 33;
// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33
int servoPin = 15;
Servo myservo; // servo object for gripper
MX1508 motorA(ROTA, ROTB, CH1, CH2, RES); //object for yrot motor
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
bool o;
int cw;
int acw;
int m_x1;
int m_y;
int m_x2;
int m_y2;
} struct_message;
// Create a struct_message called myData
struct_message myData;
/*************link moving functions***********************/
//x
void forw_x()
{
digitalWrite(dirx, HIGH);
digitalWrite(stepx, HIGH);
delayMicroseconds(800);
digitalWrite(stepx, LOW);
delayMicroseconds(500);
}
void back_x()
{
digitalWrite(dirx, LOW);
digitalWrite(stepx, HIGH);
delayMicroseconds(800);
digitalWrite(stepx, LOW);
delayMicroseconds(500);
}
//y
void forw_y()
{
digitalWrite(diry, HIGH);
digitalWrite(stepy, HIGH);
delayMicroseconds(800);
digitalWrite(stepy, LOW);
delayMicroseconds(500);
}
void back_y()
{
digitalWrite(diry, LOW);
digitalWrite(stepy, HIGH);
delayMicroseconds(800);
digitalWrite(stepy, LOW);
delayMicroseconds(500);
}
//z
void forw_z()
{
digitalWrite(dirz, HIGH);
digitalWrite(stepz, HIGH);
delayMicroseconds(800);
digitalWrite(stepz, LOW);
delayMicroseconds(500);
}
void back_z()
{
digitalWrite(dirz, LOW);
digitalWrite(stepz, HIGH);
delayMicroseconds(800);
digitalWrite(stepz, LOW);
delayMicroseconds(500);
}
//yr
void forw_yr()
{
digitalWrite(diryr, HIGH);
digitalWrite(stepyr, HIGH);
delayMicroseconds(800);
digitalWrite(stepyr, LOW);
delayMicroseconds(500);
}
void back_yr()
{
digitalWrite(diryr, LOW);
digitalWrite(stepyr, HIGH);
delayMicroseconds(800);
digitalWrite(stepyr, LOW);
delayMicroseconds(500);
}
/****************function for links****************/
void links() {
//move x
if (myData.m_y == 0)
{
forw_x();
Serial.println(myData.m_y);
}
if (myData.m_y >= 4000 && myData.m_x1 < 4000)
{
back_x();
//Serial.println(myData.m_x1);
}
//move y
if (myData.m_x1 == 0)
{
forw_y();
Serial.print(myData.m_x1);
}
if (myData.m_y >= 4000 && myData.m_x1 > 4000)
{
back_y();
Serial.print(myData.m_y);
}
//move z
if (myData.m_y2 == 0)
{
forw_z();
Serial.print(myData.m_y2);
}
if (myData.m_y2 >= 4000 && myData.m_x1 < 4000)
{
back_z();
Serial.print(myData.m_x2);
}
//move yr
if (myData.m_x2 == 0)
{
forw_yr();
Serial.print(myData.m_x2);
}
if (myData.m_y2 >= 4000 && myData.m_x2 > 4000)
{
back_yr();
Serial.print(myData.m_y2);
}
}
/****************function for gripper****************/
void gripper() {
if (myData.o == LOW){
Serial.println("Openning");
myservo.write(0);
} else {
// Stop motor if no button is pressed
Serial.println("Default open");
myservo.write(180);
}
}
/****************function for gripper****************/
void rotation() {
// Control motor based on button states
if (myData.cw == LOW && myData.acw == HIGH) {
//Serial.println("Forward");
motorA.motorGo(4095); // Pass the speed to the motor
} else if (myData.acw == LOW && myData.cw == HIGH) {
//Serial.println("Reverse");
motorA.motorRev(4095); // Pass the speed to the motor
} else {
// Stop motor if no button is pressed
//Serial.println("Stop");
motorA.motorBrake(); // Hard Stop -no arguement
}
}
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
links();
gripper();
rotation();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
//links pins
pinMode(stepx, OUTPUT);
pinMode(dirx, OUTPUT);
pinMode(stepy, OUTPUT);
pinMode(diry, OUTPUT);
pinMode(stepz, OUTPUT);
pinMode(dirz, OUTPUT);
pinMode(stepyr, OUTPUT);
pinMode(diryr, OUTPUT);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);// Standard 50hz servo
myservo.attach(servoPin, 500, 2400);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}