Hello everyone, this is my first post on the forum...
I am trying to communicate between 2 arduinos using SPI. I am using standard SPI library. I can send 1 float number by splitting it to 4 bytes on master side and sending by one by. On the slave side I read them in a byte buffer array and I am converting it to a float number.
Here is the question. I want to send 2 float numbers. So on slave side I have byte array which has 8 elements. I can convert the first 4 elements to my first float number. How can I convert the other number using the rest of byte array data?
master side:
float test_var = 345.646,
test_var2 = 596.729;
void loop(void)
{
spi_float_transfer(test_var);
spi_float_transfer(test_var2);
delay(3000);
}
void spi_float_transfer(float inp){
byte* i=(byte*) &inp;
digitalWrite(SS, LOW);
delayMicroseconds(150);
SPI.transfer(i[0]);
delayMicroseconds(150);
SPI.transfer(i[1]);
delayMicroseconds(150);
SPI.transfer(i[2]);
delayMicroseconds(150);
SPI.transfer(i[3]);
digitalWrite(SS, HIGH);
delayMicroseconds(150);
}
slave side:
void loop(void)
{
if(i==4){
my_f_val = *((float*)(b));
((float *)b) = my_f_val;
Serial.println(my_f_val,3);
i=0;
}
}
ISR (SPI_STC_vect) //Inerrrput routine function
{
b[i] = SPDR;
i++;
}
The same as you do on the master side. Increase the receiver array's size to 8 bytes, in the loop check if i equals 8 and then cast the address of the array's first element to an array of type float
Something like this (not tested):
byte receivedBytes[8];
uint8_t receiverIndex;
void loop(void)
{
if(receiverIndex >= 8)
{
float* receivedValues = reinterpret_cast<float*>(receivedBytes);
// display the received floats
Serial.println(receivedValues[0], 3);
Serial.println(receivedValues[1], 3);
receiverIndex = 0;
}
}
ISR (SPI_STC_vect)
{
receivedBytes[receiverIndex++] = SPDR;
}
Another approach is to use a union, which works in either direction and makes code easy to read.
union f2byte { float f[2]; byte b[8]} vals;
//example of use
vals.f[0] = 1.234;
vals.f[1]= 2.345;
send_message(vals.b, 8);
...
get_message(vals.b);
Serial.println(f[0]);
@OP
Unfortunately, you have not posted complete codes of your sketches; moreover, your codes are very cryptic at the Slave side to convert the received bytes into float. Frankly speaking, I could not compile your sketches even having had added the missing codes. Please, post the complete codes,
BTW:
I usually execute the following sketches to transfer multiple float data via SPI Port.
SPI Master UNO Codes
#include<SPI.h>
union
{
float txFloat;
byte txArray[4];
} txData;
void setup()
{
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV128);
digitalWrite(SS, LOW); //Slave is selected
}
void loop(void)
{
txData.txFloat = 345.646;
SPI.transfer(txData.txArray, sizeof(txData.txArray));
//---------------------------------------------------
txData.txFloat = 596.729;
SPI.transfer(txData.txArray, sizeof(txData.txArray));
//--------------------------------------------------
delay(3000);
}
SPI Slave NANO Codes
#include<SPI.h>
volatile int i = 0;
float test_var;
float test_var2;
byte b[8];
union
{
float rxFloat;
byte rxArray[4];
} rxData;
void setup()
{
Serial.begin(9600);
pinMode(SS, INPUT_PULLUP);
pinMode(MOSI, OUTPUT);
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
void loop(void)
{
if (i == 8)
{
{
for (int j = 0, i = 0; j < 4, i < 4; j++, i++)
{
rxData.rxArray[j] = b[i];
}
test_var = rxData.rxFloat;
Serial.print("Received test_var as: ");
Serial.println(test_var, 3);
}
//------------------------------
{
for (int j = 0, i = 4; j < 4, i < 8; j++, i++)
{
rxData.rxArray[j] = b[i];
}
test_var2 = rxData.rxFloat;
Serial.print("Received test_var2 as: ");
Serial.println(test_var2, 3);
}
//----------------------------------
i=0;
Serial.println("=============================================");
}
}
ISR (SPI_STC_vect) //Inerrrput routine function
{
b[i] = SPDR;
i++;
}

