Serial.println - incomplete message

Hi,
i'm new to the arduino world; i tried to get this running:

const byte led = 6;
bool isRunning = true;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0)
  {
    int inValue = Serial.parseInt();
    Serial.println("received: "+inValue);
    isRunning = inValue == 1;
  }
  if (!isRunning) return;
  byte i=0;
  while(i<255)
  {
    analogWrite(led, i);
    i++;
    delay(10);
  }
  while(i>0)
  {
    analogWrite(led, i);
    i--;
    delay(10);
  }
}

But my problem is that the serial monitor logs "received: " when i input 0, and "eceived: " when i input 1.
I have no idea why. The baud rate in the monitor is also set to 9600.
It stops and restarts lighting up the LED though, so the values seem to be received correctly.
What am i doing wrong with the println?

Polymo:
Hi,
i'm new to the arduino world; i tried to get this running:

const byte led = 6;

bool isRunning = true;

void setup() {
 pinMode(led, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 while (Serial.available() > 0)
 {
   int inValue = Serial.parseInt();
   Serial.println("received: "+inValue);
   isRunning = inValue == 1;
 }
 if (!isRunning) return;
 byte i=0;
 while(i<255)
 {
   analogWrite(led, i);
   i++;
   delay(10);
 }
 while(i>0)
 {
   analogWrite(led, i);
   i--;
   delay(10);
 }
}




But my problem is that the serial monitor logs "received: " when i input 0, and "eceived: " when i input 1.
I have no idea why. The baud rate in the monitor is also set to 9600.
It stops and restarts lighting up the LED though, so the values seem to be received correctly.
What am i doing wrong with the println?

I'm surprised you even get this through the compiler. As far as I know, summing strings and numbers like that, is not allowed. This isn't java ... :slight_smile:

Thus, you'd have to write:

Serial.print("received:");
Serial.println(inValue);

Indeed, this prints correctly. I'm used to writing c#, and i thought concatenating strings this way would work here too (i also read something like that in the documentation (?)).
Thanks for pointing me to a clean way to do it.

i also read something like that in the documentation (?)

No, you did not.

Serial.println("received: "+inValue);

This is valid C but it doesn't do what the OP expected - it is not a concatenation operation. If, for example, inValue is 3, it adds 3 to the address of the string "received: " and uses that. It therefore prints "eived".

Pete

el_supremo:

Serial.println("received: "+inValue);

This is valid C but it doesn't do what the OP expected - it is not a concatenation operation. If, for example, inValue is 3, it adds 3 to the address of the string "received: " and uses that. It therefore prints "eived".

Pete

Ah, of course. Nice one! :slight_smile:

@Polymo, have a look at Serial Input Basics - simple reliable ways to receive data. Your system is not robust because the Arduino can take data out of the serial input buffer much faster than it arrives.

Also don't use delay() as it blocks the Arduino from doing other things - such as checking for new serial data. Look at how millis() is used to manage timing in Several Things at a Time

...R

I'm sure that this is most likely not the way to do it :slight_smile: (it just was my very first try at communicating something to the arduino and back; it's my second day with it). But thanks for the links, i'll read them.
https://www.arduino.cc/en/Tutorial/StringAdditionOperator this is what i read about string concatenation and i was under the impression it mirrors what i was doing, but i now see that in the middle, it talks about my case as being unpredictable.
@el_supremo, that is interesting and makes some kind of sense (i just nevery directly worked with pointers).

Thank you all for your help.

There's a difference between what that page is describing and what you were actually doing/using.
The page describes operations that can be performed on a String - note the upper case S. It is best to avoid using Strings on the smaller Arduinos because of the overheads introduced.
What you used, "received:", is a null-terminated C-style string (lower case).

Pete