0
votes

I am trying to send integers over serial using the serial monitor in the ardunio IDE (then python from a raspberry pi after I debug using serial monitor), and can get single numbers to work with code below, but as soon as I use 10 or 100 it will automatically fill the next variable as 0. I have tried to use serial.readString then convert to an int afterwards, but this is throwing an error also.

int varX = 0;
int varY = 0;
int varZ = 0;
int varCounter = 0;


void setup() {
  Serial.begin(115200);
  grabNewVars();
}

void grabNewVars()
{
  while (varCounter < 3)
  {
    if (Serial.available() > 0) {
      if (varCounter ==0)
      {
      varX = Serial.parseInt();
      }
      if (varCounter ==1)
      {
      varY = Serial.parseInt();
      }
      if (varCounter ==2)
      {
      varZ = Serial.parseInt();
      }
   varCounter++;
    }
  }
}

void loop() 
{
  Serial.println("varX = " + String(varX));
  Serial.println("varY = " + String(varY));
  Serial.println("varZ = " + String(varZ));
  Serial.println("variables received, run main loop");
  delay(100000);

}
1
what do you send to your Arduino? how?Piglet
Sorry that was unclear, I edited my post above. Just using serial monitor at the moment. My plan was to get it working simply on the IDE then implement some python code to send my arduino the integers from a raspberry piMattG
so you enter 100 into the serial monitor and hit send and it will store 1, 0 and 0 in varX, varY and varZ?Piglet
If I send 100 for first value it will set varX as 100, varY as 0, and will wait for another value for varZ. It is like it skips the next line as soon as I use anything over 9. if I use 1 (enter), 2(enter), 3(enter), it works as expected. But if one of those numbers is larger than 9, it will make the next var in the list 0.MattG
which line ending option?Piglet

1 Answers

2
votes

Let's think about what your functions are doing.

Serial.available() returns the number of bytes available in the receive buffer.

Serial.parseInt() reads any digit until a non-digit character is received or it times out if no digit is received within 1 second (default). In that case it returns 0.

So, in your while loop you wait until something is in the receive buffer by checking Serial.available() > 0.

Note that You enter: 100 (without a line ending). Now that “something” is in the receive buffer and varCounter is 0, you execute varX = Serial.parseInt();

parseInt will time out after 1 second and return the 100, that has been received within “that” 1 second.

The 100 is stored in varX and varCounter is incremented by 1.

Now you execute varY = Serial.parseInt();

As you most likely did not enter another number within a second, this will timeout and you'll store 0 in varY.

Terminate your number with a non digit character.

For example: by selecting a line ending in the serial monitor or by sending a newline or carriage return from Python code (Or any other single non-digit character). That way, Arduino knows that the complete Integer has been received and it does not have to wait until it times out.

If you want your code to wait forever: for your next input, check Serial.available > 0 before your next call to parseInt. You already read every number form the receive buffer so: calling parseInt right away doesn't make sense, unless you know for sure that you'll receive another number within a second.