Serial.print not printing all 10,000 values

I am experimenting with serial communication and I am trying to get a list to print each value and flash an LED from zero to whatever value I input into the serial monitor. The count seems to get to 48 and then turn over again to 0 when I input a number such as 10,000. It works fine if I remove the serial input and hardcode it.

int incomingByte = 0;
int a = 10;

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

void loop() {


  if (Serial.available())
  {
    incomingByte = Serial.read();


      for (int i = 0; i <= incomingByte; i++)
      {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(a);
        digitalWrite(LED_BUILTIN, LOW);
        delay(a);
        Serial.print(i);
        Serial.print("\n");
        
         
        
      }
     

  }

 

}

This code won't read a number like 10000.

incomingByte = Serial.read();

All it will get is the first '1' which has the ASCII value 49. Next time round it will read the '0' which has the value 48. Maybe that sheds some light on the problem?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

@Op

1, It is assumed that you have chosen Newline option in the 'Line ending tab' of the Serial Monitor (Fig-1) and then you have entered 10000 in the InputBox of the Serial Monitor and then you have clicked on the Send button.
SerialMonitor.png
Figure-1: Serial Monitor

2. What do you think about -- what numbers (s) have entered in your Arduino UNO when you have sent 10000 via Serial Monitor?

3. In the Serial Buffer of your UNO, these numbers: 0x31 (49) for 1, 0x30 (48) for 0, 0x30 (48) for 0, 0x30 (48) for 0, and 0x30 (48) have entered into the storage locations of the buffer as conceptually shown in the following figure.
sbuff.png
Figure-2: Conceptual view of serial buffer of UART Port

4. You have executed this instruction:

if(Serial.available())

to check if there is any data in the Serial Buffer. There is data in he buffer; so, the MCU will execute your next instruction which is:

incomingByte = Serial.read();

and you have got the content of topmost location, which is 0x31 (3x161 + 1x160 = 48 + 1 = 49). Your led will blink for 49 times due to for() loop.

5. When you have taken out ox31 from the buffer, all the 4 items below it will go up by 1 postion. As a result, the topmost location of the buffer now contains 0x30 (48).

6. because you are under loop() function, the above tasks of Step-4, 5 will be repeated. As a result, the led will blink for another 192 (4x48) times.

7. If you want that the led should blink for 10000 (ten thousand times) and not for 241 (192+49) times, then you have to execute the following codes:

void loop()
{
    byte x = Serial.available()
    if(x !=0)
    {
        Serial.readBytesUntil('\n',  myArray, 10);   //declare myArray as: char myArray [] = ""; 
    }
    int y = atoi(myArray);          //x contains 10000
    //-------blink led for y times
}

sbuff.png

SerialMonitor.png

I was unable to make the code work. Nothing happens when I try to input a number. This is my full script:

int a = 100;
char myArray [] = "";

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




void loop()
{
    byte x = Serial.available();
    if(x != 0)
    {
        
        Serial.readBytesUntil('\n',  myArray, 10);
       
    }
    
    int y = atoi(myArray);          //x contains 10000
    for (int i = 1; i <= y; i++)
      {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(a);
        digitalWrite(LED_BUILTIN, LOW);
        delay(a);
       
      }
    
}

My ultimate goal for this experiment is to have a python script send the direction and number of steps for a stepper motor to move.

mkomitsky:
My ultimate goal for this experiment is to have a python script send the direction and number of steps for a stepper motor to move.

This simple Python - Arduino demo may help.

...R

I am still unable to even input a number into the serial monitor. I want to understand that part before I start trying to use python.

mkomitsky:
I am still unable to even input a number into the serial monitor.

Can you explain that sentence, please?

AWOL:
Can you explain that sentence, please?

I mean that if I type a number into the serial monitor, nothing happens with the LED

mkomitsky:
I was unable to make the code work. Nothing happens when I try to input a number.

Try the following codes (tested in UNO) by sending small number (say 10) from the InputBox of the Serial Monitor. Compare these codes with your codes and see the difference.

//int a = 100;
char myArray [15] = ""; //Edit

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

void loop()
{
  byte x = Serial.available();
  if (x != 0)
  {
    Serial.readBytesUntil('\n',  myArray, 10);

    int y = atoi(myArray);          //x contains 10000
    for (int i = 0; i < y; i++)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
    }
  }
}

mkomitsky:
I am still unable to even input a number into the serial monitor. I want to understand that part before I start trying to use python.

Have you tried the examples in my link in Reply #1 ?

...R

GolamMostafa:
Try the following codes (tested in UNO) by sending small number (say 10) from the InputBox of the Serial Monitor. Compare these codes with your codes and see the difference.

//int a = 100;

char myArray [] = "";

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

void loop()
{
 byte x = Serial.available();
 if (x != 0)
 {
   Serial.readBytesUntil('\n',  myArray, 10);

int y = atoi(myArray);          //x contains 10000
   for (int i = 0; i < y; i++)
   {
     digitalWrite(LED_BUILTIN, HIGH);
     delay(1000);
     digitalWrite(LED_BUILTIN, LOW);
     delay(1000);
   }
 }
}

That code (as well as the OP's) has a bug: myArray can hold only one character (plus the null terminator). You need to declare myArray as char myArray[10+1] = ""; to make it big enough.

christop:
That code (as well as the OP's) has a bug: myArray can hold only one character (plus the null terminator). You need to declare myArray as char myArray[10+1] = ""; to make it big enough.

Appreciate your meticulous observation for pointing the mistake(typo/ignorance?). I am correcting the line in the referred post. (+).