Skip to main content
Fix typos and formatting
Source Link
per1234
  • 4.3k
  • 2
  • 24
  • 44

As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000ms1000 ms (1second1 second). ThereThere are a couple of ways to fix this, order most difficult to least:

  1. Instead of just sending a number. Send a number and then a letter at the end. So instead of sending "123", you would send "123x". ParseInt will immediately return when it hits the x. However, you'll still have the x in the stream so you'll need to manually read it and discard it.

    Instead of just sending a number, send a number and then a letter at the end. So instead of sending "123", you would send "123x". parseInt() will immediately return when it hits the x. However, you'll still have the x in the stream so you'll need to manually read it and discard it.
  2. Make the value that is sent a set number of digits - say 4 for example. Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying. If you read in digit1, digit2, digit3, digit4 then the number is 1000digit1 + 100digit2 + 10*digit3 + digit4.

    Make the value that is sent a set number of digits - say 4 for example. Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying. If you read in digit1, digit2, digit3, digit4 then the number is 1000 * digit1 + 100 * digit2 + 10 * digit3 + digit4.
  3. Make the timeout value for parseInt smaller. ParseInt inherits from the stream class so you would use Serial.setTimeout(50) in the setup function to change it 50ms timeout.

    Make the timeout value for parseInt() smaller. parseInt() inherits from the Stream class so you would use Serial.setTimeout(50) in the setup function to change it to a 50 ms timeout.

As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000ms (1second). There are a couple of ways to fix this, order most difficult to least:

  1. Instead of just sending a number. Send a number and then a letter at the end. So instead of sending "123", you would send "123x". ParseInt will immediately return when it hits the x. However, you'll still have the x in the stream so you'll need to manually read it and discard it.

  2. Make the value that is sent a set number of digits - say 4 for example. Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying. If you read in digit1, digit2, digit3, digit4 then the number is 1000digit1 + 100digit2 + 10*digit3 + digit4.

  3. Make the timeout value for parseInt smaller. ParseInt inherits from the stream class so you would use Serial.setTimeout(50) in the setup function to change it 50ms timeout.

As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000 ms (1 second). There are a couple of ways to fix this, order most difficult to least:

  1. Instead of just sending a number, send a number and then a letter at the end. So instead of sending "123", you would send "123x". parseInt() will immediately return when it hits the x. However, you'll still have the x in the stream so you'll need to manually read it and discard it.
  2. Make the value that is sent a set number of digits - say 4 for example. Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying. If you read in digit1, digit2, digit3, digit4 then the number is 1000 * digit1 + 100 * digit2 + 10 * digit3 + digit4.
  3. Make the timeout value for parseInt() smaller. parseInt() inherits from the Stream class so you would use Serial.setTimeout(50) in the setup function to change it to a 50 ms timeout.
Source Link
mwwalk
  • 446
  • 2
  • 6

As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000ms (1second). There are a couple of ways to fix this, order most difficult to least:

  1. Instead of just sending a number. Send a number and then a letter at the end. So instead of sending "123", you would send "123x". ParseInt will immediately return when it hits the x. However, you'll still have the x in the stream so you'll need to manually read it and discard it.

  2. Make the value that is sent a set number of digits - say 4 for example. Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying. If you read in digit1, digit2, digit3, digit4 then the number is 1000digit1 + 100digit2 + 10*digit3 + digit4.

  3. Make the timeout value for parseInt smaller. ParseInt inherits from the stream class so you would use Serial.setTimeout(50) in the setup function to change it 50ms timeout.