#include <AccelStepper.h>
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
#define HALFSTEP 8
#define motorPin1 32 // IN1 on the ULN2003 driver 1
#define motorPin2 33 // IN2 on the ULN2003 driver 1
#define motorPin3 25 // IN3 on the ULN2003 driver 1
#define motorPin4 26 // IN4 on the ULN2003 driver 1
#define motorPin5 17 // IN1 on the ULN2003 driver 2
#define motorPin6 21 // IN2 on the ULN2003 driver 2
#define motorPin7 16 // IN3 on the ULN2003 driver 2
#define motorPin8 27 // IN4 on the ULN2003 driver 2
const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
int StepsRequired;
int stepperSpeed = 1000;
void setup() {
SerialBT.begin("ESP32-Bluetooth");
Serial.begin(115200); // Untuk debugging melalui Serial Monitor
}
void loop() {
if (SerialBT.available()){
char c = SerialBT.read();
if (c == 'a'){
moveForward();
Serial.println('a');
} else if (c == 'b'){
turnLeft();
Serial.println('b');
} //else if (c == 'c'){
//turnRight();
//Serial.println('c');
//}
}
}
void moveForward() {
StepsRequired = STEPS_PER_OUT_REV * 240/360; // 360 degrees
stepper1.setMaxSpeed(1000.0);
stepper1.move(StepsRequired);
stepper1.setSpeed(stepperSpeed);
stepper2.setMaxSpeed(1000.0);
stepper2.move(-StepsRequired);
stepper2.setSpeed(stepperSpeed);
// Wait for both steppers to reach the target position
while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) {
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();
}
}
void turnLeft() {
StepsRequired = STEPS_PER_OUT_REV * 300/360; // 360 degrees
stepper1.setMaxSpeed(1000.0);
stepper1.move(StepsRequired);
stepper1.setSpeed(stepperSpeed);
stepper2.setMaxSpeed(1000.0);
stepper2.move(StepsRequired);
stepper2.setSpeed(stepperSpeed);
// Wait for both steppers to reach the target position
while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) {
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();
}
StepsRequired = STEPS_PER_OUT_REV * 220 / 360; // 360 degrees
stepper1.setMaxSpeed(1000.0);
stepper1.move(StepsRequired);
stepper1.setSpeed(stepperSpeed);
stepper2.setMaxSpeed(1000.0);
stepper2.move(-StepsRequired);
stepper2.setSpeed(stepperSpeed);
// Wait for both steppers to reach the target position
while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) {
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();
}
}
this is my code sir i have trouble when press button forward only stepper 1 move and stepper 2 don t move but LED driver on stepper 2 B and D the light is on but A and C dont the light is off why this happen sir, please