Clear the string

Hello guys !

I need to clear my string I tried putting serial.read() on several locations or '""' (putting it blank ). But the first character I sent my program remembers , it never changes when I sent other characters

For Example : I sent V,123 and then A,123 It will always return me the first character I got wich in this example is "V". For the axis this is no problem there it does overwrite the old axis value.

So it must be a clear buffer problem ... How do I solve this ?

Using :

Arduino uno

#define E1 10  // Enable Pin for motor 1
#define E2 11  // Enable Pin for motor 2

#define I1 8  // Control pin 1 for motor 1 for L293D it changes the direction of the motor
#define I2 9  // Control pin 2 for motor 1 for L293D it changes the direction of the motor
#define I3 12  // Control pin 1 for motor 2 for L293D it changes the direction of the motor
#define I4 13  // Control pin 2 for motor 2 for L293D it changes the direction of the motor


String Richting;  // Direction
int Axis;                 // Numbers (the speed I want to set on the motors)
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

void setup() {

  Serial.begin(2400);
  pinMode(13, OUTPUT);
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
  Serial.println("<Arduino is ready>");
}

void loop() {
  if (recievedMessage())
  {
    parseMessage();
    Calculation();
    showNewMessage();
  }
}

bool recievedMessage() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;


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

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

void showNewMessage() {
  if (newData == true) {
    Serial.println(Richting);
    newData = false;

  }
}

void parseMessage()
{
  strtok(receivedChars, ",");
  Axis = atoi(strtok(NULL, ","));
}

void Calculation()
{
  if (Richting = "A") {
    analogWrite(E1, Axis * 2);
    analogWrite(E2, Axis * 2);
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);
  }

}

hello @MrKrabs ;D

you moved the assignment of the direction (Richting) into the recievedMessage() function... why?

you seem to have refactored your code beyond that as well....

if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      Richting = receivedChars[1];
      if (ndx >= numChars) {
        ndx = numChars - 1;  // ????????????
      }
    }

EDIT: @Robin2... he's using your global examples....

MrKrabs:
Hello guys !

I need to clear my string I tried putting serial.read() on several locations or '""' (putting it blank ). But the first character I sent my program remembers , it never changes when I sent other characters

For Example : I sent V,123 and then A,123 It will always return me the first character I got wich in this example is "V". For the axis this is no problem there it does overwrite the old axis value.

So it must be a clear buffer problem ... How do I solve this ?

This will zero-out/clear the whole array to zero-bytes:

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

After doing so, then length of the string is 0 with a finalizing '\0' character (nullterminated string with zero length).In fact, ALL positions of the array will contain zero-bytes.

Hello BulldogLowell !

No I didn't change that part of the code this is actually raw copy and pasted from Robin go check it out it's true ;).

Are you sugesting I shouldn't use global variables ??

I never mind I see what you mean , you are talking about richting my bad ... Yes Because before every full string I sent char 22 wich is a sync if you look in the ascii-table. So a full string would be char 22 + direction + ","+axis+char 13 +char 10 that's why.

if (Richting = "A") {

             ^
-------------|

Weren't you already warned about this in a previous thread?

arduarn:
Weren't you already warned about this in a previous thread?

LOL, and these:

line 10:   String Richting;  // Direction
line 19:   pinMode(13, OUTPUT);
line 51:   Richting = receivedChars[1];
line 70:   Serial.println(Richting);
line 84:   if (Richting="V"){

I even provided the complete sketch:

#define E1 10  // Enable Pin for motor 1
#define E2 11  // Enable Pin for motor 2

#define I1 8  // Control pin 1 for motor 1 for L293D it changes the direction of the motor
#define I2 9  // Control pin 2 for motor 1 for L293D it changes the direction of the motor
#define I3 12  // Control pin 1 for motor 2 for L293D it changes the direction of the motor
#define I4 13  // Control pin 2 for motor 2 for L293D it changes the direction of the motor


int Axis;                 // Numbers (the speed I want to set on the motors)
const byte numChars = 75;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

void setup() {

  Serial.begin(2400);
  // pinMode(13, OUTPUT); <-- This is the same as I4!
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
  Serial.println("<Arduino is ready>");
}

void loop() {
  if (recievedMessage())
  {
    parseMessage();
    Calculation();
    showNewMessage();
  }
}

bool recievedMessage() {
  static byte ndx = 0;

  while (Serial.available()) {
    char rc = Serial.read();

    if (rc >= ' ') { // Not a CR orLF

      if (ndx < numChars) {  // if there's room...
        receivedChars[ndx] = rc;  //  ... save another char
        ndx++;
      }

    } else if (ndx > 0) { // not an empty string

      receivedChars[ndx] = '\0'; // terminate the string
      ndx     = 0;
      newData = true;
      return true;

    }
  }
  return false;
}

void showNewMessage() {
  if (newData) {

    Serial.print("De richting=");
    Serial.println(receivedChars);
    Serial.print("De snelheid=");
    Serial.println(Axis);
    newData = false;

  }
}

void parseMessage()
{
  strtok(receivedChars, ",");
  Axis = atoi(strtok(NULL, ","));
}

void Calculation()
{
  if ( strcmp( receivedChars, "V" ) == 0 ){
    digitalWrite(I4,HIGH); // <-- use I4, not 13
    analogWrite(E1,255);
  }
}

But he said he didn't need it because "it just worked suddenly."

Oh, well.