Okay, I'm trying to make a arduino work with my computer to make some computer controlled machining. So far, the serial computer->Arduino seems to work fine, but Arduino->Computer results in repeats of the "DONE" in the serial monitor.
Like so:
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
I'm completely baffled even after checking the code repeatedly, perhaps the more experienced users of this forum can help me? Here's the code.
EDIT: Well uh... I just noticed my error, I forgot serial is async therefore I should have waited (using "while" instead of "if") for it to finish receiving before beginning the machining.
Might as well give this code out since it's already here:
#include <Stepper.h>
Stepper xAxisMotor(200,10,12,11,13);
Stepper yAxisMotor(200,6,8,7,9);
Stepper zAxisMotor(200,2,4,3,5);
int unisonSpeed = 100;
unsigned int xTargetSpeed = 100;
int xTargetSteps = 0;
unsigned int yTargetSpeed = 100;
int yTargetSteps = 0;
unsigned int zTargetSpeed = 100;
int zTargetSteps = 0;
boolean beginMove = false;
enum {
move_solo, //Move axis by axis
move_xyunison, //Move Z axis first then the XY axis
move_unison, //Move all motors at the same time.
};
int moveMode = move_unison;
void setup() {
// initialize the serial port:
Serial.begin(9600);
//Reset all axis on startup
}
void loop() {
// read from port 1, send to port 0:
getSerialData();
/*
while(Serial.available() >0) {
//Insert code to set stuff.
//xTargetSteps = data;
xTargetSpeed = Serial.parseInt();
Serial.readStringUntil(',');
xTargetSteps = Serial.parseInt();
Serial.readStringUntil(',');
yTargetSpeed = Serial.parseInt();
Serial.readStringUntil(',');
yTargetSteps = Serial.parseInt();
Serial.readStringUntil(',');
zTargetSpeed = Serial.parseInt();
Serial.readStringUntil(',');
zTargetSteps = Serial.parseInt();
}*/
xAxisMotor.setSpeed(xTargetSpeed);
yAxisMotor.setSpeed(yTargetSpeed);
zAxisMotor.setSpeed(zTargetSpeed);
if(beginMove)
{
beginMove = false;
switch(moveMode)
{
case move_unison:
moveUison();
break;
case move_xyunison:
moveXYUison();
break;
case move_solo:
moveSolo();
break;
}
if(beginMove == false || Serial.available() <= 0)
{
Serial.println("DONE");
}
}
}
String cReadString, substring;
int loc,locend;
void moveUison()
{
while(xTargetSteps != 0 || yTargetSteps != 0 || zTargetSteps != 0)
{
if(xTargetSteps > 0)
{
xAxisMotor.step(1);
xTargetSteps--;
}
if(xTargetSteps < 0)
{
xAxisMotor.step(-1);
xTargetSteps++;
}
if(yTargetSteps > 0)
{
yAxisMotor.step(1);
yTargetSteps--;
}
if(yTargetSteps < 0)
{
yAxisMotor.step(-1);
yTargetSteps++;
}
if(zTargetSteps > 0)
{
zAxisMotor.step(1);
zTargetSteps--;
}
if(zTargetSteps < 0)
{
zAxisMotor.step(-1);
zTargetSteps++;
}
delay(unisonSpeed / 1000);
}
}
void moveXYUison()
{
while(zTargetSteps != 0)
{
if(zTargetSteps > 0)//Yus, lazeh
{
zAxisMotor.step(1);
zTargetSteps--;
}
if(zTargetSteps < 0)
{
zAxisMotor.step(-1);
zTargetSteps++;
}
delay(zTargetSpeed / 1000);
}
while(xTargetSteps != 0 || yTargetSteps != 0 || zTargetSteps != 0)
{
if(xTargetSteps > 0)
{
xAxisMotor.step(1);
xTargetSteps--;
}
if(xTargetSteps < 0)
{
xAxisMotor.step(-1);
xTargetSteps++;
}
if(yTargetSteps > 0)
{
yAxisMotor.step(1);
yTargetSteps--;
}
if(yTargetSteps < 0)
{
yAxisMotor.step(-1);
yTargetSteps++;
}
delay(unisonSpeed / 1000);
}
}
void moveSolo()
{
while(zTargetSteps != 0)
{
if(zTargetSteps > 0)//Yus, lazeh
{
zAxisMotor.step(1);
zTargetSteps--;
}
if(zTargetSteps < 0)
{
zAxisMotor.step(-1);
zTargetSteps++;
}
delay(zTargetSpeed / 1000);
}
while(xTargetSteps != 0)
{
if(xTargetSteps > 0)//Yus, lazeh
{
xAxisMotor.step(1);
xTargetSteps--;
}
if(xTargetSteps < 0)
{
xAxisMotor.step(-1);
xTargetSteps++;
}
delay(xTargetSpeed / 1000);
}
while(yTargetSteps != 0)
{
if(yTargetSteps > 0)//Yus, lazeh
{
yAxisMotor.step(1);
yTargetSteps--;
}
if(yTargetSteps < 0)
{
yAxisMotor.step(-1);
yTargetSteps++;
}
delay(yTargetSpeed / 1000);
}
}
void getSerialData()
{
while(Serial.available() > 0) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == '\n') {
//Serial.println(readString); //prints string to serial port out
//do stuff
loc = cReadString.indexOf("XSPEED:");
locend = cReadString.indexOf("XSPEED;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+7, locend);
xTargetSpeed = substring.toInt();
}
loc = cReadString.indexOf("XSTEP:");
locend = cReadString.indexOf("XSTEP;");
if(loc != -1 || locend != -1)
{
substring = cReadString.substring(loc+6, locend);
xTargetSteps = substring.toInt();
}
loc = cReadString.indexOf("YSPEED:");
locend = cReadString.indexOf("YSPEED;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+7, locend);
yTargetSpeed = substring.toInt();
}
loc = cReadString.indexOf("YSTEP:");
locend = cReadString.indexOf("YSTEP;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+6, locend);
yTargetSteps = substring.toInt();
}
loc = cReadString.indexOf("ZSPEED:");
locend = cReadString.indexOf("ZSPEED;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+7, locend);
zTargetSpeed = substring.toInt();
}
loc = cReadString.indexOf("ZSTEP:");
locend = cReadString.indexOf("ZSTEP;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+6, locend);
zTargetSteps = substring.toInt();
}
loc = cReadString.indexOf("MODE:");
locend = cReadString.indexOf("MODE;");
if(loc != -1 || locend != -1)
{
//Serial.println(loc);
substring = cReadString.substring(loc+5, locend);
switch(substring.toInt())
{
case 0:
moveMode = move_solo;
break;
case 1:
moveMode = move_xyunison;
break;
case 2:
moveMode = move_unison;
break;
}
zTargetSteps = substring.toInt();
}
cReadString=""; //clears variable for new input
substring="";
beginMove = true;
}
else {
cReadString += c; //makes the string readString
}
}
}