I2C between nano and mega unstable

I am working on a car that communicates wirelessly with a controler, after a while struggling with the SPI of the mega (which drives the car) I have decided to place a 2nd arduino (nano) in the car that communication does ... this is connected to a bluetooth module and a nrf24l01 with anthene, the code between the nano of the car and the nano in the controller over the nrf24l01 works fine, but if I want to forward the value of the nano in the car to the mega in the car then the values are correct but it always changes variable (one time it puts the value in one variable and the other time in another) so the value is useless :frowning:
I don't understand what it is problem, I hope someone knows a solution, I have a picture of a schematic I quickly made in paint and a screenshot of the value I got in the serial monitor

I have attached the codes (I use different tabs so it is difficult to place directly)

  • the one that always ends with "_PCT" is the code of the controller
  • the one that ends in "_PCN" is that of the nano in the car
  • the one that ends with "_PCM2" is that of the mega in the car

Isaak

schema.png

codes.zip (7.74 KB)

minor update, i think i know where the problem is but i don't know how to fix it!
the problem is with the nano on the car that gets the info from the controller and has to send it to the mega.
I think there is something wrong with the timing.
he should only steer when he has received everything correctly from the controller ...

Isaak

It is always better to fix a problem in the main Arduino board, instead of adding more Arduino boards. More Arduino boards will make it more complex and thus the chance for bugs increases.

Your files are very, very, very small. It's only a few bytes per file.
When a file is 2000 lines long, then I might put some functions in a new file.

You can make a schematic with pen on paper and make a photo of it.

Can you give a link to the OLED display that is connected to the I2C bus ? Preferably a link to where you bought it, so we know for sure which OLED display you have.
The OLED itself is 3.3V and your I2C bus is a 5V I2C bus.
Do you have pullup resistors on the I2C bus ? Which value ?
How long are the wires of the I2C bus ? Do you use a cable ?

In the Nano, your 'received_Package' and your 'updated_Package' are four integers, that is a total of 8 bytes.
The Wire.write() and Wire.read() use bytes. The data that is send and received over the I2C bus are packages of 4 bytes.
Do you want to use four bytes or four integers ?

In the Nano, you use SoftwareSerial.
The SoftwareSerial is an amazing piece of code, but it uses almost the entire Nano to do its job. When you use SoftwareSerial, then you can blink a led, but not much more.
For a reliable I2C communication, the sketch needs to respond to interrupts very quick. That is not possible when SoftwareSerial is running.

When a variable is used both in a interrupt routine and in the loop(), then it should be made 'volatile'. The onRequest and onReceive handlers are interrupt routines.

You have variables and you have an array to transfer over the I2C bus.
Often just one struct is used for that. A struct can be send with a single Wire.write() and a single Wire.readBytes().

I did not check if the variables are changed somehow when there was no command.

thanks, koepel
I understand your explanation and will apply some things,
I can not find anything in the arduino reference about a struckt.
have I not looked enough or is it not there?
and if it is not there, maybe you know where I can find it?

ps, spreek je nederlands? je gebruikersanaam is koepel en dat is een nederlands woord :wink:

c++ struct

User "Robin2" made a tutorial for I2C Master-Slave with struct : Use I2C for communication between Arduinos - Exhibition / Gallery - Arduino Forum.

Yes, I'm from the Netherlands. I noticed that you used "fout" and "schema" in your file names :wink:
There is also a Dutch section on this forum: Nederlands - Arduino Forum.