Sending int from python to arduino, but there is an upper limit. How do I solve it?

Viewed 44

in my project I am sending int from python to arduino, which is the number of steps the stepper motor should take. Python code:

while(1):
userInput = input('Get data point?')

ser.write("s{}".format(int(131.0268562*userInput)).encode())

if userInput == 'c':
    break;
    

Arduino Code:

if (Serial.available() > 0){


int receivedValue = Serial.parseInt();

digitalWrite(4,HIGH);


if (receivedValue>=0){
  for(Index = 0; Index < receivedValue; Index++)
  {
    digitalWrite(5,HIGH);

    delayMicroseconds(s);

    digitalWrite(5,LOW);
    
    delayMicroseconds(s);
    }
    }
    
}

But when the value is bigger than 30000, the code doesn't work. When I print the value on arduino side, it is either negative, or some small positive number

Why is it happening and how do I solve it?

Edit: I have solved it thanks to the helps of "quamrana" and "Serge Ballesta"

If anyone stumbles on a similar problem. Just use:

long receivedValue = Serial.Stream::parseInt();
1 Answers

I would assume that the limit is exactly 32767 which is 2**15 - 1. This is a hint that on Arduino parseInt return a 16 bits, 2's complement integer.

If you have a larger integral type (long ?) on Arduino, you should try to use it. An alternative would be to use a different encoding passing two integers each 15 bits max. At the end you would loop high value times 32767 times + low value times.

Related