Trying To Serial.Read() Data, Always Returns 10

int Speed = 0;
const int motinp1 = 2;
const int motinp2 = 3;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(motinp1, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()) {
    Speed = Serial.read();
    Serial.print("Enter a speed ");
    Serial.println(Speed);
    return Speed;
    delay(500);
  }
  Serial.println(Speed);
  delay(500);
  analogWrite(motinp1, Speed);
  analogWrite(motinp2, 0);
}

This is the code.

Whenever I write any number into the serial monitor, it returns this:

The input for this example was 255.

That's the ASCII code for the NewLine/LineFeed character. Change the Line Ending in Serial Monitor to "None".

If you want to read a multi-digit integer value from Serial Monitor, try Serial.parseInt();.

johnwasser:
That's the ASCII code for the NewLine/LineFeed character. Change the Line Ending in Serial Monitor to "None".

If you want to read a multi-digit integer value from Serial Monitor, try Serial.parseInt();.

By "None," did you mean "No line ending"?

If so, it did increase the value of speed, but it is still a seemingly random number.

Imgur: The magic of the Internet - new serial monitor output. The input was 255, as in the last example.

  • Oops, I hadn't noticed the function you recommended. It works exactly as it is supposed to now, thank you very much :slight_smile:

If I enter 120 in the serial monitor and hit enter I get: (serial monitor line endings* set to both).

Enter a speed 49  // ascii code for '1'
Enter a speed 50  // ascii code for '2'
Enter a speed 48  // ascii code for '0'
Enter a speed 13  // ascii code for 'carriage return ('\r')
Enter a speed 10  // ascii code for new line '\n'
10  // ascii code for new line '\n'
10  // ascii code for new line '\n'
10
10
void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()) 
{  // is there data in the serial buffer?
    Speed = Serial.read();  // read one byte
    Serial.print("Enter a speed ");  // ask for a number (why ask for a number after you read one byte?)
    Serial.println(Speed);  // print the value of the byte read (NOT the whole number entered in serial monitor.)
    return Speed;  // Why a return here?
    delay(500);
}
     Serial.println(Speed);  // print the value of the last byte read (NOT the whole number entered in serial monitor.)
  }

The serial input basics tutorial shows how to receive serial data. The parseInt() function is a blocking function whereas the methods in the tutorial are non-blocking methods that read data into a null terninated character array (string). I advise reading it and playing with the examples to understand how to receive serial data.

line ending.jpg

line ending.jpg

The parseInt() function is a blocking function whereas the methods in the tutorial are non-blocking methods that read data into a null terninated character array (string). I advise reading it and playing with the examples to understand how to receive serial data.

What do you mean by a "blocking function"? What can be the future complications of this?

A blocking function halts all execution until the function is finished. In the case of the parseInt() function the program is stopped while waiting for serial input of the data. That could make a big difference if using, for instance, the AccelStepper library to run steppers or some display libraries to refresh displays. Those libraries require a call to a refresh or run function to update the stepper or display. The refresh/run functions must be called often. Those refreshes cannot happen while the program is blocked. So, if you plan on making more advanced programs, you really need to learn to make non-blocking code. That includes learning how to do timing without the delay() function (which blocks, too).
Non-blocking timing and writing non-blocking code tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

johnwasser:
That's the ASCII code for the NewLine/LineFeed character. Change the Line Ending in Serial Monitor to "None".

Line ending tab of Serial Monitor (Fig-1) has the following options:
1. No line ending,
2. Newline,
3. Carriage return, and
4. Both NL & CR


Figure-1:

Should it not be "No line ending"?

Should it not be "No line ending"?

If the OP takes my advice and uses the methods in the serial input basics tutorial it will not matter.

The image that I posted was meant to show where the line endings box in the serial monitor is located, not what to set it to. That was pointed out by johnwasser:

Change the Line Ending in Serial Monitor to "None".

@OP
1. When I upload your original sketch, I always get continuous 0 on the Serial Monitor without clicking the Send button.

