Hi, I have a 10bit rotary encoder, which translates 360 degrees into a range of 0 -1023, and I want to send the value to Matlab. I'm trying to send 2 Bytes, using 8 bits in 1 Byte and 2 bits of an other byte. I'm send for example a value of 1024 and get 3252 send. The 10 bit value is send from the arduino to maltab when an integer greater than 19 is sent to the arduino.
This is my code so far.
int incomingvalue = 0;
unsigned int val; // This is a 16 bit value
byte msb; // 8 bit variable to hold the most significant byte
byte lsb; // 8 bit variable to hold the least significant byte
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if (Serial.available())
{
incomingvalue = (int)Serial.read();
if (incomingvalue > 19)
{
//this is the value that is to be send to matlab
val = 1020;
digitalWrite(13, HIGH); // set the LED on, communication ok
Serial.write(lowByte(val));
Serial.write(highByte(val));
}
if (incomingvalue < 19)
{
Serial.print(1);
}
}
}
Hope someone can help, thanks.