Been trying to make a simple car with 2 28BYJ-48 5VDC motors and their drivers, using the CustomStepper library. If I use the demo script from the lib it does work well, but with my custom manager for handling movement with 2 motors they just vibrate in place and do nothing. It worked fine earlier and the only change I made was restructuring the code. I power them from a separate 9V battery (I know that can be problematic but the same problem happens when I connect the motors straight to the Arduino). Here’s all my code:
MotorManager.h:
#ifndef MOTORMANAGER_H
#define MOTORMANAGER_H
#include <CustomStepper.h>
class MotorManager {
public:
CustomStepper &lMotor;
CustomStepper &rMotor;
MotorManager(CustomStepper &_lMotor, CustomStepper &_rMotor)
: lMotor(_lMotor), rMotor(_rMotor) {}
void setup(float RPM, float SPR) {
lMotor.setRPM(RPM);
rMotor.setRPM(RPM);
lMotor.setSPR(SPR);
rMotor.setSPR(SPR);
}
bool areDone() {
return lMotor.isDone() && rMotor.isDone();
}
void stopMoving() {
lMotor.setDirection(STOP);
rMotor.setDirection(STOP);
}
void startMoving() {
lMotor.rotate();
rMotor.rotate();
}
void dirForward() {
lMotor.setDirection(CCW);
rMotor.setDirection(CW);
}
void dirBackward() {
lMotor.setDirection(CW);
rMotor.setDirection(CCW);
}
void rotateDegrees(float degrees) {
lMotor.rotateDegrees(degrees);
rMotor.rotateDegrees(degrees);
}
void dirLeft() {
lMotor.setDirection(CW);
rMotor.setDirection(CW);
}
void dirRight() {
lMotor.setDirection(CCW);
rMotor.setDirection(CCW);
}
void run() {
lMotor.run();
rMotor.run();
}
};
#endif
car.ino:
#include <CustomStepper.h>
#include "MotorManager.h"
CustomStepper lMotor(8, 9, 10, 11);
CustomStepper rMotor(2, 3, 4, 5);
MotorManager motors(lMotor, rMotor);
const int joyX = A0;
const int joyY = A1;
const int button = A5;
const float RPM = 18.0;
const float SPR = 4075.7728395;
void setup() {
Serial.begin(9600);
motors.setup(RPM, SPR);
// joystick is unused for now
pinMode(joyX, INPUT);
pinMode(joyY, INPUT);
pinMode(button, INPUT_PULLUP);
}
void loop() {
if (motors.areDone()) {
// this is a dummy program used just as a test
motors.dirLeft();
motors.rotateDegrees(180.0);
Serial.println("Rotated 180 deg left");
}
motors.run();
}
Looked online and couldn’t find any solutions to similar code-related problems. I highly doubt the hardware is connected wrong since the demo stepper.ino script from the lib works flawlessly with both steppers. Please help