Help with Serial Data to Unreal Engine from a Bourns ACE128 rotary encoder

I'm trying to build a lens encoder to feed serial data into Unreal Engine. I decided to use a Bourns absolute rotary encoder. However my serial data seems incorrect in the serial monitor.

Can anyone please offer some advice? I'm a novice when it comes to variable types and byte arrays, etc. I'm just simply looking to verify that my sketch is good, before I begin attempting to see the data in Unreal Engine

See my code attached, and also the readings from the serial Monitor:

spinning the encoder around.txt (16.4 KB)

ace128_0x20_Joel.ino (2.91 KB)

Read the sticky at the top of the forum "Read this before posting a programming question".

Thanks Steve,

Curious if you're suggesting that I didn't follow the some part of the 'rules before posting'. Perhaps you can be specific.

jreo:
Thanks Steve,

Curious if you're suggesting that I didn't follow the some part of the 'rules before posting'. Perhaps you can be specific.

verify if your sketch is good?

what does "good" mean?

How would we know it is good?

@ieee48 as seen in the sketch, I'm attempting to send an integer via serial. The raw integer values coming from my encoder module are 'mpos', so I'm attempting to debug whether I'm properly sending the integer out as a byte. However, the serial monitor appears that the data I'm sending is not an integer - which is bad, not good. I'm not certain. Perhaps I am debugging the 'bytes' in the serial monitor incorrectly, or perhaps I'm handling the variables incorrectly.

I would suggest that you lean how to post code with code tags which is what SteveMann suggested earlier if you want someone to easily look at your code.

.

However my serial data seems incorrect in the serial monitor.

Are the mpos values correct?

Is your question how to send a signed integer to Unreal Engine instead of to the serial monitor?

byte lBuffer[] = {                  // not sure i'm converting the encoders data to a byte correctly
      byte(intValue & 0xff),
      byte(intValue >> 8 & 0xff),
      byte(intValue >> 16 & 0xff),
      byte(intValue >> 24 & 0xff)
    };
   Serial.write(lBuffer, 4);

This code looks correct according to the library example provided with UE4Duino

long int lVal = 123456L;
    byte lBuffer[] = {
      byte(lVal & 0xff),
      byte(lVal >> 8 & 0xff),
      byte(lVal >> 16 & 0xff),
      byte(lVal >> 24 & 0xff)
    };
    Serial.write(lBuffer, 4);
  }

The Serial.write of the buffer will not show correctly in the monitor, but you can see that the data is correct from this example code

void setup() {
  Serial.begin(115200);
  long int  lVal = -127;
  byte lBuffer[] = {
    byte(lVal & 0xff),
    byte(lVal >> 8 & 0xff),
    byte(lVal >> 16 & 0xff),
    byte(lVal >> 24 & 0xff)
  };

  for (byte i = 0; i < 4; i++)
  {
    Serial.println(byte(lBuffer[i]), HEX);
  }   
  long int reconstructedValue = 0;
  for(int i = 3; i >= 0; i--)
  {
    reconstructedValue = (reconstructedValue<<8) + lBuffer[i];
  }
  Serial.println(reconstructedValue); 
}
void loop() {}

@cattledog yes the mpos values are correct

The Serial.write of the buffer will not show correctly in the monitor, but you can see that the data is correct from this example code

This is very helpful, thank you. Also thanks for the sample code which greatly help me resolve my uncertainty. Just watching serial monitor made me doubt the code. Intentions were to send to UE, but serial.write would still show up in the monitor, so I thought to focus on the discrepancy there. Moving forward I won't rely on serial monitor for the serial.write buffer. I'll be moving onto UE to debug the data now. I'm thankful to people like you who are contributing useful insights to this community. :slight_smile: :slight_smile:

I would suggest that you lean how to post code with code tags which is what SteveMann suggested earlier if you want someone to easily look at your code.

@ieee488 Not sure what you mean. I may need to 'lean'????

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.