Shop OBEX P1 Docs P2 Docs Learn Events
xbee problem with memsic2125 accelerometer — Parallax Forums

xbee problem with memsic2125 accelerometer

yeyeee01yeyeee01 Posts: 2
edited 2010-10-03 06:34 in Accessories
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.
//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;
}

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-09-19 10:35
    You should probably ask this question in the Arduino forums because the problem seems to be in the programming of the arduinos not the 2125. You could hook the 2125/arduino directly to your computer first and test that.

    Also you should go delete your duplicate post from the other forum here as that is not allowed in the forum rules.
  • Rob_WRob_W Posts: 32
    edited 2010-10-02 04:09
    I put your code in Visual Studio 6.0 and noticed that in the transmit code, under your Serial Variables declarations, the second line " byte packet[( packetLength - 1)] ", the use of the word "byte" is not recognized as a keyword. This caused my compiler to produce a C2146 error code, i.e., F:\Tx.cpp(7) : error C2146: syntax error : missing ';' before identifier 'packet'. This leads me to believe your data type isn't right???
    The receive code compiles correctly when I commented out the include file which I do not have. See attached jpg.
    1280 x 1024 - 521K
    byte.jpg 521.3K
  • FranklinFranklin Posts: 4,747
    edited 2010-10-02 17:54
    Rob, thats because this isn't visual c++ but Arduino code.
  • Rob_WRob_W Posts: 32
    edited 2010-10-03 06:34
    Thank You for the clarification Franklin.
    I couldn't find a reason for the code not to compile other than the keyword byte, however, it was fun trying to "make it work". My interest is in Parallax products for learning micro-processing / programming.
Sign In or Register to comment.