SPI Communication Master to Slave

The Arduinos iam using
Mega 2560 (master)
Uno (slave)

I have the SPI communication kind of working between the two Arduino is in this example I’m trying to send the number 1000 from the master to the slave.

When I view the master and slave in the serial monitor the master is sending the number correctly by breaking it up and sending it as a byte array but when Slade receives it it puts it back together but not all the time this is how it is displayed on the serial monitor on the slave.

1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 0 0 0
3 3 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0
1000 232 3 0 0

The first number is the number received and the four after that is that byte array.

Currently the number received is at 1000 then for a few milliseconds it is 3 then goes back up to the thousand and constantly repeats this process.

I need the first number to constantly be displaying 1000 as this will eventually turn into the delay to run a stepper motor at an exact speed.

Can anyone help me get round this problem.

here is the code for the master (Arduino mega).

#include <SPI.h>

// SPI variables 
  unsigned char byteArray[5];
  unsigned long int Nosent=1000;

 
void setup(void) 
{ 
//SPI setup
     Serial.begin(115200) ;
     digitalWrite(SS, HIGH); // initialize SS high 
     
   // initialize the SPI for master Mode
     SPI.begin();
   
   //slow the clock rate by 8
     SPI.setClockDivider(SPI_CLOCK_DIV8); 
 
}

void loop(void)
{

//Convert long to byte array
    void IntToByteArray(long Nosent);
    {
       for (int i = 0; i < 4; i++)
         {
         byteArray[i] = ((Nosent >> (8 * i)) & 0XFF);
         }
    }


// start SPI transfer
  digitalWrite(SS, LOW); 

    SPI.transfer(byteArray[0]);
    SPI.transfer(byteArray[1]);
    SPI.transfer(byteArray[2]);
    SPI.transfer(byteArray[3]);

 // end SPI transfer
     digitalWrite(SS, HIGH); 
  
 // print  
  Serial.print(" ");
  Serial.print(Nosent);
  Serial.print(" ");
  Serial.print(byteArray[0]); 
  Serial.print(" ");
  Serial.print(byteArray[1]);
  Serial.print(" ");
  Serial.print(byteArray[2]); 
  Serial.print(" ");
  Serial.println(byteArray[3]); 
delay(10);

  
}

Here is the code for the slave (Arduino Uno).

#include <SPI.h> 

// SPI variables 
unsigned char byteArray[5];
volatile int pos;
volatile boolean process_it; 
byte recmicstep;
 unsigned long int micstepnew;



void setup(void) 
{   
  
  Serial.begin(115200);
  
 // SPI setup
  //now turn on interrupt
      SPI.detachInterrupt();
  
   //ste MISO pin as out put
     pinMode(MISO, OUTPUT);
     
   // turn on SPI in slave mode
      SPCR |= bit (SPE);
     
  // get ready for an interrupt 
  pos = 0;   // buffer empty
  process_it = false;
      
   //now turn on interrupt
      SPI.attachInterrupt();

 
}

// SPI interrupt service routine 
ISR (SPI_STC_vect) 
{
  recmicstep = SPDR;
     if(pos < 5)
       {
         byteArray [pos++] = recmicstep;
       }
  process_it = true; 
}
  
void loop(void)
{

// Convert byte array to long 
   long  recoveredValue = 0;
    for (int i = 0; i < 4; i++)
      {
        auto byteVal = (((long long)byteArray[i]) << (8 * i));
        recoveredValue = recoveredValue + byteVal;
      }

micstepnew = recoveredValue; 
      pos = 0;
      process_it = false;
      
 // print  
  Serial.print(" ");
  Serial.print(recoveredValue);
  Serial.print(" ");
  Serial.print(byteArray[0]); 
  Serial.print(" ");
  Serial.print(byteArray[1]);
  Serial.print(" ");
  Serial.print(byteArray[2]); 
  Serial.print(" ");
  Serial.println(byteArray[3]); 

  
}

Thank you for your help

Jim

You don't check if you already received the 4 bytes. You convert the byte array into a long regardless where the reception process is at the moment. After the conversion you reset the pos variable as if you really did receive all bytes. Looking at this it's interesting that you were successful that often, my expectations would have been much worse.