Hi,
I am trying to code my Arduino Mega2560 in slave mode using SPI and SPI_anything.h library to send a struct of one byte and six int16s to a Raspberry that is coded using Simulink support package. Arduino reads the sensors and need to feed RPi which is the master controller of a robotic platform.
I have followed Gammon's forum where he explaines the SPI_anything library.
This is my struct definition
typedef struct myStruct //1x7 size array
{
byte expe_spi_time;
int16_t IMU_ori;
int16_t IMU_ang_vel;
int16_t enc_count_int16; // int16_t enc_count;
int16_t knee_pot_meas; // int16_t knee_pot_meas;
int16_t curfed_4V; // int16_t curfed_4V;
int16_t accelero_z_ms2;
};
volatile myStruct from_simulink_rpi;
volatile myStruct buff;
volatile bool haveData;
and this is my interrupt service routine.
ISR (SPI_STC_vect) {
//SPI_readAnything_ISR (from_simulink_rpi) ;
SPDR = 1 ;
//delayMicroseconds(1);
SPI_writeAnything (buff);
haveData = true;
}
The problem is: When I use these codes with an easy handled (Two lines of +10 addition to some values to check from the master side if it is working) with a large delay (around 10 ms) in the void loop, everything works well, I receive all the data at my Simulink screen via running Raspberry Pi in external mode.
When I add my encoder and some I2C sensors in the loop to send to the master, data received at the master is very distorted. (Sensors and everything works fine, I check the readings in serial monitor)
What I mean by distorted is: The byte ordering changes randomly and causes ill readings for all 6 values that jumps around. For small brief moments some read values matches with the ones that is send from the Arduino but the problem is the consistency.
I tried adding some delaymicroseconds between SPDR commands in the .h file but it sometimes fix the problem temporarly, nothing consistent.
Any ideas why this can happen?
Cheers.