Hi all. I have an issue with my Stepper Motor/Controller. I am using the below part code. The full code have a transmitter controller, that sent Analog signals to 4 Receiver controllers via ESP-NOW.
I have a total of 12 Stepper Motors where I control 4 steppers per controller, but I operate max two steppers at any time between the controllers. I have a 5v 4A power supply to the 4 Receiver boards.
The issue is that the motors run to its set positions as intended but when it should return back to its start position it struggle.
I can hear the PWM signal but the motors jitter back and forward but do not reach its starting point.
I am using L298N mini driver boards.
Code:
#include <esp_now.h>
#include <WiFi.h>
#include <EEPROM.h>
#include <AccelStepper.h>
uint8_t incoming_mac1[] = {0xC0, 0x49, 0xEF, 0xD4, 0x58, 0x48};
uint8_t incoming_mac2[] = {0xe0, 0x5a, 0x1b, 0x6c, 0x8f, 0xf4};
typedef struct {
int switch3;
int switch4;
int switch5;
int switch6;
int switch7;
int switch8;
int switch9;
int switch10;
int switch11;
int switch12;
int switch13;
int switch14;
} struct1_message1;
typedef struct {
float pedalA;
float pedalB;
float pedalC;
float pedalD;
float pedalE;
} struct2_message2;
typedef struct {
int LLValue;
int RRValue;
int switch1;
int switch2;
} struct3_message3;
struct1_message1 myData1;
struct2_message2 myData2;
struct3_message3 myData3;
AccelStepper stepper1(AccelStepper::FULL4WIRE, 27, 14,13, 12);
AccelStepper stepper4(AccelStepper::FULL4WIRE, 15, 2,0 , 4);
AccelStepper stepper5(AccelStepper::FULL4WIRE, 16, 17,18,5);
const int potPin1 = 35;
const int potPin2 = 32;
const int potPin3 = 39; // Changed to a different pin
int val1 = 0;
int val2 = 0;
int val3 = 0;
long newval1 = 0;
long newval4 = 0;
long newval5 = 0;
void setup() {
// Serial.begin(9600);
stepper1.setMaxSpeed(2000);
stepper1.setAcceleration(1000);
stepper4.setMaxSpeed(2000);
stepper4.setAcceleration(1000);
stepper5.setMaxSpeed(2000);
stepper5.setAcceleration(1000);
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
return;}
esp_now_register_recv_cb(OnDataRecv);
}
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
if (memcmp(mac_addr, incoming_mac1, 6) == 0) {
// Serial.println("Data received from MAC1:");
// Process data intended for mac1
memcpy(&myData1, incomingData, sizeof(myData1));
} else if (memcmp(mac_addr, incoming_mac2, 6) == 0) {
// Serial.println("Data received from MAC2:");
// Process data intended for mac2
memcpy(&myData2, incomingData, sizeof(myData2));
}
}
void loop() {
val1 = analogRead(potPin1); // kneeData.LLValue
val2 = analogRead(potPin2); // kneeData.RRValue
myStepper.step(1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
delay(500);
float pedalA = myData2.pedalA;
float pedalB = myData2.pedalB;
float pedalC = myData2.pedalC;
float pedalD = myData2.pedalD;
float pedalE = myData2.pedalE;
/////////// STEPPER1 F# TO G
if (pedalD > 0) {
newval1 = map(pedalD, 0, 2023, 0, 100);
stepper1.moveTo(newval1);
stepper1.enableOutputs(); // Enable stepper
} else {
stepper1.disableOutputs(); // Disable stepper
}
/////////// STEPPER4 E TO F
if (pedalE > 0) {
newval4 = map(pedalE, 0, 2023, 0, 200);
stepper4.moveTo(newval4);
stepper4.enableOutputs(); // Enable stepper
} else {
stepper4.disableOutputs(); // Disable stepper
}
/* /////////// STEPPER4 E TO F#
else if (val2 > 2200) {
newval4 = map(val2, 2200, 4095, 0, 100);
stepper4.moveTo(newval4);
}
/////////// STEPPER4 E TO D#
else if (val2 < 2000) {
newval4 = map(val2, 0, 2000, 0, -100);
stepper4.moveTo(newval4);
}*/
/////////// STEPPER5 B TO C#
if (val1 > 0) {
newval5 = map(val1, 0, 950, 0, 200);
stepper5.moveTo(newval5);
stepper5.enableOutputs(); // Enable stepper
} else {
stepper5.disableOutputs(); // Disable stepper
}
// Update motors
stepper1.runSpeedToPosition();
stepper4.runSpeedToPosition();
stepper5.runSpeedToPosition();
/*}
Serial.print(" LL Knee : " + String(val1)); //Pin34
Serial.println(" RR Knee : " + String(val2));//Pin18
//Serial.print(" STRING 1 : " + String(myData1.switch5));//Pin1
//Serial.print(" STRING 2 : " + String(myData1.switch6));
Serial.print(" A : " + String(myData2.pedalA)); //Pin34
Serial.print(" B : " + String(myData2.pedalB));//Pin18
Serial.print(" C : " + String(myData2.pedalC));//Pin18
Serial.print(" D : " + String(myData2.pedalD));//Pin18
Serial.println(" E : " + String(myData2.pedalE));//Pin18
//delay(50);*/
}