Arduino + Unity Timeout Exception Only with Certain Code?

Hey y'all, first post, please go easy on me :smiley:

Anyway, so I've been using a joystick made with a Teensy LC, and used the Arduino IDE to program it to send data to Unity. This is what it looks like:

int x;
int y;

void setup() {
  Serial.flush();
  Serial.begin(115200);
}

void loop() {
  x = analogRead(1);
  y = analogRead(2);
  Serial.print("X");
  Serial.println(x);
  Serial.print("Y");
  Serial.println(y);
}

This works fine, but I need to include a moving average filter in order to smooth the signal a bit, and this is what that looks like:

int x;
int y;
int sumx;
int sumy;
int period = 0;

void setup() {
  Serial.flush();
  Serial.begin(115200);
}

void loop() {
  sumx += analogRead(1);
  sumy += analogRead(2);
  period++;
  if (period == 2) {
    x = round(sumx / period);
    y = round(sumy / period);
    Serial.print("X");
    Serial.println(x);
    Serial.print("Y");
    Serial.println(y);
    sumx = 0;
    sumy = 0;
    period = 0;
  }
}

But for some reason, this gives me a timeout exception (from Unity) a few seconds after starting up my Unity program. I know that returning the average every 2 iterations doesn't really give me good smoothing, but I was trying it with larger numbers (i.e. period == 20 or 50 or 100) and those would instantly give me a timeout exception.

On the Unity side, I have tried increasing readTimeout to 5000, and setting the SerialPort object's DtrEnable to true (C# btw), but nothing has worked so far.

For now, I'm just using the former code, but if I'm still curious as to why one code works and the other doesn't. Any feedback would be greatly appreciated :slight_smile:

You are definitely saturating the small output buffer of your arduino by sending data so fast, so when that happens print will be blocking so I don’t see this code really running slower than the first one when it comes to the pace of the output. 115200 bauds will be the limiting factor probably.

If you use anything high for the period, change the data type to long just to ensure you don’t overflow the sums

Any way to change this to 1000000 bauds on the unity side ?

You should also throttle the sending of the data on the arduino side using millis() this way you can decide how many events per second you send

PS: The Serial.flush(); before Serial.begin is useless.

Sorry, I hadn't seen this until now.

Thanks for the advice. I tried everything you said and it still just times out immediately. I posted this problem on the Unity forum too, but I haven't gotten any replies there yet, so I can't say whether this pertains more to Arduino or to Unity. I do think it's probably something with Unity, though.

Anyway, thank you! I'll report back here if I do find something or if someone replies on the Unity forum.

You might want to initialize your variables before using them..

int sumx;
int sumy;

Danois90:
You might want to initialize your variables before using them..

int sumx;

int sumy;

Why? The standard mandates that global variables be initialized to 0.

blh64:
Why? The standard mandates that global variables be initialized to 0.

It's better to explicitly tell the compiler what you want instead of having it giving you something.

Danois90:
It's better to explicitly tell the compiler what you want instead of having it giving you something.

well that something is part of the language standard, not something the compiler decides for you. You are guaranteed that global variables will be set at 0.

That being said, just for the sake of readability and showing that I intentionally want 0 as a default value (and for those who never read the fine prints of the language :slight_smile: ) I usually initialize my variables even if it's with 0. That does not cost more memory at run time.