I'm trying to read and print wheel encoder values on a Mega 2650, and am having
great difficulty. I've tried several variations of
void EncoderLt() {
Serial3.write(0x00);
Serial3.write(0x23);
delay(50);
if(Serial3.available() > 3) {
a1 = Serial3.read();
a2 = Serial3.read();
a3 = Serial3.read();
a4 = Serial3.read();
long result1;
result1 = a1<<24;
result1 += a2<<16;
result1 += a3<<8;
result1 += a4;
Serial.println(result1);
}
}
The serial Tx is to a Devantech MD49 motor controller; the a's are typed bytes. The count proceeds normally until the value reaches 2^15, then the count reverts to negative values and counts down toward zero. In other words, the output behaves like a signed integer instead of a long. I've printed out the a's and switch occurs when a3 = 127. I've also tried
- typing the Serial.reads as long
- typing and shifting the reads in a single statement into a long.
All give the same result.
Any insight is welcomed,
John-