Serial Communication Problem - Simulink/Arduino

SOLVED

It turned out my problem was not in the code, but I selected the wrong transistor for my fan. I have no idea why, but the transistor (BD139) from the fan circuit was causing interference in the lamp circuit, although the only physical connection between them was the common ground.

I changed from the BD139 to a TIP122 and no more flickering.