SPI Master-Slave data transmition with correct timing

Hey Folks
I’m building a data aquisition unit consiting of three Arduino Mega2560. Currently I’m just trying to make two of them talk to each other with SPI.
The slave is going to read out the sensors and then send the data to the Master. Since i can only send 1 byte at a time, I take the sensor value apart, send two bytes and put it back together on the Master side.

I read up on SPI and as far as I understand the SPI.transfer sends and receives simultaneously. Therefore I let the Master initiate a transfer with a specific command byte which calls a function on the slave. The function sends back the data to the Master.

Here is the code so far:


#include "SPI.h" 
#define SS_PIN    53
SPISettings mySettings;

void useClockSpeed(unsigned long clock) {
  mySettings = SPISettings(clock, MSBFIRST, SPI_MODE3);

int getSensor(void) {
  digitalWrite(SS_PIN, LOW);

  // Send start Byte
  // Get first Byte
  byte first = SPI.transfer('1');
  // Debugging
  Serial.println("First Byte: ");
  // Get second Byte
  byte second = SPI.transfer(2);
  // Debugging
  Serial.println("Second Byte: ");
  // End transmission
  digitalWrite(SS_PIN, HIGH);

  // Calculating actual sensor data
  Serial.println("Actual Data:");
  int value = (first << 8) | second;
  // Debugging
  return value;

void setup() {
  pinMode(SS_PIN , OUTPUT);
  digitalWrite(SS_PIN, HIGH);
  SPI.begin(); // wake up the SPI bus.


void loop() {
  // Will later be a for loop to get senor data of several sensors



#include "SPI.h"

#define SCK_PIN   52
#define MISO_PIN  50
#define MOSI_PIN  51
#define SS_PIN    53
#define Sensor1   A0

void SlaveInit(void) {
  // Set MISO output, all others input
  pinMode(SCK_PIN, INPUT);
  pinMode(MOSI_PIN, INPUT);
  pinMode(MISO_PIN, OUTPUT);
  pinMode(SS_PIN, INPUT);

  // Enable SPI
  SPCR = B00000000;
  SPCR = (1 << SPE);

void SensorTransfer(int Pin) {
  //int Sensor = analogRead(Pin);
  //Dummy value until transfer works and actual sensors are hooked up
  int Sensor = Pin; 
  // Shifts 8 positions to right. Here the number 2 results
  byte one = Sensor >> 8;
  // transmit first byte
  byte rx1 = SPI.transfer(one);
  // Debugging
  Serial.print("First receive: ");
  // Getting second part of the number. Here it is 108
  byte two = Sensor & 0xFF;
  // transmit second byte
  byte rx2 = SPI.transfer(two);
  // Debugging
  Serial.print("Seconde receive: ");


void setup() {

void loop() {

  // Wake up slave and execute command
  byte select = SPI.transfer(1);
  // Debugging
  Serial.print("Got Select byte: ");
  // If Master asks for Sensor Transfer - do so
  if(select == 's'){

However I have some timing problems with the whole SPI.transfer business.
With this code i get this on the serial monitor most of the time

First Byte: 
Second Byte: 
Actual Data:

However i seem only to get this correct readout when the delays in the masters getSensor function are all set to 100. With other values i either get the bytes swaped resulting in the wrong actual data number, the numbers sent get echoed back or sometimes even utter nonsense.
How do I tackle the problem that I always get the right ‘pairs’ of SPI.transfer to talk to each other? And also how can it be done faster. Since I’m aiming on getting 200Hz readouts with 8 sensors hooked up on each Arduino, a delay of 100ms like in the code wouldn’t work at all.

On this note just one more question: is it even possible to analogRead 8 sensors and send the 16bytes back to the Master in less than 5ms (200Hz)?

Best Regards,