[PROBLEM] String

Hello guys ,

I’m gonna try to explain this as good as possible.

First of all thanks for responding and listening.

I’m making an remote control car that I can move with only one analog joystick.
I know there is an arduino kit for that ( RC CAR) well basically I’m trying to replicate that with my own stuff.

So far I made the transmitter side , I made a PCB and wrote the software with FlowCode ( it works perfect).

Picture : LOOK ATTACHMENT EXAMPLE

Then I decided to make the receiver side with arduino UNO/Genuino (starter kit)

So I have the following problem :

If you look at the image in attachment you’ll see that I sent char 22 wich is a SYNC

The I sent the following letter ( depends on wich direction the analog stick is moved ) and atleast a numberic value wich is convert to a string. For example if I move the analog stick forward it will send : V115 , the numeric value depends how far I move the joystick forward. Same for backwards wich is “A”. And left is “L” and right is “R”

At the end I sent A carriage return and line feed.

What i’m trying to do know is to split the string I receive for example : V + 115
So It know that “V” is the direction which the motor shoud move in and the value “115” it should set on the motor ( PWM )

NOTE :I’m using 2 DC motors

But the arduino COM3 is doing weird things and don’t do exactly I say somtimes it enters where it shouldn’t and sometimes it just doesn’t enter I don’t know it has a life of his own as it seems.

I just simply need to split the string and the numberic value sent as PWM to motor.

COM3 image in attachment

The Code :

#define E1 10  // Enable Pin for motor 1
#define I1 7  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1

#include"stdlib.h"
#include"ctype.h"


boolean stringComplete = false;
String inputString = "";
String BeepString = "";

void setup() {
  // initialize serial:
  Serial.begin(2400);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(E1, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);

}

void loop() {

  //const String temp = inputString;
  String convert = "";
  for(unsigned int i=0; i < inputString.length(); i++){
    if(  isdigit(inputString[i]) ) convert += inputString[i];
  }
  Serial.println(inputString);
  //Serial.println("Orginal at beginning="+temp);
  
  if (stringComplete) {
    //Serial.println(inputString);
    //Serial.println(&inputString[1]);
    // clear the string
    inputString = "";
    stringComplete = false;
  }

/*  for(unsigned int i=0; i < inputString.length(); i++){
    Serial.print(inputString[i]);
    Serial.print("  ");
  }
  */
  //Serial.println(inputString.length());


  if (inputString.startsWith("BEEP", 1)) {
    tone(8, 1000);
    delay(100);
    noTone(8);

    inputString = "";
  }


  if (inputString.startsWith("V", 1)) {
    /*String convert = "";
    Serial.println("TEMP="+temp);
    for(unsigned int i=0; i < temp.length(); i++){
      if(  isdigit(temp[i]) ) convert += temp[i];
    }
    Serial.println("Converted="+convert);
    Serial.println("\n\n");
    */
    //Serial.println(atoi(convert.c_str()));
    
    //int Snelheid =  atoi(&inputString[1]);
    //int Snelheid =  atoi(convert.c_str());
    //int Snelheid =  inputString.toInt();
    int Snelheid =  convert.toInt();
    Serial.println(Snelheid);
    Snelheid *= 2;
    if ( Snelheid > 255 ) Snelheid = 255;
    digitalWrite(2, HIGH);
    digitalWrite(2, LOW);
    analogWrite(E1, Snelheid);
    digitalWrite(7, HIGH);
    digitalWrite(9, LOW);
    inputString = "";
  }

  if (inputString.startsWith("A", 1)) {
    digitalWrite(3, HIGH);
    digitalWrite(3, LOW);
    inputString = "";
  }

  if (inputString.startsWith("L", 1)) {
    digitalWrite(4, HIGH);
    digitalWrite(4, LOW);
    inputString = "";
  }

  if (inputString.startsWith("R", 1)) {
    digitalWrite(5, HIGH);
    digitalWrite(5, LOW);
    inputString = "";
  }
}

void serialEvent() {
  int charsRead;
  while (Serial.available()) {

    char inChar = (char)Serial.read();

    inputString += inChar;

    if (inChar == '\n') {
      stringComplete = true;
      //inputString[inChar] = '\0';
      //inputString = inputString.c_str();
    }
  }
}

Hello, try to re-load the bootloader I don't know why but in some cases when you use the serial function the board, specifically arduino uno had problem´s, try this in some cases i had this problem, probably the usb cable had a bad connection or something.
or Check your communication program

I suspect that your problem is that you're using two 'independent' pieces of code; loop() and serialEvent().

In loop(), you clear inputString when a message was completely received, but it might very well be possible that there was already some new data in there that is now lost. This can happen if commands follow each other very quickly.

Have a look at Robin2's Serial Input Basics - updated thread to get some ideas how to approach receiving serial data without using serialEvent and without using the String (capital S) class.

Use of the String class might leave holes in your memory where you can hide an elephant.