Micro to Uno serial Tx/Rx troubleshooting issues

TL,DR: Data from accelerometer routed to Micro Tx line and received by Uno Rx line, displayed on serial monitor, not congruent with known calibration data from accelerometer.

I am trying to link a 3-axis accelerometer via the Micro to the Uno using the built-in serial line. Once the data is received on the Uno, I need to combine it with data from 4 force sensors and route it through a bluetooth transceiver to be received by a phone application. Due to strange output from a bluetooth app on my phone, I removed that part of the circuit and currently have the data from the micro coming in on Rx and printing one force sensor and that data to the serial monitor. The issue is that when I test data from the Micro using Keyboard functions instead of serial, I get values for each axis (the accelerometer is taped to a table) that follow an arc that starts around 150 and after about two minutes (or 2000 iterations of data) level off at about 220. When I route this data to the Uno via Tx, and call for serial data on the Uno, I pull in values that bounce around wildly from 150-250. Any ideas on why this isnt working the way I think it is?

Below is the code for each device. I have also attached an image of my circuit (2014-04-21 18.23.38.jpg), a graph of the correct accelerometer data (accelerometer calibration.PNG ), and a graph of the serial data from the Uno(Uno data in.PNG ).

Here is my code for the Micro:

const int xpin = A0;                  // x-axis 
const int ypin = A1;                  // y-axis
const int zpin = A2;                  // z-axis 

void setup()
{
  Serial1.begin(115200);
  delay(5000); 
}

void loop()
{
    Serial1.print(analogRead(xpin));
    Serial1.print(analogRead(ypin));
    Serial1.print(analogRead(zpin));
    delay(100);
}

Here is the code from the Uno:

int flexiForcePin = A0;               //force sensor

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int flexiForceReading = analogRead(flexiForcePin); 
  float voltage = flexiForceReading * (5.0/1023.0);

  while(Serial.available() < 3)       //wait for 3 bytes in the buffer
    Serial.print(voltage);
    Serial.print("\t");
    Serial.print(Serial.read());
    Serial.print("\t");
    Serial.print(Serial.read());
    Serial.print("\t");
    Serial.println(Serial.read());
    delay(100);                  
}

I am using a 3-axis accelerometer MMA7361. link: https://www.sparkfun.com/products/9652

Here's one issue...

Serial1.begin(115200);

And..

Serial.begin(9600);

Ok, I fixed that. I dont know how I forgot to change them both…

The loop is bypassing the force sensor value. Do I need to break the while loop, and if so, what is the syntax for that?

Also, the new data is shown in the attached image. Does serial convert the integers to bytes on its own? If so, how do I convert them back to the correct values?

I would suggest that you take this one piece at a time. For now just print out on the UNO everything that is received from the Micro. Something like this…

void loop() {
  while (Serial.available()) {
    Serial.write(Serial.read());
  }
}

Once that is working you can begin to think about what type of message format you want to use to pass data.

This is almost certainly not what you want…

while(Serial.available() < 3)

Take a moment to think about what this does.

running the code below results in the attached output in one line in the serial monitor.

int flexiForcePin = A0;            

void setup()
{
  Serial.begin(9600);
}

void loop(){
  int flexiForceReading = analogRead(flexiForcePin); 
  float voltage = flexiForceReading * (5.0/1023.0);

  while (Serial.available()) {
    Serial.write(Serial.read() * (1024/255));
  }
  delay(100);                       
}

I had been using the other command to be able to call read() multiple times and get it to read the next value instead of three iterations of the same value. It helped with getting me to the point where I had different values printing in each column of data in the serial monitor. Unfortunately, the data had been just as jumbled as it is now. I should be getting three trendlines varying about ± 10, rather than the strange array of data in the image attached. Any idea what else it could be?

Once you see (in plain text and not on a chart) the output coming from the Micro you will have a better idea about your problem.

void loop()
{
    Serial1.print(analogRead(xpin));
    Serial1.print(analogRead(ypin));
    Serial1.print(analogRead(zpin));
    delay(100);
}

The analogRead() function returns an integer between 0 and 1023. The print function formats this integer in ASCII and sends it out the serial port. So assuming that the three analog values are 876, 239 and 74, this is what appears on the serial port: 87623974.

Sorry but from the picture i don't see common ground (on both boards) do you have it ? if not do it and then try again.

I tried a simultaneous comparison test to see real time what values I should be receiving, and the values are vastly different. In the attached excel file, I have two sets of data, to the right are three columns, one for each axis, and the other set of data is the serial monitor output. Just looking at the first few lines show that the data is not correct. Even with a different constant multiplied by the output variable, these numbers are not my data.

test 3.xlsx (34.1 KB)

can you be a little more specific about what you mean by common ground? I have both circuits grounded to the on-board ground pins...

The raw output from the serial monitor (reversing the calculation i tried to make to account for bytes/integer conversion) results in numbers at or around 50, which is also incorrect.

At last, a solution!

Turns out the major error was using serial.print instead of serial.write.

Thanks for all the help!