Arduino as a slave to raspberry pi

I am working on an autonomous maze solving bot and decided to use raspberry pi for processing and arduino uno for working with the sensors.I have written the maze solving code in c++ and am looking for ways to communicate raspberry pi with arduino (i.e. most optimal protocol without converting my maze solving code to python).I also would like to know that my decision of using raspberry pi is correct or I can do the extensive processing work with arduino Due instead of using raspberry pi and arduino together.

An RPi and an Arduino can be a very effective combination.

You have provided no information about the direction, type or quantity of data that must flow between them.

A serial connection is probably the simplest. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

The Raspberry Pi seems like a good environment to learn C++, it has protected memory space so you will know when memory is trying to use other memory that is already in use. I use AVR's, and find that the R-Pi can interact with them using serial, SPI, or I2C. Each has advantages and disadvantages, which I can somewhat go over.

I'll start with I2C, for which I find there really is not an OS that can handle proper I2C correctly, the way they work with the bus is called SMBus.

The thing to know is that you have to make the AVR act like an SMBus device to have reliable communication between the AVR and R-Pi running Linux. I am sure the R-Pi hardware can do normal I2C when it is running bare metal embedded software and not Linux.

For SPI the R-Pi is a master so the AVR is a slave (again if the R-Pi is running Linux). The R-Pi has two slave select lines and both are easy to enable with the R-Pi config tools.

On the AVR side, for the SPI slave function, I have only done a little test program that echoes back the byte sent with an interrupt service routine function. SPI really makes sense when there needs to be a high data rate between the R-Pi and the AVR, but my projects are not yet to that point.

Uart or Serial is probably the one you want to use, it is easy and just works as expected. If you can figure out some functions that you want the AVR to do you can turn it into a command processor and just issue commands form the R-Pi to the AVR. The AVR could be used to do tightly coupled functions, like using a hall sensor to control a PWM. The trick is to figure how to abstract the coupled functions so the R-Pi can send a simple command to control it.

The new R-Pi's have a hardware UART that is probably used with a BLE radio, but I recommend trying to turn off the BLE and swapping the onboard hardware serial for use with your project. In my opinion hardware serial is more reliable than USB, and will use less power if that is a concern.