How to control a stepper motor with a text file on an SD card

Hey,

I'm having problems controling universal stepper motor. Im trying to get it moved as I want. I have a txt. file loaded on my SD card which I inserted in SD card - modul and connect that modul to my Arduino Uno R3. On a text file are two columns, one contains data of time and the other data of steps. So this file represents, at which time the stepper needs to make that many steps. So how many steps needs to be made by one specific time. The txt file looks like this:
Column of time:
t[s]
22.6074982
22.6099987
22.6124992
22.6149979
22.6174984
22.6199989
.
.
.

Column of steps:
steps
0.0
-1.0
-2.0
-3.0
-4.0
-5.0
.
.
.

I dont know how to write the code for this. I found something similar on the yt, but this one is for controling servo motors, so I copied it and change it a little bit, trying to set it for stepper motor, but it doesnt work, maybe someone will get the idea.

CODE FOR STEPPER MOTOR CONTROL:

// Include libraries
#include <SPI.h>
#include <SD.h>
#include <Stepper.h>

// CS pin for SD Card Module
const int chipSelect = 4;

// String to hold one line of text
String buffer;

//Create a Stepper object
Stepper mystepper(800, 2, 3);

void setup() {
// Open serial comunications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}

Serial.print("Initializing SD card...");
// See if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// dont do anything more:
while (1);
}
Serial.println("Card initialized.");

//Attach stepper on pin 2 to the stepper object
mystepper.attach(3);

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("koraki2.txt");

//If the file is available, read it
if (dataFile) {
while (dataFile.available()) {
// Write one line to buffer
buffer = dataFile.readStringUntil('\n');
// Print to serial monitor
Serial.println(buffer);
// Convert string to intiger and position stepper
mystepper.write(buffer.toInt());
delay(2501);
}
dataFile.close();
}
// if the file isnt open, pop up an error:
else {
Serial.println("error opening koraki2.txt");
}
}

void loop() {
// put your main code here, to run repeatedly:

}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

What do you see when you print the buffer ?

what do you mean?

In your code you have these statements

buffer = dataFile.readStringUntil('\n');
// Print to serial monitor
Serial.println(buffer);

what do you see on the Serial monitor ?

Nothing, becaus I have an error here:
mystepper.write(buffer.toInt());
error: Class stepper has no member named 'write'

You did not think that that was important enough to mention in the original post?

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Look at the examples in the Stepper library to see what the legal functions are and how to use them.

mystepper.write(buffer.toInt());

Did you perhaps mean to use the step() function ?

You have top understand more about two things:

a.) how a servo works
b.) how a steppermotor works

I'm unsure what this means

You posted numbers
t[s]
22.6099987

does this mean 22.6099987 seconds?

steps
-1.0

does this mean do

-1.0 steps in a time of 22.6099987 seconds?

-2.0 steps in a time of 22.6124992 seconds?

so some more questions

what does the minus-sign mean?

can it occur that you want to do
-301.9654 steps in a time of 0.00456 seconds?

Do you really need the precision of 22.6124992
which means a resolution of 100 nano-seconds?
or would 22.6 seconds be sufficient?

Where do these decimal numbers with the many many digits come from?

If you give an overview what your steppermotor is driving maybe much better suited solutions can be suggested.

best regards Stefan

https://forum.arduino.cc/t/how-to-control-a-stepper-motor-with-a-file-from-sd-card/1009855?u=primi12

here its explained better

Hey,
So I have a text file that is uploaded on my SD card which is inserted in SD card Modul and modul is connected to my arduino. To my arduino is also conected a stepper motor. How can I control the movements of my stepper with that txt file. How to write a code so the arduino will read the data in text file and use it to make the stepper move as I want. In the text file are values of time and numbers of steps. So that means by what time the stepper needs to make that many steps. So how many steps needs to be made by certain time. The file has two columns, one contains values of time, the other the values of steps. Looks like this:
t[s] - time in seconds
0
0,0025
0,0050
0,0075
0,0100

steps - numbers of steps
0
1
2
4
6

So when the time is 0s, the stepper doesnt move, when the 0,0025 seconds passes the stepper in that time needs to make 1 step. When the another 0,0025 seconds passes, the stepper needs to make in that time another stepp. When another 0,0025 seconds passes, so from the beginnig 0,0075 seconds has passed, the stepper needs to be at 4 steps. That means that from the beginnig it made 4 steps, but from the previous time (0,0050 seconds) it made 2 stepps.

Does anybody had an idea how to write a code about this?
Thanks

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Why did you start a new topic ?

I highly doubt that reading in the values will be as fast as 2,5 milliseconds.
You would have to code the whole thing based on microseconds

If the number of steps can change from line to line in an unpredictable way
live reading and stepping will be very hard.

It is about time that you give an overview about the whole project.
What is the final purpose of rotating the steppermotor in the described pattern?

I doubt that your approach is the best one.
But I can't really tell.

Which way of coding would be the best one depends on

  • the exact microcontroller you are using
  • the maximum number of steps stored on the SD-card.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.