Replacing String objects with C strings

Thanks, Paul. I've indexed the array and its working better. I've tried to get an idea of what is in the readSting array by using a FOR loop to print each character and where it is in the array. Just the initialization strings sent from the control board after power up eats up 43 array spots. I copied a portion of the Serial Monitor output showing where I hit the limit switch. The "L,16392" in the readString array seems to be chopped into spaces and some other garbage characters. So, I either need to filter the characters coming into the array so I don't get the garbage. Or, I have to somehow modify my strcmp() function to start looking at the "L" character and then read the array from there. Maybe strbrk() would do this? Any recommendations?

SERIAL MONITOR OUTPUT:

? ---> Character 0

---> Character 1
? ---> Character 2

---> Character 3
? ---> Character 4

---> Character 5
L ---> Character 6
, ---> Character 7
0 ---> Character 8

---> Character 9

---> Character 10

  • ---> Character 11
    ?
    ?
    ?
    L,0
  • --> Read String
    Strings not Equal
    -30000XG --> IF LOOP
    30000XG --> IF LOOP
    -30000XG --> IF LOOP
    30000XG --> IF LOOP
    -30000XG --> IF LOOP

---> Character 0
? ---> Character 1

---> Character 2
L ---> Character 3
, ---> Character 4
1 ---> Character 5
6 ---> Character 6
3 ---> Character 7
9 ---> Character 8
2 ---> Character 9

---> Character 10

---> Character 11

  • ---> Character 12

?
L,16392

  • --> Read String
    Strings not Equal
    30000XG --> IF LOOP
    -30000XG --> IF LOOP
    30000XG --> IF LOOP
    -30000XG --> IF LOOP
/*
CNC Bed Sketch 

This program drives a Peter Norburg SD4DEU Stepper Driver board running SD4DNCRouter firmware connecting at 9600 Baud.
 
 
*/

//******************Constants***********
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

char homePosition[5] = "0XYG";



//******************Variables***********

char readString[70];
char c;
int i = 0;
int commandNumber = 0;
int numCommands = 0;

//****************Function Prototypes******
long convertToSteps(int inches);

//***************Setup******************************
void setup() {
  delay(1000);
  
  //readString[0] = '\0';    //Add a null termination to readString
  
  Serial.begin(9600);    //Begin serial communication at 9600
  mySerial.begin(9600);  //Set software serial baud rate
  
  randomSeed(analogRead(0)); 
  
  // Initialize Stepperboard Values
  mySerial.write("!");      //reset Stepper Board
  delay(50);
  mySerial.write("1500R");  //Set Run Rate
  delay(50);
  mySerial.write("800P");  //Set Ramp Rate
  delay(1000);
}

//*************Main Program Loop************************
void loop() {
 
 
 char* stepCommand[] = {    //Load up CNC Patterns into string array
 "30000XG", 
 "-30000XG"
  }; 
 
 int numCommands = (sizeof(stepCommand)/sizeof(char*)); //Calculate array size

 while(mySerial.available()){
   c = mySerial.read();   //get one byte from buffer 
   readString[i++] = c;    //increment array (original array index is returned)
   readString[i] = '\0';    //Add NULL to next index
   //i=i+1;
   //Serial.print(c);
   //Serial.println(" --> c charachter byte");
   if(readString[i-1]== '*'){
     mySerial.write(stepCommand[commandNumber]);
     Serial.print(stepCommand[commandNumber]);
     Serial.println(" --> IF LOOP");
     commandNumber++;
     if(commandNumber == numCommands){    //if the max number of commands is reached...
        commandNumber = 0;                //reset command number back to zero
     }  
     break;
   } 
 }

 mySerial.write("L");
  
 if(strlen(readString) > 0){
   for(int j=0; j < strlen(readString); j++){
     Serial.print(readString[j]);
     Serial.print(" ---> Character ");
     Serial.println(j);
   }
   Serial.print(readString);
   Serial.println(" --> Read String");
   char response[] = {'L', ',','1', '6', '3', '9', '2',' ','\0'};
   if(strcmp(readString, response) == 0){
     Serial.println("Write Q Here");
   }
   else{
     Serial.println("Strings not Equal");
   }
   readString[0] = '\0';      //clear the array
 }  
}
//*********************FUNCTIONS***************************
long convertToSteps(int inches){
  long steps = 130435 * inches;  //there are 130435 steps in 1-inch
  return(steps);
}
//**************************************************************************