I just had an interesting issue that took a little poking to resolve.
The sketch I was working with received two bytes from the serial port and then sent quite a few back, depending on what was received.
I declared some variables:
byte rx_buffer; byte rx_pos; byte packet_len;
followed by a few more.
Not long into the run of my program things went horribly wrong, and a bit of debugging showed that the variables after the buffer were being corrupted.
I inserted a temporary buffer in between my rx_buffer and the rest of my variables and zeroed it in setup and added code to tell me which positions in the buffer were non-zero.
The buffer was being written to two bytes at a time after some point, and some playing with the length showed it didn’t corrupt past position 103.
Anyhow, I found what was happening. The file wiring_serial.c has a global variable:
And so everything got confused.
So I can think of three fixes that could prevent people being burned by stuff like this in the future - in order of difficulty:
Add all the global variables to the Keywords page.
Turn on some compiler warnings. This might require a bit of parsing and suppressing of warnings that might not be too beginner friendly, but variable redeclaration is probably something that needs to be caught.
Either manually or using clang or antlr or something, write a utility that scans the c files in /hardware/arduino/cores and prepends some kind of namespace prefix to the global variables, both for their declaration and internal usage. If this sounds like it’s the best option and I get some free time in the coming months I might do this just for kicks.
I just thought I’d let people know there was a potential pitful lurking around and waiting to pounce on beginner programmers.