hi every one I am trying to make simple code in arduino that read from SD card (x y z) and then use that data to control three stepper motor each one for axis this code do what I need but when the motors run it run in very low speed hover you increase the speed it stay in the same speed
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <SPI.h>
#include <SD.h>
File myFile;
String buffer;
String xa;
String ya;
String za;
String X;
String Y;
String Z;
String A;
String B;
AccelStepper xaxis(1, 5, 6);
AccelStepper yaxis(1, 9, 8);
AccelStepper zaxis(1, 2, 3);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
xaxis.setAcceleration(1000);
yaxis.setAcceleration(1000);
zaxis.setAcceleration(1000);
xaxis.setMaxSpeed(50);
yaxis.setMaxSpeed(50);
zaxis.setMaxSpeed(50);
}
void loop()
{
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!SD.begin(4)) {
return;
}
// open a new file and immediately close it:
myFile = SD.open("example.txt");
while(myFile.available()&&(xaxis.distanceToGo() == 0)){
buffer=myFile.readStringUntil('\r');
X="X";
Y="Y";
Z="Z";
A="A";
xa=buffer.substring(buffer.indexOf(X)+1,buffer.indexOf(Y));
ya=buffer.substring(buffer.indexOf(Y)+1,buffer.indexOf(Z));
za=buffer.substring(buffer.indexOf(Z)+1,buffer.indexOf(A));
xaxis.moveTo(xa.toInt());
yaxis.moveTo(ya.toInt());
zaxis.moveTo(za.toInt());
}
xaxis.run();
yaxis.run();
zaxis.run();
myFile.close();
}
xaxis.setMaxSpeed(50);
yaxis.setMaxSpeed(50);
zaxis.setMaxSpeed(50);
You set the speed in those lines and never change the speed in code. That is set to 1/4 revolution per second on a 200 step motor in single step mode. The bigger the number the faster the speed.
I change the number but nothing hapen same speed
I don't see it changed... Might be my crystal ball is stuck again. Can you help us by posting your code again after you make changes?
String buffer;
String xa;
String ya;
String za;
String X;
String Y;
String Z;
String A;
String B;
Dammmnnnnnnn, good luck with Swiss Cheese Memory!
I change the speed before you told that ok
I don’t see how it is ever going to get to the second line of your file, let alone the last line, if you re-open the file every time through loop(). Try this version to see if you get better results.
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <SPI.h>
#include <SD.h>
File myFile;
String buffer;
AccelStepper xaxis(1, 5, 6);
AccelStepper yaxis(1, 9, 8);
AccelStepper zaxis(1, 2, 3);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) ;
if (!SD.begin(4))
{
Serial.println(F("Failed to initialize SD card."));
while (true) ; // Hang
}
// open a file
myFile = SD.open("example.txt", FILE_READ);
if (!myFile)
{
Serial.println(F("Failed to open file."));
while (true) ; // Hang
}
xaxis.setAcceleration(1000);
yaxis.setAcceleration(1000);
zaxis.setAcceleration(1000);
xaxis.setMaxSpeed(5000);
yaxis.setMaxSpeed(5000);
zaxis.setMaxSpeed(5000);
}
void loop()
{
xaxis.run();
yaxis.run();
zaxis.run();
if (xaxis.distanceToGo() == 0 && yaxis.distanceToGo() == 0 && zaxis.distanceToGo() == 0)
{
// All motion is done. Read another command from the file, if we have not reached the end yet.
if (myFile.available())
{
String buffer = myFile.readStringUntil('\r');
String xa = buffer.substring(buffer.indexOf('X') + 1, buffer.indexOf('Y'));
String ya = buffer.substring(buffer.indexOf('Y') + 1, buffer.indexOf('Z'));
String za = buffer.substring(buffer.indexOf('Z') + 1, buffer.indexOf('A'));
xaxis.moveTo(xa.toInt());
yaxis.moveTo(ya.toInt());
zaxis.moveTo(za.toInt());
}
}
}