2. When I upload your original sketch, I always get continuous 10 on the Serial Monitor with clicking the Send button.

3. You wish to send a number to UNO of this range: 0 to 999 from the Serial Monitor; receive the ASCII formatted numbers and then extract the original number (say: 255), save it in a variable, use it and then show it on the OutputBox of the Serial Monitor. This is the non-blocking sketch you can upload in your UNO (select Newline option in the Line ending tab of Serial Monitor as a terminating symbol):

char mySpeed[3] = "";
const int motinp1 = 2;
int i = 0; //index for mySpeed[] array

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

void loop()
{
  byte n = Serial.available();
  if (n != 0 )
  {
    char x = Serial.read();  //get data item from Serial BUFFer
    if (x != '\n') //Newline character is not detected
    {
      mySpeed[i] = x;
      i++;         //adjust array index pointer
    }
    else  //speed entry is done; Newline character is found
    {
      int Speed = atoi(mySpeed);   //integer Speed is extracted from ASCII formatted Speed data 
      Serial.println(Speed);
      analogWrite(motinp1, Speed);
      memset(mySpeed, 0, 3); //Speed[] array is reset to 0s
      i = 0;  //reset array index
    }
  }
}

@golamMostafa

I did something similiar to yours, but do you know why, this doesn't advance past the input 1?

int Speed = 0;
const int motinp1 = 2;
const int motinp2 = 3;
const int motinp3 = 4;
const int motinp4 = 5;

int inpSpeedS[4]; // An array to determine the speed of each individual input


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(motinp1, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //Booleans for changing motor input
  boolean valueInp1 = false;
  boolean valueInp2 = false;
  boolean valueInp3 = false;
  boolean valueInp4 = false;

  //Motor driving functions times 4
 if (Serial.available () && valueInp1 == false && valueInp2 == false && valueInp3 == false) {
    inpSpeedS[0] = Serial.parseInt();
    Serial.print("Input 1: ");
    Serial.print(inpSpeedS[0]);
    delay(500);
    boolean valueInp1 = true;
    Serial.println(valueInp1);
    return inpSpeedS[0], valueInp1;
  }
  else if (Serial.available () && valueInp1 == true && valueInp2 == false && valueInp3 == false) {
    inpSpeedS[1] = Serial.parseInt();
    Serial.println("Input 2: ");
    Serial.print(inpSpeedS[1]);
    delay(500);
    valueInp2 = true;
    return inpSpeedS[1], valueInp2;
  }
  else if (Serial.available () && valueInp1 == true && valueInp2 == true && valueInp3 == false) {
    inpSpeedS[2] = Serial.parseInt();
    Serial.println("Input 3: ");
    Serial.print(inpSpeedS[2]);
    delay(500);
    valueInp3 = true;
    return inpSpeedS[2], valueInp3;
  }
  else if (Serial.available () && valueInp1 == true && valueInp2 == true && valueInp3 == true) {
    inpSpeedS[3] = Serial.parseInt();
    Serial.println("Input 4: ");
    Serial.print(inpSpeedS[3]);
    delay(500);
    valueInp1 = false;
    valueInp2 = false;
    valueInp3 = false;
    valueInp4= false;
      return inpSpeedS[3],valueInp1, valueInp2, valueInp3; valueInp4;
  }
  delay(500);
  analogWrite(motinp1, inpSpeedS[0]);
  analogWrite(motinp2, inpSpeedS[1]);
  analogWrite(motinp3, inpSpeedS[2]);
  analogWrite(motinp4, inpSpeedS[3]);
}

groundFungus:
A blocking function halts all execution until the function is finished. In the case of the parseInt() function the program is stopped while waiting for serial input of the data. That could make a big difference if using, for instance, the AccelStepper library to run steppers or some display libraries to refresh displays. Those libraries require a call to a refresh or run function to update the stepper or display. The refresh/run functions must be called often. Those refreshes cannot happen while the program is blocked. So, if you plan on making more advanced programs, you really need to learn to make non-blocking code. That includes learning how to do timing without the delay() function (which blocks, too).
Non-blocking timing and writing non-blocking code tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Well I did actually need, as it turned out, a blocking function. But I will ask the same question I asked to Golan to you, do you know why my code doesn't advance past input 1?

