Microcontroller UART - acting bizzarely
The problem was not with the code, but the fact that serial port BAUD rate on my computer was different than on the controller. I solved the problem running the following command:

sudo stty -F /dev/ttyACM2 9600 raw -echo