Xbee problem with with memsic2125 accelerometer
yeyeee01
Posts: 2
hello ther guys, im having an issue while tranmitting values form a memsic2125 accelerometer wirelessly. i have 2 xbees, one connected to an arduino nano. anotherone is connected to an arduin MEGA. my wireless communication is just fine as i can send vales from flex sensors one one side and control 5 servo motors on the other side. the range of values sent is just fine which is -1090-to-0-to-+1090. but the values received are 200-16-8-212 and so forth. i was wondering if some can tell me something that i dont know about this whole accelerometer and its pulse in funciton of maybe look at my code and let me know if im doing something wrong.
here is the code for the transmitting side.
here is the code for the transmitting side.
//Eric Semper //Senior Project //Sender Program // Serial Variables const int packetLength = 4; byte packet[(packetLength - 1)]; int checksum; // Flex Sensors Variables const int thumbflex = 1; const int indexflex = 5; const int midflex = 4; const int ringflex = 3; const int pinkyflex = 2; // Accelerometer Variabbles const int xpin = 4; const int ypin = 5; //int valx = 0; //int valy = 0; // Servo Motor Variables int thumbval; int indexval; int midval ; int ringval ; int pinkyval; void setup() { Serial.begin(38400); // Begin Serial Comunication pinMode(xpin,INPUT); //This variables will define two pins as an input pin for the accelerometer pinMode(ypin,INPUT); } void loop() { // Read all analog values from flex sensors thumbval = analogRead(thumbflex); indexval = analogRead(indexflex); midval = analogRead(midflex); ringval = analogRead(ringflex); pinkyval = analogRead(pinkyflex); //Map All Values read thumbval = map(thumbval, 190, 69,0,179 ); // scale it to use it with the servo (result between 0 and 179) indexval = map(indexval, 190, 69, 140, 1); // scale it to use it with the servo (result between 0 and 179) midval = map(midval, 189, 77, 155, 1); ringval = map(ringval, 180, 79, 85, 180); pinkyval = map(pinkyval, 171, 71, 100, 35); // variables to read the pulse widths: int pulseX, pulseY; int accelerationx, accelerationy; // variables to contain the resulting accelerations // read pulse from x- and y-axes: pulseX = pulseIn(xpin,HIGH); pulseY = pulseIn(ypin,HIGH); /* convert the pulse width into acceleration accelerationX and accelerationY are in milli-g's: earth's gravity is 1000 milli-g's, or 1g. */ accelerationx = ((pulseX / 10) - 500) * 8; accelerationy = ((pulseY / 10) - 500) * 8; /* This Section will Send all the variables obtained using the function "sendSerialData" created below */ createPacket(0x00, thumbval); sendSerialData(packet); delay(75); // Delay loop createPacket(0x01, indexval); sendSerialData(packet); delay(75); // Delay loop createPacket(0x02,midval); sendSerialData(packet); delay(75); // Delay loop createPacket(0x03,ringval); sendSerialData(packet); delay(75); // Delay loop createPacket(0x04,pinkyval); sendSerialData(packet); delay(75); // Delay loop createPacket(0x05,int(accelerationx)); sendSerialData(packet); delay(75); // Delay loop createPacket(0x06,accelerationy); sendSerialData(packet); delay(75); // Delay loop // print the acceleration Serial.print(accelerationx); // print a tab character: Serial.print("\t"); Serial.print(accelerationy); Serial.println(); } // Create Packet Data void createPacket(byte cmd, byte data) { packet[0] = 0x7E; packet[1] = cmd; packet[2] = data; // Create Checksum Value, (cmd + data) = 0xFF - (cmd + data) checksum = cmd + data; checksum = 0xFF - checksum; checksum = 0xFF & checksum; packet[(packetLength - 1)] = checksum; return; } // Send Packet Serially void sendSerialData(byte thePacket[3]) { Serial.print(thePacket[0], BYTE); Serial.print(thePacket[1], BYTE); Serial.print(thePacket[2], BYTE); Serial.print(thePacket[3], BYTE); return; }Here is the conde for the receiving side
//Eric Semper //Senior Project //Receiver Program #include <Servo.h> // Serial Variables int inByte = 0; // incoming serial byte int serialData[5]; int arrayCount = 0; int checksum; // Servo Motor Pin Variables /* const int servothumb = 13; const int servoindex = 12; const int servomid = 11; const int servoring = 10; const int servopinky = 9; const int servox = 8; const int servoy = 7; */ // Servo Motor Variables int thumbval; int indexval; int midval; int ringval; int pinkyval; int accelerationx; int accelerationy; // Servo Motor Object Variables Servo myservothumb; Servo myservoindex; Servo myservomid; Servo myservoring; Servo myservopinky; Servo myservox; Servo myservoy; void setup() { // Attach the Servo to the Object (Pin) myservothumb.attach(13); myservoindex.attach(12); myservomid.attach (11); myservoring.attach (10); myservopinky.attach (9); //myservox.attach (8); //myservoy.attach (7); Serial.begin(38400); } void loop() { // Read Serial Data In, if it is available if(Serial.available() > 0) { inByte = Serial.read(); // get incoming byte /*Load Serial data into a packet until 4 bytes is reached. Verify the the beginning of the packet with 0x7E */ if(inByte == 0x7E || arrayCount > 0 && arrayCount < 4) { serialData[arrayCount] = inByte; // Receive Data From Serial Port arrayCount++; // Increment the Array Counter } // Verify Checksum if a packet is received if(arrayCount == 4) { arrayCount = 0; // Reset Data Packet Counter checksum = serialData[1] + serialData[2]; // Add Bytes 1 & 2 for checksum checksum = 0xFF - checksum; // Subtract checksum from 0xFF checksum = 0xFF & checksum; if(checksum == serialData[3]) { // Checksum is good switch(serialData[1]) { case 0x00: // Servo thumb moveservothumb(serialData[2]); break; case 0x01: // Servo index moveservoindex(serialData[2]); break; case 0x02: moveservomid(serialData[2]); break; case 0x03: moveservoring(serialData[2]); break; case 0x04: moveservopinky(serialData[2]); break; case 0x05: moveservox(serialData[2]); break; case 0x06: moveservoy(serialData[2]); break; /* if (pinkyval >= 100) { xservo.write(accelerationx); yservo.write(accelerationy); delay(10); } */ } } else { // Checksum Failed Serial.println("Bad Packet"); } } } } void moveservothumb(int thumbval) { //Serial.println("Thumb"),("\t"),("Index"),("\t"),("Middle"),("\t"),("Ring"),("\t"),("Pinky"),("\t"),("X-Axis"),("\t"),("Y-Axis"); myservothumb.write(thumbval); // sets the servo position according to the scaled value return; } void moveservoindex(int indexval) { myservoindex.write(indexval); // sets the servo position according to the scaled value return; } void moveservomid(int midval) { myservomid.write(midval); // sets the servo position according to the scaled value return; } void moveservoring(int ringval) { myservoring.write(ringval); // sets the servo position according to the scaled value return; } void moveservopinky(int pinkyval) { myservopinky.write(pinkyval); // sets the servo position according to the scaled value return; } void moveservox(int accelerationx) { //Serial.print("\t"); //Serial.println(); accelerationx= map(accelerationx, -1090, 1090, 0, 179); Serial.print(accelerationx); return; } void moveservoy(int accelerationy) { Serial.print("\t"); //accelerationy= map(accelerationy, -1090, 1090, 0, 179); //accelerationy = map(accelerationx,0, Serial.print(accelerationy); Serial.println(); //Serial.println(thumbval),("\t"),(indexval),("\t"),(midval),("\t"),(ringval),("\t"),(pinkyval),("\t"),(accelerationx),("\t"),(accelerationy); delay(400); return; }