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:
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.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 is1000 * digit1 + 100 * digit2 + 10 * digit3 + digit4.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 forparseInt()smaller.parseInt()inherits from the Stream class so you would useSerial.setTimeout(50)in the setup function to change it to a 50 ms timeout.