char mySpeed[3] = "";Oops.

TheMemberFormerlyKnownAsAWOL:

char mySpeed[3] = "";

Oops.

When I declare the following, a null-byte is automatically inserted (during compilation) at the end of the last item .

char myData[] = "A1C";
==> char myData[] = {0x41, 0x31, 0x43, 0x00};

In my quoted declaration, I have asked space for three charcaters with initial values of 0s. The compiler should insert null-byte automatically. I really don't understand why the dimension of the mySpeed array should be 4.

gigawhat:
Well I did actually need, as it turned out, a blocking function. But I will ask the same question I asked to Golan to you, do you know why my code doesn't advance past input 1?

Try the following sketch (your's one with slight adjustments):

int Speed = 0;
const int motinp1 = 2;
const int motinp2 = 3;
const int motinp3 = 4;
const int motinp4 = 5;
//-------------------------
boolean valueInp1 = false;
boolean valueInp2 = false;
boolean valueInp3 = false;
boolean valueInp4 = false;


int inpSpeedS[4]; // An array to determine the speed of each individual input


void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(motinp1, OUTPUT);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  //Booleans for changing motor input
  // boolean valueInp1 = false;
  // boolean valueInp2 = false;
  // boolean valueInp3 = false;
  //boolean valueInp4 = false;

  //Motor driving functions times 4
  if (Serial.available () && (valueInp1 == false)
      && (valueInp2 == false) && (valueInp3 == false))
  {
    inpSpeedS[0] = Serial.parseInt();
    Serial.print("Input 1: ");
    Serial.println(inpSpeedS[0]);
   // delay(500);
    valueInp1 = true;
   // Serial.print("  "); Serial.println(valueInp1);
   // return inpSpeedS[0], valueInp1;
  }
 // else
  if (Serial.available () && (valueInp1 == true) && (valueInp2 == false)
           && (valueInp3 == false))
  {
    inpSpeedS[1] = Serial.parseInt();
    Serial.print("Input 2: ");
    Serial.println(inpSpeedS[1]);
    //delay(500);
    valueInp2 = true;
   // return inpSpeedS[1], valueInp2;
  }
 // else 
  if (Serial.available () && (valueInp1 == true) && (valueInp2 == true)
           && (valueInp3 == false))
  {
    inpSpeedS[2] = Serial.parseInt();
    Serial.print("Input 3: ");
    Serial.println(inpSpeedS[2]);
    //delay(500);
    valueInp3 = true;
  //  return inpSpeedS[2], valueInp3;
  }
 // else 
  if (Serial.available () && (valueInp1 == true) && (valueInp2 == true)
           && (valueInp3 == true))
  {
    inpSpeedS[3] = Serial.parseInt();
    Serial.print("Input 4: ");
    Serial.println(inpSpeedS[3]);
    delay(500);
    valueInp1 = false;
    valueInp2 = false;
    valueInp3 = false;
    valueInp4 = false;
   // return inpSpeedS[3], valueInp1, valueInp2, valueInp3; valueInp4;
  }
  //delay(500);
  analogWrite(motinp1, inpSpeedS[0]);
  analogWrite(motinp2, inpSpeedS[1]);
  analogWrite(motinp3, inpSpeedS[2]);
  analogWrite(motinp4, inpSpeedS[3]);
}


Figure-1:

GolamMostafa:
When I declare the following, a null-byte is automatically inserted (during compilation) at the end of the last item .

char myData[] = "A1C";

==> char myData = {0x41, 0x31, 0x43, 0x00};




In my quoted declaration, I have asked space for three charcaters with initial values of 0s. The compiler should insert null-byte automatically. I really don't understand why the dimension of the mySpeed[] array should be 4.

Because (in your own words)

  1. You wish to send a number to UNO of this range: 0 to 999 from the Serial Monitor

and because atoi(mySpeed)