hello,
i am in the process of making a home made CNC that prints with clay i am trying to control three stepper motors and two solenoids with an Arduino mega, two stepper drivers and a ULN 2004. for the steppers i am using the accel stepper library which has worked great. however i am not sure what i am doing wrong with the serial communication aspect. i am trying to create a 5 character array with information from the serial port ( line number,X position,Y Position,Zposition, and solenoid state. any help would be much aprecieated
thankyou
here is my code attempt
#include <AccelStepper.h>
// Define some steppers and the #, pins the will use
AccelStepper stepperx(2,32,30); // Defaults to 4 pins on 2, 3, 4, 5
AccelStepper steppery(2,36,34);
AccelStepper stepperz(4,13, 11, 10, 11);
// Create a list of integers
// For each of the four string inputs
int resetPinx=31;
int enablePinx=33;
int resetPiny=35;
int enablePiny=37;
int openPin=8;
int closePin=9;
//int fanPin=
int XSteps, YSteps, ZSteps, SolState;
void setup()
{
Serial.begin(115200);// opens serial port, sets data rate to 115200 bps
stepperx.setMaxSpeed(300.0);
stepperx.moveTo(0);
steppery.setMaxSpeed(300.0);
steppery.moveTo(0);
stepperz.setMaxSpeed(300.0);
stepperz.moveTo(0);
pinMode(enablePinx,OUTPUT);
pinMode(enablePiny,OUTPUT);
pinMode(resetPinx,OUTPUT);
pinMode(resetPiny,OUTPUT);
pinMode(openPin,OUTPUT);
pinMode(closePin,OUTPUT);
//pinMode(fanPin,OUTPUT)
}
void loop()
{
// Create a list of integers
// For each of the four string input
while(Serial.available()>0)
{
for (int i = 0; i < 4; i++)
{
// Create a temporary string buffer; remember
// that there are no “strings” in this language, but
// you can instead use character-arrays which
// are about the same thing
// PLEASE NOTE THAT THE MAXIMUM LENGTH IS 31 CHARACTERS!!!
// The last character (the 32nd, which is at index 31)
// is the “null terminator” so that we know where the end
// of the string is:
char TemporaryString[32];
// Read the current string from the input until we see
// a space character
int Index = 0;
while(true)
{
// Read single character
TemporaryString[Index] = Serial.read();
// If we see a space, that means we are done with
// this value; cap the string with a null-terminator
if(TemporaryString[Index] == ’ ')
{
TemporaryString[Index] = ‘\0’;
break;
}
// Else, we are not at the end, keep
// growing the index so we can place the next char
else
Index++;
}
// Conver the string into an integer
int Value = atoi(TemporaryString);
// Place the value into the correct list
if(i == 0)
XSteps = Value;
else if(i == 1)
YSteps = Value;
else if(i == 2)
ZSteps = Value;
else if(i == 3)
SolState = Value;
}}
// Retrieve the relative stepper positions from the index
stepperx.setSpeed(XSteps);
stepperx.moveTo(XSteps);
steppery.setSpeed(YSteps);
steppery.moveTo(YSteps);
stepperz.setSpeed(ZSteps);
stepperz.moveTo(ZSteps);
digitalWrite(enablePinx,LOW);
digitalWrite(enablePiny,LOW);
digitalWrite(resetPinx,HIGH);
digitalWrite(resetPiny,HIGH);
if(SolState == 1)
{
digitalWrite(openPin,HIGH);
digitalWrite(closePin,LOW);
}
if(SolState == 0)
{
digitalWrite(openPin,LOW);
digitalWrite(closePin,HIGH);
}
//digitalWrite(fanPin,OUTPUT)
//run the stepper motors
stepperx.run();
steppery.run();
stepperz.run();
Serial.println(‘nextline’); //print line number to the serial
//port to tell grasshopper to
//send the next line of code
}