Arduino losing connection to Serial port after running function For some reason when I loop my code through a separate function the serial port hangs up. The serial connection appears fine when I call cases within the loop but not when I call outside. 

I tried to figure out where the problem is and for some reason using the Serial.print(ComControlValue); and the output is


  -1 


But the error still persists.

Previous it was this before I changed the baudrate


  -b⸮⸮⸮⸮ Waiting for Command


It's not printing out the -1 I'm using as the main case. Could this be the problem?

#include &lt;Wire.h&gt;
/* RPi Army variables */
int RPi[3] = {3,6,9};                                        // SDA/SCL code names for the different Raspberry pi's

/* Define parameters for computer control */
int ComControlValue = -1;

void setup() {
  Serial.begin(115200);                                     // Establish Serial connection to computer
  Wire.begin();                                             // join i2c bus as master
}

void loop() {
 switch(ComControlValue){
  /* -1 : nothing happened yet */
  case -1:
     Serial.println("Waiting for Command");
     while (Serial.available()==0){}
     ComControlValue=Serial.parseInt();                             // Pull value from serial port
     break;     

  case 111:
     Serial.println("Eat more Toast");
     ComControlValue = -1;
     break;

  case 210:
     Sendsignals(RPi[0],333);
     Sendsignals(RPi[1],333);
     Sendsignals(RPi[2],333);
     ComControlValue= -1;
     Serial.print(ComControlValue);
     break;
  }
}      

void Sendsignals(int Rpi, int code){
   char str[4];
   sprintf(str, "%3d\n", code);
   Wire.beginTransmission(Rpi);                                 // transmit to device of choice
   Wire.write(str);                                             // sends 16 bytes
   Wire.endTransmission();                                      // stop transmitting
}
Your str variable in function Sendsignals is short. sprintf writes 5 bytes to it, 3 digits, the \n and terminating zero. The terminating zero is written outside of the allocated array and causes the crash.