Trying to catch an empty value?

c =;
  if (c == 'z') {
    travel_mode = 0;
  } else if (c == 'y') {
    travel_mode = 1;
  } else if (c == -1) {
    deactivation_sum = deactivation_high_threshold;

I am trying to catch an empty (when no data is coming in) in the final else if statement above. However, when I check the Serial monitor that the value of c is getting printed out to, it shows a little square and not a -1.

Does anyone know what I am missing?

An empty read returns -1
It is detailed on the reference page.

That’s what I thought but I am receiving this so my else if statement is never catching the scenario where there is no incoming data.

Because you haven’t written that bit yet?

I'm not following. I have the ability to send characters over the Bluetooth serial via a C# GUI. When this connection is broken, I want to enter the final else if statement. However, when there is no data in the it is not returning a -1 as you suggested, it is returning a square.

You’ve changed the code in the original post.
But you print whatever you received, so yes, you will print a weird character

Perhaps a less confusing approach is to use the Serial.available() method as a precursor to calling which appears in almost all the Arduino serial port examples.

I changed it to reflect the -1 that I should be receiving. It had no effect on the functionality of the code.

I am not receiving anything though, I have no data being sent over Serial and therefore expect to be receiving a -1 but I am not.

Does the -1 imply that there is a connection but no values are coming in while the little square is just a garbage value because there is no connection?

What is the ASCII character for -1?

No, I don’t know either, but I bet, for you, it looks a lot like a little square.

It shows a little square because println is trying to print an ASCII version of the value (-1) stored in variable c.

From Arduino language reference pages:


Prints data to the serial port as human-readable ASCII text followed by a carriage return character

You probably declared c as a char or int type... Remember, variable c stores integer values, not actual characters. Serial.println just converts those integer values into things we recognize (ASCII text).

Here's another way to see this in action.

Take your original code in your first post.




 Serial.println(c + 66);

What does it output?

Quick note for the OP.

If you're working with serial and different datatypes in Arduino, it's important to understand how Arduino's implementation of Serial.print and Serial.println behave depending on operations and datatype. You should very clearly understand why each of the lines of code outputs what it does in the examples below.

  int a = -1;
  byte b = -1;
  char c = -1;
  Serial.println(a + 66);  Serial.println();

  Serial.println(b + 66);  Serial.println();

  Serial.println(c + 66);  Serial.println();

  Serial.println(c + 0);
  Serial.println(char(c + 66));






You need to be careful using the -1 return from .read() to indicate a broken connection. Even at high baud rates, serial is very slow compared to the arduino, so unless your code is very slow, you will normally see a lot of -1's returned from read() between characters.