0

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 <Wire.h>
/* 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
}
Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Nov 16, 2018 at 6:36
5
  • Consider adding more debug lines during your Sendsignals function. Perhaps it hangs up during the Wire transmission. You should also consider changing sprintf to snprintf - apparently it's more robust against buffer issues, and requires the buffer size to be set manually. Commented Nov 16, 2018 at 7:16
  • I tried that and it seems everything runs fine up until the break; when going through my separate function. Commented Nov 16, 2018 at 7:43
  • Arduino Uno with an Adafruit motor shield Commented Nov 16, 2018 at 8:17
  • reconnecting Serial Monitor resets the board. while (Serial.available()==0){} blocks the loop Commented Nov 16, 2018 at 8:18
  • Everything seems to work fine though when not calling Wire.h . Once I call the Wire.h functions the serial connection messes up. Commented Nov 16, 2018 at 8:21

1 Answer 1

2

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.

answered Nov 16, 2018 at 8:42
3
  • This causes the serial terminal to disconnect? How to fix? Should I increase the str to 8 bits? Commented Nov 16, 2018 at 8:47
  • Thanks. I changed the size of the str to 16 and it fixed the issue. Commented Nov 16, 2018 at 8:58
  • str[5] would be enough, if your codes have always 3 digits Commented Nov 16, 2018 at 9:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.