Arduino SPI Mutual Communication

Below are my codes for transferring string data between two Arduinos from Master to Slave via SPI. Similarly, how can I transfer string data from Slave to Master? Any suggestions?

Master Code:

#include <SPI.h>

char input;

const int size_arr = 150;  // define array size (must be less than size of slave)
char buf[size_arr]; // define char array, use for convert from string for send to slave
String main_dat = "hello from master"; // real String data

void setup() {
  Serial.begin(9600);
  pinMode(SS, OUTPUT);
  digitalWrite(SS, HIGH);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV16);
}

void loop() {
  input = Serial.read();

  if(input == 's'){
    main_dat.toCharArray(buf, size_arr); // convert String data to char array
    Spi_tranmis(buf); // send char array
  }
  
  delay(500);
}

void send_spi(byte pram) {

  digitalWrite(SS, LOW);
  SPI.transfer(pram);
  digitalWrite(SS, HIGH);
  delayMicroseconds(5);
}

void Spi_tranmis(char buf[]) {
  for (int i = 0 ; i < size_arr  ; i ++) {
    send_spi(buf[i]);
  }
  send_spi('\n');
}

Slave Code:

const int size_arr = 151; // define array size (must be more than size of master)
char buf[size_arr]; // char array , use for recive from master

#define SS_Pin 9

volatile byte pos = 0;
volatile boolean process = false;

String msg; // real data

void setup() {
   Serial.begin(9600);
   pinMode(MISO, OUTPUT);
   digitalWrite(MISO, LOW);

   pinMode(SS_Pin, OUTPUT);
   SPCR |= bit(SPE);
   SPCR |= bit(SPIE);
}

ISR(SPI_STC_vect) {
  char c = SPDR;

  if (pos < sizeof buf) {
    if (c == '\n') {
      buf[pos] = '\0';
      process = true;
    }
    else {
      buf[pos++] = c;
    }
  }
}

void loop() {
  recvHandle();
}

void recvHandle() {
  if (process) {  // when SPI available
    msg = String(buf);   // convert char array to String
    Serial.println(msg);
    process = false;
    pos = 0;
  }
}

I moved your topic to an appropriate forum category @enesorhaan.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

The Slave cannot initiate a SPI transaction. If the Master wants data, it must ask the Slave for the data.

So, can't I use both devices as master? What if I assign two different CS pins?

What about SCLK?

This is what I want help with. I don't know if it can be done. Mine is just an idea.

Well, with some additional signal lines, external hardware (some gates) and if you access the SPI registers directly, I guess you could kludge something together but I won't be able to help you with that.

If you need bidirectional asynchronous communications, I would use the serial UART.

UART is slower than SPI and is not a definitive solution for me. Thanks for the advice.

Which Arduino are you using?

Arduino Uno

If the Master wants data, it must request data from the Slave.

How can I achieve the data request you mentioned here?

SPI has no specific "request data function" you have to make your own.
Send a one byte code to the slave that the slave can examine so it knows what the master wants.
E.G. send an 0xAA to tell the slave that the master wants data xx.

I don't know exactly how to do this. I tried a few ways but I couldn't improve my codes above. Can you help?

I have never tried to make a SPI slave on an UNO. Maybe another forum member can help.

Have you read the response from @Delta_G ?

In my example above, I am sending the "hello from master" expression, which is already a string expression. I don't know how to get a response from the slave after the data I sent is finished and how I can receive and see it on the master!

I can summarize it as follows; I want to convert JSON type data into string data type between my two Arduino Uno cards and transfer it from Master to Slave. So I need to send string. I also tried with I2C and UART, but according to my research, SPI was faster. What are you thinking?

What I'm saying is quite simple: In the project, I want to transfer the JSON type data I receive from Ethernet from the master to the slave. A simple ethernet stepper motor drive project?

Actually, I told you about my project. Let me summarize again as follows:

I have an Ethernet Server on my own computer. This server is connected to my master device, Arduino, via ethernet and streams JSON type data. To give an example, this JSON data contains speed, direction and location information.

Then, my Master device receives this data and processes it within itself, and I send this data in JSON type separately to the Slave Arduino devices and try to control the Stepper Motors at the end. This is the plot of the project. I'm trying to communicate between Master and Slave right now. What more can I say? That is all.

What message/data/expression do you expect to receive from Slave-UNO while the Master-UNO has been transferring "Hello from Master" message to Slave?

Can I assume that you are expecting to receive this message: "Hello! from Slave"?

Yes, that's exactly what I'm waiting for.