Modbus RTU incompatibility with Wire and SPI

Hi Folks,
I'm trying to make a system where I have two Arduios talking to each other over Modbus RS485.
The application is to control a two way radio, a CODAN 6801. The Arduino with the rotary encoder and display will be a remote control, and the second arduino will be inside the radio and drive a couple of relays and an AD9805 module. I want to use RS485 to link the two Arduinos as I believe it will be the most robust link.

To simplify testing and development, I made sketches for each section and a test circuit on breadboards.

The hardware in my test circuit is: 2x Arduino Uno, 2x MAX485 modules, 1x1306 OLED display, 1x KY-040 rotary encoder and 1x LCD display driven by I2C.

My plan was to create sketches to test each section of the system to simplify troubleshooting. I made sketches to test the Modbus link, encoder and screen, and AD9805 module. Each individual sketch works, so I'm confident it's not a hardware problem.

To test the Modbus link, I wrote a simple slave sketch that displays the Modbus registers that the master writes to.

My problem is that when I try to combine the Modbus link and encoder+screen sketches, the program fails. The program compiles and loads, but neither the screen or Modbus link work.

What it seems to come down to is that the Modbus, SPI and Wire libraries don't play together.

If I initiate the screen with

Adafruit_SSD1306 display(OLED_RESET);

The modbus and screen work, but the font on the screen skips every second line.
If I initiate the screen with

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

The display works correctly, but only if I comment out the Modbus section.

Is there some sort of limitation here? Does anyone have any tips for troubleshooting and resolving this?

remote_control.pdf (25.5 KB)

Combined_Test1.ino (3.96 KB)

What it seems to come down to is that the Modbus, SPI and Wire libraries don't play together.

I'm sure they do as I use them together without problems.

Is there some sort of limitation here? Does anyone have any tips for troubleshooting and resolving this?

Yes, the memory is limited. The SSD1306 library uses more than half of the memory just for the screen buffer. Using the String class is also a no-go on the AVR platform because it fragments the memory too fast.

So keep your globals to an absolute minimum, save memory where ever you can. If you compile your sketch the Arduino IDE should show more than 1200 bytes free at the end of the compilation.