Hi
I have a circuit set up with 3 accelerometer's and a gyroscope. The data from these sensors are going to be processed using an Arduino Uno and sent through an Xbee 802.15.4 to an another Xbee module and then sent through another Ardunio before the data is displayed on Megunolink. I am planning on using a parsing function to separate the data the being sent from xbee module.
I am trying to send the data like so
xbee.print(scaledX);
xbee.print(",");
xbee.print(scaledY);
xbee.print(",");
and read the data like so
Serial.print(xbee.read());
if (Serial.find("P")) {
// read and parse characters before the comma:
int X = Serial.parseInt();
// read and parse characters after the comma:
int Y = Serial.parseInt();
Serial.print(X);
Serial.print("X");
Serial.print(Y);
Serial.print("Y");
however I seem to be getting a unbroken stream of numbers such as 157156 the values should be around 4.02.
My code is attached below as well as copied below. Thanks for reading any help would be greatly appreciated.
include <Wire.h>
#include "MegunoLink.h"
#include <SoftwareSerial.h>
char XF = 'X';
int scale = 200;
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
TimePlot MyPlot("Waveforms");
TimePlot MyPlot1("Waveforms1");
SoftwareSerial xbee(2, 3); // RX, TX
char c = 'A';
int pingPong = 1;
int L3G4200D_Address = 105; //I2C address of the L3G4200D
int x;
int y;
int z;
void setup(){
xbee.begin( 115200 );
MyPlot.SetTitle("Accelerometer");
MyPlot.SetXlabel("Time");
MyPlot.SetYlabel("G");
MyPlot.SetSeriesProperties("X", Plot::Blue, Plot::Solid, 2, Plot::Square);
MyPlot1.SetTitle("Accelerometer1");
MyPlot1.SetXlabel("Time");
MyPlot1.SetYlabel("G");
MyPlot1.SetSeriesProperties("Resultant1", Plot::Blue, Plot::Solid, 2, Plot::Square);
Wire.begin();
Serial.begin(115200);
Serial.println("starting up L3G4200D");
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec
delay(1500); //wait for the sensor to be ready
}
void loop(){
getGyroValues(); // This will update x, y, and z with new values
Serial.print("Xgyro:");
Serial.print(x);
Serial.print(" Ygyro:");
Serial.print(y);
Serial.print(" Zgyro:");
Serial.println(z);
MyPlot.SendData(F("x"),x);
MyPlot.SendData(F("y"),y);
MyPlot.SendData(F("z"),z);
// Get raw accelerometer data for each axis
int rawX = analogRead(A0);
int rawY = analogRead(A1);
int rawZ = analogRead(A2);
// Scale accelerometer ADC readings into common units
// Scale map depends on if using a 5V or 3.3V microcontroller
float scaledX, scaledY, scaledZ; // Scaled values for each axis
scaledX = mapf(rawX, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
scaledY = mapf(rawY, 0, 675, -scale, scale);
scaledZ = mapf(rawZ, 0, 675, -scale, scale);
// Print out raw X,Y,Z accelerometer readings
Serial.print("Xraw: "); Serial.println(rawX);
Serial.print("Yraw: "); Serial.println(rawY);
Serial.print("Zraw: "); Serial.println(rawZ);
Serial.println();
// Print out scaled X,Y,Z accelerometer readings
Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");
Serial.println();
MyPlot1.SendData(F("Scaledx"),scaledX);
MyPlot1.SendData(F("Scaledy"),scaledY);
MyPlot1.SendData(F("ScaledZ"),scaledZ);
xbee.print(scaledX);
xbee.print(",");
xbee.print(scaledY);
xbee.print(",");
void TableData(String Name, float Value, String Description);
{
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("x");
Serial.print("|");
Serial.print(scaledX);
Serial.print("|");
Serial.print("g");
Serial.println("}");
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("y");
Serial.print("|");
Serial.print(scaledY);
Serial.print("|");
Serial.print("g");
Serial.println("}");
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("z");
Serial.print("|");
Serial.print(scaledZ);
Serial.print("|");
Serial.print("g");
Serial.println("}");}
void TableData1(String Name, float Value, String Description);
{
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("Xgyro");
Serial.print("|");
Serial.print(x);
Serial.print("|");
Serial.print("g");
Serial.println("}");
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("Ygyro");
Serial.print("|");
Serial.print(y);
Serial.print("|");
Serial.print("g");
Serial.println("}");
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print("Zgyro");
Serial.print("|");
Serial.print(z);
Serial.print("|");
Serial.print("g");
Serial.println("}");}
delay(500); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
}
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void getGyroValues(){
byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);
byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);
byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}
int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun's code
// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);
// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
// CTRL_REG4 controls the full-scale range, among other things:
if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}
// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
int readRegister(int deviceAddress, byte address){
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // read a byte
while(!Wire.available()) {
// waiting
}
v = Wire.read();
return v;
}
RF3.ino (870 Bytes)