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:
}