Am using an arduino nano to power a stepper motor.
Am running in full step mode with a drv8825 controller.
Is this pushing the limits for this arduino to handle the pulses? At higher speeds it looses stability and will just lock up and oscillate/buzz loudly.
Im reading about mid band resonance potentially causing issues, but most of this is way over my head sorry.
Do you see any issues with the code?
I found setting the driver to full step mode performed better and lowered the number of cycles for the arduino.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
const int dirPin = 8;
const int stepPin = 9;
const int enablePin = 10;
const int clkPin = 2;
const int dtPin = 3;
const int swPin = 4;
const int motorStepsPerRev = 200; // Set Driver to full Step
const float totalRatio = 15.0;
int drumRPM = 40;
int runMinutes = 30;
bool isRunning = false;
bool isStopping = false;
unsigned long startTime = 0;
int menuMode = 0;
int lastClkState;
unsigned long lastButtonPress = 0;
float targetStepsPerSec = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
AccelStepper stepper(1, stepPin, dirPin);
void setup() {
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lastClkState = digitalRead(clkPin);
updateDisplay();
}
void loop() {
if (!isRunning) {
handleMenu();
} else {
handleMotorLogic();
}
}
void handleMenu() {
int currentClkState = digitalRead(clkPin);
if (currentClkState != lastClkState && currentClkState == LOW) {
if (digitalRead(dtPin) != currentClkState) {
if (menuMode == 0) drumRPM = min(drumRPM + 1, 90);
if (menuMode == 1) runMinutes = min(runMinutes + 5, 120);
} else {
if (menuMode == 0) drumRPM = max(drumRPM - 1, 40);
if (menuMode == 1) runMinutes = max(runMinutes - 5, 5);
}
updateDisplay();
}
lastClkState = currentClkState;
if (digitalRead(swPin) == LOW && (millis() - lastButtonPress > 400)) {
lastButtonPress = millis();
menuMode++;
if (menuMode > 2) menuMode = 0;
updateDisplay();
if (menuMode == 0 && !isRunning) { // Cycled through GO back to 0
// Do nothing, just cycle menu
} else if (menuMode == 2) {
// START triggered from GO!
delay(200);
startSequence();
}
}
}
void startSequence() {
targetStepsPerSec = (float(drumRPM) * totalRatio * motorStepsPerRev) / 60.0;
stepper.setMaxSpeed(targetStepsPerSec);
stepper.setAcceleration(2000); // add acceleration back
stepper.move(1000000000); // large move target
lcd.clear();
lcd.setCursor(0,0); lcd.print("RUNNING...");
lcd.setCursor(0,1); lcd.print("DRUM RPM: "); lcd.print(drumRPM);
digitalWrite(enablePin, LOW);
isRunning = true;
startTime = millis();
lastButtonPress = millis();
}
void handleMotorLogic() {
// Stop button
if (digitalRead(swPin) == LOW && (millis() - lastButtonPress > 800)) {
lastButtonPress = millis();
finishStop();
return;
}
// Timer stop
if ((millis() - startTime) / 60000 >= (unsigned long)runMinutes) {
finishStop();
return;
}
stepper.run(); // constant speed mode
}
void initiateStop() {
isStopping = true;
stepper.setSpeed(stepper.speed() * 0.5);
}
void finishStop() {
stepper.setSpeed(0);
digitalWrite(enablePin, HIGH);
isRunning = false;
isStopping = false;
menuMode = 0;
lcd.clear();
lcd.setCursor(2, 0); lcd.print("CYCLE DONE!");
delay(1000);
updateDisplay();
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0); lcd.print(menuMode == 0 ? ">Drum RPM: " : " Drum RPM: "); lcd.print(drumRPM);
lcd.setCursor(0, 1); lcd.print(menuMode == 1 ? ">Runtime: " : " Runtime: "); lcd.print(runMinutes); lcd.print("m");
if (menuMode == 2) { lcd.setCursor(11, 1); lcd.print(">GO!"); }
}