Hey Experts,
I'm trying to run my stepper motor using values stored in sd card. The problem is when i try to run this code the stepper motor stutters and takes some steps and slows down. I request you guys to help me!!!
The code is given below:
#include <SD.h>
#include <SPI.h>
#include <AccelStepper.h>
// Pins for the SD card module
const int chipSelect = 10;
// Pins for the stepper motor driver (DRV8825)
const int stepPin = 2;
const int dirPin = 3;
const int enablePin = 4;
// Stepper motor configuration
const int stepsPerRevolution = 200; // Change this value based on your motor
const float maxSpeed = 1000.0; // Maximum speed in steps per second
const float acceleration = 1000.0; // Acceleration in steps per second^2
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
Serial.begin(9600);
pinMode(enablePin, OUTPUT);
SD.begin(chipSelect);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
}
void loop() {
if (SD.exists("steps.txt")) {
File file = SD.open("steps.txt");
if (file) {
// Read the integer value from the file
int targetSteps = file.parseInt();
file.close();
// Move the stepper motor to the target position
stepper.moveTo(targetSteps);
// Run the stepper motor until it reaches the target position
while (stepper.distanceToGo() != 0) {
stepper.run();
}
}
}
// Wait for a short duration before checking the SD card again
delay(1000);
}