Sending Doubles and long numbers over SPI between two arduinos

hello friends , i need a small help, i am trying to send some double numbers and some long numbers with SPI between arduino slave to arduino master (both MEGA).

i was able to convert numbers into byte array.doubles were converted to long numbers first then it was splitted to 4 bytes along with other long numbers.

but when i send the data to master from slave all the long numbers have become within -32000-32000 roughly
that means it has become int.i cannot find the fault.

i wonder why SPI library is not there so that wecould send this data easily converted within such a library
please let me know if there is any.and if there is another way to convert and get the original back.

thank you for your precious time.attachment contains slave and masters code snippets.

shirantha

q1.zip (1.05 KB)

You don’t need to zip up such a small file. Here is its contents:

########################## Code of the Master calling for 40  bytes stred as array(actually they are the 10 long numbers)

void readEncoders(){

  //Serial.print("Satrt");
  //Serial.print(millis());
  digitalWrite(SS, LOW); 

  delayMicroseconds(10);
  transferAndWait ('a');
  delayMicroseconds(10);
  if(transferAndWait ('c')=='b'){

 //Serial.print("success");
  }


  for(int i=0;i<39;i++){

      recievedDataPacket[i]=transferAndWait('d');

  }

  recievedDataPacket[39]=transferAndWait('e');
  
  if (transferAndWait('g')=='f'){
    //Serial.print("success 2");
  }

  for (int i=0;i<10;i++){
    
    recievedLongData[i]=(unsigned long)(recievedDataPacket[4*i+3] << 24) | (recievedDataPacket[4*i+2]<< 16) | (recievedDataPacket[4*i+1] << 8) | recievedDataPacket[4*i];
  
  }
  //printState();
  
  digitalWrite(SS, HIGH);


  
  
  time_value=recievedLongData[0];
  x=(double)(recievedLongData[1]/1000.0);
  y=(double)(recievedLongData[2]/1000.0);
  thetaWhole=(double)(recievedLongData[3]/1000.0);

  newPosition1=recievedLongData[4];
  newPosition2=recievedLongData[5];
  newPosition3=recievedLongData[6];

  countFrequency1=(double)(recievedLongData[7]/1000.0);
  countFrequency2=(double)(recievedLongData[8]/1000.0);
  countFrequency3=(double)(recievedLongData[9]/1000.0);
}

byte transferAndWait (const byte what)
{
  byte a = SPI.transfer (what);
  delayMicroseconds (10);
  return a;
} // end of transferAndWait




########################################SlaVes Code:

ISR (SPI_STC_vect)
{
  //d- byte holder
  if (SPDR=='a'){
    SPDR='b';
    //Serial.print("s");
    byteNumber=0;
  }
  else if(byteNumber<40){

  d=dataPacket[byteNumber];
  SPDR=d;
  //Serial.print(byteNumber);
  
  byteNumber+=1;
  }
  else if(byteNumber==40 && SPDR=='e') {
    //Serial.println(milis());
    byteNumber=41;
    SPDR='f';
  }
} 


//for dtata packing at slave

void packData(){

  longData[0]=timeValue;//ms
  longData[1]=long(1000*x);//mm
  longData[2]=long(1000*y);//mm
  
  longData[3]=long(1000*thetaWhole);//mrad

  longData[4]=newPosition1;//encoder count long
  longData[5]=newPosition2;
  longData[6]=newPosition3;

  longData[7]=long(countFrequency1*1000);//m rps
  longData[8]=long(countFrequency2*1000);//m rps
  longData[9]=long(countFrequency3*1000);//m rps

  for (int i=0;i<10;i++){

      IntegerToBytes(longData[i],dataPacket,4*i);
  }
  /*
  IntegerToBytes(lx, byteArray);
  Serial.print(byteArray[0]);
  Serial.print(byteArray[1]);
  Serial.print(byteArray[2]);
  Serial.println(byteArray[3]);
*/


}

void IntegerToBytes(long n, byte buf[40], short bytePosition) {
  buf[bytePosition]   = (byte) n;
  buf[bytePosition+1] = (byte) (n >> 8);
  buf[bytePosition+2] = (byte) (n >> 16);
  buf[bytePosition+3] = (byte) (n >> 24);
}

See my thread about transferring any data type over SPI: Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

hi nick,

i learned about SPI from your article on spi,still its looks like that i have missed the last part.
thanks for pointing that up.it solved number of problems that i had.

thanks again.

you might update your code to a version that sends type information to the receiving end, so it can be determined runtime.

Ach, you'll be wanting remote procedure calls next!