Communication between two Arduino Boards

Arduino should read user input string from serial monitor and send it to another arduino through SPI connection. I have done Master and Slave code for arduino but i cant read User input from serial monitor from arduino1 and pass to arduino2. This is Master code and it sends "Hey" but i need to send user input string from serial monitor over SPI. How i can get from user input string from serial monitor and send to arduino1 and send to arduino2 over SPI.

#include <SPI.h>

void setup (void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}

void loop (void) {
String in="Hey\n";
int str_len = in.length()+1;
char input[str_len];  
in.toCharArray(input,str_len);
char *p=input;
char c;
digitalWrite(SS, LOW); // enable Slave Select
// send test string
for (* p; c = *p; p++) 
{
  SPI.transfer (c);
  Serial.print(c);
}
digitalWrite(SS, HIGH); // disable Slave Select
delay(2000);
}

This is Slave code:

#include <SPI.h>
char buff [100];
volatile byte indx;
volatile boolean process;

void setup (void) {
Serial.begin (115200);

SPCR |= bit (SPE); // turn on SPI in slave mode
pinMode(MISO, OUTPUT);
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}

ISR (SPI_STC_vect) // SPI interrupt routine 
{ 
char c = SPDR; // read byte from SPI Data Register
if (indx < (sizeof (buff) - 1)) {
  buff [indx++] = c; // save data in the next index in the array buff
}
  if (c == '\n'){ //check for the end of the word
  process = true;
  }
}

void loop (void) {
if (process) {
  buff[indx] = 0;
  Serial.println (buff); //print the array on serial monitor
   process = false; //reset the process
  indx= 0; //reset button to zero
}
}

The following codes grasp the 'newline terminated string' coming from the InputBox of the Serial Monitor; the string is saved in a character type buffer named myArray[].

void loop()
{
   byte x = Serial.available();
   if (n !=0)
   {
       Serial.readBytesUntil('\n', myArray, 50);   //declare in global area: char myArray[50] = "";
   }

To add 'newline' character at the end of the inputted string of the InputBox, select the newline option in the Line ending tab of the Serial Monitor.
SerialMonitor.png

Arrange to send the content (the string) of the array (myArray[]) from Arduino1 to Arduino2 via SPI Port.

for (int i=0; i<sizeof(myArray), i++)
{
    SPI.transfer(myArray[i]);
}
SPI.transfer('\n');  //end mark

You may visit this link to see how the string "Arduino!" is being sent from SPI UNO Master to SPI NANO Slave.

SerialMonitor.png

K4MR4N:
Arduino should read user input string from serial monitor and send it to another arduino through SPI connection. I have done Master and Slave code for arduino but i cant read User input from serial monitor from arduino1 and pass to arduino2. This is Master code and it sends "Hey" but i need to send user input string from serial monitor over SPI. How i can get from user input string from serial monitor and send to arduino1 and send to arduino2 over SPI.

That is not very clear but I think you are saying that you can receive data correctly from the Serial Monitor on ArduinoA but you can't send the data from ArduinoA to ArduinoB using SPI? Am I correct?

If so I suggest you start with a small program that just sends 'hello" from ArduinoA to ArduinoB using SPI - i.e. with no Serial complications.

...R

Serial is S. L. O. W.

Try not to think of strings with serial. Think of it more like isolated characters. The Arduino can do thousands of things in between each character, even at the highest serial baud rate. One of those things might be sending the character by SPI.