Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Serial fixes #4015
Another potential fix would be putting this code
outside of draw() and into
The code in that example is almost ten years old, and hasn't been synced with the equivalent Arduino examples. So it should change. See https://www.arduino.cc/en/Tutorial/PhysicalPixel for the current version.
Not sure where the delay(100) is coming from, but it's not in the equivalent Arduino example (see https://www.arduino.cc/en/Tutorial/PhysicalPixel). The real dependency is the delay(), of course, and the Serial.read(). It reads one byte at a time. If you read using if(), then the remaining bytes stay in the Arduino's serial buffer until the next read(). I tend to use them both interchangeably. If I'm sending ASCII, then I use Serial.parseInt() (see https://www.arduino.cc/en/Tutorial/ReadASCIIString).
If you use a while() loop, then nothing else happens until you're finished that loop. In this example, that's no problem, but if a person tries to incorporate other things into their code that's timing-dependent, there can be trouble, depending on how fast the sender is sending.
I usually follow this lesson up with a lesson on handshaking, in which Arduino only sends when Processing requests bytes. (See https://www.arduino.cc/en/Tutorial/SerialCallResponse). But I keep them separate, because these are two different concepts, and I find people learn better when a given example introduces only one concept at a time.
I also try to teach people not to use delay() where possible, but instead to use something like this instead:
Hope that helps,