[SOLVED] How reset a char array?

Hello,

I'm working on a project that uses a RPi for a ui to and Arduino controlling a motor. I had it working fine assigning commands from the RPi to Stings for the Arudino to execute on. However, thanks to this forum, I've learned that Strings aren't ideal so I'm trying to use char arrays. I've stripped my code to a working example of where I'm running into and issue. I'm using the "Serial Input Basics" example that was posted here to listen for instructions from the RPi. Once the instruction is parsed, my loop determines what to do. In this example below, I'm sending "<HOME,1.375>". As you'll see in the code, I'm merely trying to tell the arduino when it finds the rpiCommand "HOME", to print "I'm going home", then I want it to set the rpiCommand to nothing so it wont match anymore. It seems to be clearing out rpiCommand, but it continues to match. Can someone tell me what I'm doing wrong please.

//+++++CODE SPECIFIC VARIABLES+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Parameters used in the code. Will be changed later.
byte MotorState = 0;     //Motor State options are: DISABLED, HOMING, MOVING, IDLING
#define DISABLED 0
#define HOMING   1
#define MOVING   2
#define IDLING   3


// Instructions from RPi
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
// variables to hold the parsed data
char rpiCommand[numChars] = {
  0};
double inputValue = 0.0;

boolean newData = false;

//============

void setup() {
  Serial.begin(9600);
  //Setup Pins as outputs, HLFB as input, set GND to LOW for Ground.

}

//============

void loop() {

  //Listen for commands from RPi
  recvWithStartEndMarkers();
  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();
    showParsedData();
    newData = false;
  }

  switch (MotorState) {
  case DISABLED: 
    {     
      if ( strcmp(rpiCommand, "HOME") == 0 ) {
        Serial.println("I'm going home");
        char rpiCommand[numChars] = "";
        Serial.println(rpiCommand);
      }        
      break;
    }

  }   

}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = 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 - the string
  strcpy(rpiCommand, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ",");
  inputValue = atof(strtokIndx);     // convert this part to a double

}

//============

void showParsedData() {
  Serial.print("command ");
  Serial.println(rpiCommand);
  Serial.print("input ");
  Serial.println(inputValue,4);
}
3 Likes

To reset a cstring simply set the value of the first index to 0 (or '\0').

Now, this code:
strtokIndx = strtok(tempChars,","); (1)
~~ strcpy(rpiCommand, strtokIndx); (2)~~
Assuming tempChars holds: "HOME,1.375", the value of rpiCommand after the (2) statement is "1.375" which does not have a "HOME" in it. Perhaps what you want to replace the (2) statement by:
strcpy(rpiCommand, tempChars);

nvm, your code is fine.

That was it. Thank you so much!

If you need to clear out the entire array, try:

memset(0, receivedChars, sizeof(receivedChars));

1 Like

Good to know. Thank you. For this, I really just needed it to not match the command so it wouldnt get stuck in a loop.

In any case, I've added all my code back in and everything is working great. I couldnt be happier. Thanks again, guys!