//do not edit this code it self, make a copy with your name and edit that version sp we don't lose track
// ALi, 15/12/2023
#include <SD.h>
#include <AccelStepper.h>
// pin setup
const int cspin = 53; //SPI BUS initializaion (mega 50-51-52) other (10)
//the X&Y coordinates stuff
const int numoline = 8;
float Xco[numoline] = {0};
float Yco[numoline] = {0};
int currentline = 0;
//motors pins & variables
const int stepsPerRevolution = 350;
const int xpul = 9;
const int xdir = 8;
const int ypul = 10;
const int ydir = 11;
float Xspeed = 50;
int Xacc = 100;
float Yspeed = 50;
int Yacc = 100;
AccelStepper stepperX(1, xpul, xdir);
AccelStepper stepperY(1, ypul, ydir);
//other variables
File Chalkzila;
File root;
void setup() {
pinMode(xpul, OUTPUT);
pinMode(xdir, OUTPUT);
pinMode(ypul, OUTPUT);
pinMode(ydir, OUTPUT);
float Xspeed = 100;
int Xacc = 1000;
float Yspeed = 100;
int Yacc = 1000;
stepperX.setMaxSpeed(Xspeed);
stepperX.setAcceleration(Xacc);
stepperY.setMaxSpeed(Yspeed);
stepperY.setAcceleration(Yacc);
stepperX.moveTo(2);
while (stepperX.distanceToGo() != 0)
stepperX.run();
stepperY.moveTo(0);
while (stepperY.distanceToGo() != 0)
stepperY.run();
// //the serial and SD stuff
Serial.begin(9600);
while (!Serial){};
Serial.println("Card initialization Started!"); //starting to look for the SD
if (!SD.begin(cspin)) {
Serial.println("Dude, where's the SD!");
return;}
else Serial.println("I see your SD");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0); //calling the print directory function
Serial.println("");
Chalkzila = SD.open("Chalkz.txt");
if (Chalkzila) {
Serial.println("Chalkz.txt: ");
while (Chalkzila.available()) {
Serial.write(Chalkzila.read());}
Chalkzila.close();}
else {Serial.println("error opening Chalkzila.txt!");}
Chalkzila = SD.open("Chalkz.txt");
while (Chalkzila.available()) { //break into lines and call the processLine function
String line = Chalkzila.readStringUntil('\n');
Serial.println("I'm sending : " + line);
processLine(line);}
Serial.println("The values of X are: ");
for (int i = 0; i < numoline; i++) {
Serial.print(i+1);
Serial.print(" ");
Serial.println(Xco[i]); }
Serial.println("The values of Y are: ");
for (int i = 0; i < numoline ; i++) {
Serial.print(i+1);
Serial.print(" ");
Serial.println(Yco[i]);}
} // end of setup
void processLine(String line){
int xPos = line.indexOf('X');
int yPos = line.indexOf('Y');
int zPos = line.indexOf('Z');
String xSubstring = line.substring(xPos + 1, yPos);
String ySubstring = line.substring(yPos + 1, zPos);
String zSubstring = line.substring(zPos + 1);
float x = xSubstring.toFloat();
float y = ySubstring.toFloat();
float z = zSubstring.toFloat();
Serial.print("I've done : ");
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
Xco[currentline] = x;
Yco[currentline] = y;
currentline = currentline+1;
} //end of processLine function
void loop() {
float newYspeed = 50;
float newXspeed = 50;
int currentline2 = 1;
float speedfactor = 0;
// the core movement
while (currentline2 < numoline){
stepperX.moveTo(Xco[currentline2]);
stepperY.moveTo(Yco[currentline2]);
//find the bigger one
float Xdistance = abs (Xco[currentline2]- Xco[currentline2-1]);
float Ydistance = abs (Yco[currentline2] - Yco[currentline2-1]);
Serial.print("X , Y distances are ");Serial.print(Xdistance);Serial.print(" , ");Serial.println(Ydistance);
if(Ydistance < Xdistance){
Serial.println("I see that X is greater than Y");
if (Xdistance != 0)
speedfactor = Ydistance/Xdistance;
else
speedfactor = 1;
Serial.print("The speed factor is : ");Serial.println(speedfactor);
newYspeed = speedfactor*50; Serial.print(" New Y speed is : ");Serial.println(newYspeed);
newXspeed = 50;
delay(10);}
else if(Xdistance < Ydistance){
Serial.println("I see that Y is greater than X");
if (Ydistance != 0)
speedfactor = Xdistance/Ydistance;
else
speedfactor = 1;
Serial.print("The speed factor is : ");Serial.println(speedfactor);
newYspeed = 50;
newXspeed = speedfactor*50;Serial.print(" New X speed is : ");Serial.println(newXspeed);
delay(10);}
else if(Xdistance == Ydistance){
Serial.println("I see that X and Y are the same");
speedfactor = 1 ;
Serial.print("The speed factor is : ");Serial.println(speedfactor);
newYspeed = 50;
newXspeed = 50;
delay(10);
}
stepperX.setMaxSpeed(newXspeed);
stepperY.setMaxSpeed(newYspeed);
Serial.print("the new speeds are : ");Serial.print(newXspeed); Serial.print(" , ");Serial.println(newYspeed);
while (stepperX.distanceToGo()!=0 || stepperY.distanceToGo()!=0){
if(stepperX.distanceToGo()!=0)
stepperX.run();
if (stepperY.distanceToGo()!=0)
stepperY.run();
delay(11);
}
currentline2++;
Serial.print(" line num "); Serial.print(currentline2);Serial.println(" Finished ");
Serial.print("X speed was "); Serial.print(newXspeed);Serial.print(" Y speed was ");Serial.println(newYspeed);
}
stepperX.moveTo(2);
while (stepperX.distanceToGo() != 0)
stepperX.run();
stepperY.moveTo(0);
while (stepperY.distanceToGo() != 0)
stepperY.run();
delay(50000);
} //end of void loop
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}