arduino UNO Serial port problems

Hello forum readers ,

I'm receiving data with a receiver on the RX pin of the arduino. I'm trying to make a car move according to wich value I send. So I made this program that works perfect.

Example if i'm sending : F,123 // the motors now they need to turn FORWARD and halfspeed.

But there is like a Serial error I don't understand , I've been reading articles and figuring out what it can be but there just don't seem to be a solution. YES I HAVE READ Robin2's Serial Input Basics - updated

So I receive my data correctly but it doesn't put the value I get to my motor. But when I unplug my receiver , reuplode the code and open the terminal and write the exact same string as I would receive IT DOES WORK !! I simply don't understand what i'm doing wrong or what I oversaw.

I'm really desperate to find a solution this forum is my last hope :).

Thank you for takig your time and reading this.
I'm trying to make a car move forward

#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
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 13  // Control pin 2 for motor 2

String inputString = "";
boolean stringComplete = false;

void setup() {
  // initialize serial:
  Serial.begin(2400);
  inputString.reserve(200);
    pinMode(E1, OUTPUT);
    pinMode(E2, OUTPUT);
 
    pinMode(I1, OUTPUT);
    pinMode(I2, OUTPUT);
    pinMode(I3, OUTPUT);
    pinMode(I4, OUTPUT);

}

void loop() {


  if (stringComplete) {
int commaIndex = inputString.indexOf(',');
int secondCommaIndex = inputString.indexOf(',', commaIndex + 1);
String firstValue = inputString.substring(0, commaIndex);
String secondValue =inputString.substring(commaIndex + 1, secondCommaIndex);
String richting = firstValue;
int snelheid = secondValue.toInt();

if (richting ==" F") {
    analogWrite(E1, snelheid*2);
    analogWrite(E2, snelheid*2);
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
    digitalWrite(I3, LOW);
    digitalWrite(I4, HIGH);
    inputString = "";
}


    Serial.print("Richting = ");
    Serial.println(richting);
    Serial.print("Snelheid = ");
    Serial.println(snelheid*2);
    stringComplete = false;
    inputString = "";
  } 
}




void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

When does stringComplete get set to True ?

When the last char I receive from the string is a carriage return

Your example of " F,123" only contains one comma, but from your code it would appear you are looking for a second comma. Not sure what substring does when the second index is -1, which is what it would get when the indexOf doesnt find a second comma. Perhaps just use:

String secondValue =inputString.substring(commaIndex + 1);

If you read "Robin2's Serial Input Basics - updated", why still use String?

And if you really really really want to use String, why piss away 200! bytes of precious memory?

MrKrabs:
But when I unplug my receiver , reuplode the code and open the terminal and write the exact same string as I would receive

Why do you reupload the code? A program isn't gone when you unplug it.

MrKrabs:
and write the exact same string as I would receive IT DOES WORK !!

Are you 100% sure? For example about:

MrKrabs:
When the last char I receive from the string is a carriage return

You can simply echo the receiver string back to the computer (if Tx isn't going to the transceiver or just disconnect it for now). Put known stop and start characters around it to be sure you see all. Or, might be even better, print each received char as hex :slight_smile:

PS Your code is all over the place, press Ctrl+T and see how that looks.

PSS const variables are preferred over macros (#define) because they are safer

PSSS Names like I1, I2 and E1 are very poor. Yes, they are short but they don't tell what they are. Which makes understanding the code harder, makes remembering them harder and thus makes the change of writing an error bigger.

Yes I don't know any c language and It was a typo it's now set to 20 instead of 200. Sorry I meant Line feed instead of carriage return.

I reuplode the code because it won't listen anymore otherwise.

const M1 = 10; // Enable Pin for motor 1
const M2 = 11; // Enable Pin for motor 2

const I1 = 8; // Control pin 1 for motor 1
const I2 = 9; // Control pin 2 for motor 1
const I3 = 12; // Control pin 1 for motor 2
const I4 = 13; // Control pin 2 for motor 2

String inputString = "";
boolean stringComplete = false;

void setup() {
  // initialize serial:
  Serial.begin(2400);
  inputString.reserve(20);
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);

}

void loop() {


  if (stringComplete) {
    int commaIndex = inputString.indexOf(',');
    int secondCommaIndex = inputString.indexOf(',', commaIndex + 1);
    String firstValue = inputString.substring(0, commaIndex);
    String secondValue = inputString.substring(commaIndex + 1);
    String directionn = firstValue;
    int speedd = secondValue.toInt();

    if (directionn == " F") {
      analogWrite(M1, speedd * 2);
      analogWrite(M2, speedd * 2);
      digitalWrite(I1, LOW);
      digitalWrite(I2, HIGH);
      digitalWrite(I3, LOW);
      digitalWrite(I4, HIGH);
      inputString = "";
    }


    Serial.print("Richting = ");
    Serial.println(directionn);
    Serial.print("Snelheid = ");
    Serial.println(speedd * 2);
    stringComplete = false;
    inputString = "";
  }
}




void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

MrKrabs:
YES I HAVE READ Robin2's Serial Input Basics - updated

But you seem to have ignored it completely ?

(Don't worry, I can sleep nights :slight_smile: )

...R

Robin hello :smiley: ! No i didn't ignore it ( yes I did but not on with bad intentions). It's just that I don't know any C language.

MrKrabs:
It's just that I don't know any C language.

So? String is also C (or actually C++ even) and you seem to kind of make something with that as well.

And if you need to reupload it you did something weird. The code will still be there. You might need to reset the Arduino.

And even 20 bytes is still 20 bytes you don't really need.

And line feed or carriage return, I don't care. But are you a 100% sure the receiver sends them? Like I said, start printing some debug info to the computer. What Arduino do you use and how is it wired to the receiver?

MrKrabs:
Robin hello :smiley: ! No i didn't ignore it ( yes I did but not on with bad intentions). It's just that I don't know any C language.

To use my examples you only need to know how to copy and paste :slight_smile:

...R