SPI HELP. 2 Arduinos one as master the other as slave

I have looked at many of the SPI help topics and have pulled some of the my code for the slave from them.

I have used the master code before to just write to a DAC but not to read any values sent from a slave back to it.
I have also used the arduino as a slave to read from a pic16f1827 a/d channel successfully so ignore that commented code for now.

I'm just trying to send a byte or two and see if the slave/master receive it correctly(i plan to add to this later to finish a project I'm working on but for now i just want to get SPI working correctly). It seems to work and gets the correct values for the first display but the second time it prints it the data it prints the values i'm trying to send not the ones i received.

Serial Monitor Display
Setting Pin is low...
Command: 100
: 200
25
: 50
Setting Pin is low...
Command: 100
: 200
200
: 100
Setting Pin is low...
Command: 100
: 200
25
: 50
Setting Pin is low...
Command: 100
: 200
200
: 100
Setting Pin is low...
Command: 100
: 200
25
: 50
Setting Pin is low...
Command: 100
: 200
200
: 100

So you can see every other print is that of the transmission data and not that of what the incoming data should be and I have no idea why this is happening and any help would be great on how to fix this.

Master code

// Master
#include <SPI.h>
#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

unsigned long int adc_num1;
double adc_num;
    byte rxData;
    byte rxData2;
    byte txData;
    byte txData2;

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

unsigned long lastSent;

void setup() {
  Serial.begin(57600);
  digitalWrite(5, HIGH);
  SlaveInit();
  lastSent = millis();
  pinMode(5, OUTPUT);
}

void loop() {
  
  setup();
  
  while(1){
  
delay(500);
delay(500);
delay(500);
delay(500);
    
    Serial.println("Setting Pin is low...");


    txData=100;
    txData2=200;

    
SPI.begin();
 SPI.setBitOrder(MSBFIRST); // MSB first bit order
 SPI.setDataMode(SPI_MODE0); //SPI Mode 0
 SPI.setClockDivider(SPI_CLOCK_DIV8); // 16MHZ/8 = 1MHZ
 digitalWrite(SS_PIN,LOW);
 rxData = SPI.transfer(txData); //
 rxData2 = SPI.transfer(txData2); //
 digitalWrite(SS_PIN,HIGH);
 SPI.end();
    
    Serial.print("Command: ");
    Serial.println(txData, DEC);
    Serial.print(" : ");
    Serial.println(txData2, DEC);
    Serial.println(rxData, DEC);
    Serial.print(" : ");
    Serial.println(rxData2, DEC);
    
//adc_num1 = rxData;
//adc_num1 = (adc_num1 << 8) + rxData2;
//adc_num = double(adc_num1)/65472*4.72;


/*
    Serial.println(adc_num, DEC);
    if (rxData == 17) {
      Serial.println("Sending data to master...");
      WriteByte(19);
      Serial.println("Done Sending data...");
    }
  }
  if (millis() > lastSent + 2000) {
    Serial.println("Pin 5 low...");
    digitalWrite(5, LOW);
    delay(10);
    digitalWrite(5, HIGH);
    lastSent = millis();
  }
  */
}
  
}

Slave Code

// Slave

#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11
#define SS_PIN    10

unsigned long int adc_num1;
double adc_num;

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);
}

byte ReadByte(void) {
  while(!(SPSR & (1<<SPIF))) ;
  return SPDR;
}

void WriteByte(byte value) {
  SPDR = value;
  while (!(SPSR & (1<<SPIF))) ;
  return;
}

unsigned long lastSent;

void setup() {
  Serial.begin(57600);
  digitalWrite(5, HIGH);
  SlaveInit();
  lastSent = millis();
  pinMode(5, OUTPUT);
}

void loop() {
  if (digitalRead(10) == LOW) {
    Serial.println("Pin 10 low...");
    byte rxData;
    byte rxData2;
    byte txData;
    byte txData2;
    txData = 25;
    txData2 = 50;
    rxData = ReadByte();
    WriteByte(txData);
    rxData2 = ReadByte();
    WriteByte(txData2);
    Serial.print("Command: ");
    Serial.println(rxData, DEC);
    Serial.print(" : ");
    Serial.println(rxData2, DEC);
//adc_num1 = rxData;
//adc_num1 = (adc_num1 << 8) + rxData2;
//adc_num = double(adc_num1)/65472*4.72;
//    Serial.println(adc_num, DEC);
    if (rxData == 17) {
      Serial.println("Sending data to master...");
      WriteByte(19);
      Serial.println("Done Sending data...");
    }
  }
  if (millis() > lastSent + 2000) {
    Serial.println("Pin 5 low...");
    digitalWrite(5, LOW);
    delay(10);
    digitalWrite(5, HIGH);
    lastSent = millis();
  }
}
  1. I wouldn't do SPI.begin () and SPI.end () every time through the loop. Do the SPI.begin () once in setup.

  2. SPI.begin () does what your SlaveInit does, so I wouldn't bother doing that at all.

  3. I wouldn't call setup from loop.

  4. Loop automatically loops, you don't need to do a while (1) in it.

  5. Don't do the slave like this:

if (digitalRead(10) == LOW) 
// blah blah

You need an interrupt handler.

  1. You don't need all this stuff:
 while(!(SPSR & (1<<SPIF))) ;

It's all in the SPI library.

I have examples of doing an Arduino SPI master and slave here:

(two-thirds of the way down the page is the relevant bit).