Hello guys,
I am having troubles getting my code to work. It is parsing the data properly, but it does not seem to be using it for the motorLocations section. The printData section is working, printing out "2" if I send it <02>, but it does not print "Moving to Position 2" which leads me to believe it would not run the motors if I had them connected. Apologies for the excessive commenting, it helps me to keep track as I am very new to this.
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper stepper1(1,8,9); // stepper1, Pulse Pin 8, Direction Pin 9
AccelStepper stepper2(1,4,5); // stepper2, Pulse Pin 4, Direction Pin 5
bool newData = false;
bool runallowed = false;
char receivedCommand;
long CoordinateX = 0;
long CoordinateY = 0;
//===========
const byte numChars = 32; //number of characters set to 32, constant which means read only basically
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
char messageFromPC[numChars] = {0}; // variables to hold the parsed data
int bottleNum = 0; //defining first input variable for location
//===========
void setup()
{
Serial.begin(9600); //baud rate for serial monitor
Serial.println("Motor Position Testing"); //print this message on open
Serial.println("Positions available: 1-9"); // ^
Serial.println("Syntax <01>");
stepper1.setMaxSpeed(1600); //Max speed steps/second
stepper1.setAcceleration(800); //Acceleration steps/second/second
stepper2.setMaxSpeed(1600); //Max speed steps/second
stepper2.setAcceleration(800); //Acceleration steps/second/second
stepper1.disableOutputs(); //Keeps outputs disabled when not in use
stepper2.disableOutputs(); //Keeps outputs disabled when not in use
}
//==============
void loop()
{
recieveData(); //void command defined later
if (newData == true) {
strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data because strtok() used in parseData() replaces the commas with \0
parseData(); //void command defined later
printData(); //prints parsed bottle location
newData = false; //setting new data bool back to false at end of loop
}
motorLocations(); //tells the motor where to go and returns message
runMotor(); //enables or disables motor outputs
}
//=============
void recieveData() { //command for recieving data and identifying start and end markers
static boolean recvInProgress = false; //static is visible to only one function, defining this static bool as false by default
static byte owo = 0; //static as above, but for number of characters
char startMarker = '<'; //defining start marker
char endMarker = '>'; //defining end marker
char uwu; //defining variable uwu as a character value
while (Serial.available() > 0 && newData == false) { //while serial monitor is unavailable, i.e getting inputs between markers
uwu = Serial.read(); //set input to variable uwu
if (recvInProgress == true) { //if we are currently recieving data
if (uwu != endMarker) { //if the character is not an end marker
receivedChars[owo] = uwu; //Set number of characters to static byte
owo++; //add one character to static byte
if (owo >= numChars) { //if static byte is the same or greater than contant byte
owo = numChars - 1; //static byte becomes constant byte
}
}
else {
receivedChars[owo] = '\0'; // terminate the string
recvInProgress = false; //set recieve in progress to false
owo = 0; //set static byte back to 0
newData = true; //set new data to true
}
}
else if (uwu == startMarker) { //set character value to start marker
recvInProgress = true; //set recieve in progress to true
}
}
}
//=============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part
bottleNum = atoi(strtokIndx); // convert it to an integer
}
//============
void runMotor() //enables or disable motor outputs
{
if (runallowed == true)
{
stepper1.enableOutputs(); //enable output pins if run is allowed
stepper1.run();
stepper2.enableOutputs(); //enable output pins if run is allowed
stepper2.run();
}
else
{
stepper1.disableOutputs(); //disable output pins if run is not allowed
stepper2.disableOutputs(); //disable output pins if run is not allowed
return;
}
}
void motorLocations() //check for recieved commands on the serial
{
if (Serial.available() > 0) //if something comes from the computer
{
newData = true; //indicate that there is a new data by setting this bool to true
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
{
switch (bottleNum) //we check what is the command
{
case '1': //Position 1 for the motors
Serial.println("Moving to Position 1"); //print text before moving
CoordinateX = 1000;
CoordinateY = 1000;
MovePosition(); //run the command for moving to a position
break;
//deleted identical cases 2-8 for this post
default:
break;
}
}
newData = false;
}
}
//==========
void MovePosition()
{
runallowed = true; //enable outputs
stepper1.moveTo(CoordinateX); //move large motor to coordinates defined in the serial cases
stepper2.moveTo(CoordinateY); //move small motor to coordinates defined in the serial cases
}
//==========
void printData() {
Serial.print("Recieved Bottle Number: ");
Serial.println(bottleNum);
}
I am not getting any errors.
Thanks in advance for your help!