I am building an "R2D2", and I need help with a plan for communication between all of them.
The set up:
Im using an Arduino Nano, and an nrf24l01 as a remote.
And one Arduino Mega to receive communication from the remote (nrf24l01) and perform most of the lower body functions (ie drive, lights, sounds exc...)
I have another Arduino Mega in the head that controls the head lights, and a lift system (series of servos and motors that move things in the head) and all the motors and servos for all the doors via several PWM servo motor drivers.
BOTH Megas and PWM servo motor drivers are connected Via an I2c network via the SCL and SDA pins.
Separately, they all work perfectly. My problem is NOW I would like the Remote to call functions (by means of the Body Arduino MEGA) on the HEAD Arduino MEGA, and I don't know if this can be done via the I2c, as I think the Head Arduino MEGA may be set up as a master to control the PWM servo motor drivers.
I did not Write the Lift portion of the Head sketch, but there does NOT seem to be any Reference in the code ie: Wire.begin(); to indicate that it is set up as the master.
Does an Arduino have to be set up as a master to send commands to a PWM servo motor driver? Or could I still set it up as a Slave to the BODY Arduino Mega to receive commands?
Right now, the Head Functions are being called using momentary switches in the head, and I'd like to have them work off the switches in the remote.
All of the sketches are too big too post but I have posted the set up portion of the Head Sketch (the one I'd like to be the slave to the Body Mega) to pastebin if anyone would like to take a look: Dome Mega Setup
I2C with a lot of luck and kluging might work. It was designed for on board communication to ICs, not inter board communications. If you want something robust look at CAN, RS485 and other bus systems.
You need to post a Schematic, not frizzy pictures showing how it is wired along with links to the technical info on appropriate hardware parts.
Why the choice of I2C? Serial communication (optionally with RS232/RS422/RS485 if distances are long) would be a lot easier. Your Megas have 4 UARTs, one allocated to USB. In the code that you posted, the pins for Serial1, Serial2 and Serial3 don't seem to be used so you can use them leaving Serial available for debugging.
Note:
We don't have insight in the body Mega's code so don't know if the additional serial pins (14..19) are in use for something else.
The head Mega already uses Serial for communication; it would be a small change to replace Serial by e.g. Serial1 leaving Serial available for debugging. Connect one of the additional Serial ports of the body Mega to e.g. Serial1 on the head Mega; if need to be, you can even use the head Mega unchanged if you connect one of the additional Serial ports of the body Mega to Serial on the head Mega; you will loose the ability to debug via USB and probably the ability to upload code to the head Mega (you will have to disconnect).
The Choice was mostly a practical one. In order to get the head to turn 360, all electrical connections have to go thru a slip ring and even buying the biggest one I could get, only has so many connections. So i needed to conserve as many as I could.
As of right now, NO, pins 14 and 19 are free. but would that interfere with the I2C on the SDL and SDA lines? as there are several servos in the Body that would still need to be controlled via their own PWM servo driver.
Thank you, I had a feeling it was which is what sparked my looking into this, As I realized a possible flaw in my plan.
that could take some time. I took good notes, but I'm working thru organizing what I had previously done. BUT the I2c (scl and sda) pins are connected to a Bus, where I have several PWMservo motor drivers, and the Other Arduino Mega Connected.
I guess where Im getting hung up is the Master/Slave where If i can only have ONE master, then If I were to set up the body with the same Adafruit_PWMServoDriver.h library I'd get into problems using it to also control the Servos and motors connected to the Head/Dome Mega.
If you look at the sketches I uploaded, the Body uses input from an analog pin on the remote, to play an audio clip on an SD card. and I'd like it to run portions of the sketch in the Head at the same time. Ie running the "lift system".
The I2C bus uses two wires plus GND, serial also uses two wires plus GND; so no loss if you use serial.
It's not 14 and 19 I was referring to the three pairs 14/15, 16/17 and 18/19. Although you don't need to use a pair, it is easier to use e.g. Serial1 for all communication than e.g. Serial1 for transmit and Serial3 for receive.
Can't look at your code now, might look later if needed.
Oh, Im Sorry What I meant was I'd prob have to do it in addition to the I2c as most of the other servos motors and a few others I have yet to add l all use the I2c. Tho If I could use it as well as, I do have 2 extra wires in the slip ring and that Could be an option if need be. (tho way more work if there is a simpler code based solution.)
OH, that makes since sorry. Prob should have been thinking of the pin layout there.
Any help you can give would be appreciated THANK YOU!
Projects with I2C between Arduino boards often fail. The I2C bus is weak because the 'high' level is made with pullup resistors. It is not fault tolerant. The SDA wire should not be near the SCL wire. It is not a network. It is not even a real communication bus.
I noticed that you use 'long' instead of 'unsigned long' for things that use millis() values, such as 'previousMillis'. You should use 'unsigned long'. You can even turn the intervals from 'long' into 'unsigned long'.
It is preferred to use 'int' or 'byte' for a pin.
For example:
// #define PTop 22
const int PTopPin = 22;
Why the baud rate of 57600 ?
The very slow 9600 is the default, but 115200 is the most common newer baudrate.
The SoftwareSerial takes over the Arduino board. There is not much else that can be done. Are you using SoftwareSerial in the Arduino Mega in the body ? Please use one of the spare hardware serial ports.
Does it not have debug output messages with Serial.println() ?
Did you know that you can start multiple instances of the Arduino IDE (it is different for the new 2.0 beta). If you have a large screen than you can have all three Serial Monitors next to each other and see what each Arduino board is doing.
I think that we need a block diagram with all components, their locations and their connections. For me, it does not make sense to have the head control something in the body (and vice versa).
Here is a basic block diagram of how I have things connected.
The goal with the 2 Arduinos (besides the slip ring limitations and #of pins) was to have several Arduinos running different functions, so I could expand on them as I go, and just add on to the droids features as I go and not have to start from scratch every time. as of right now it works perfectly separately, but in this ONE case, is I want to have a signal coming in from the remote in the body Arduino, activate the "lift system" buttons in the Dome Arduino.
I'm no expert on this stuff but I can give you a little insight into i2c. Whenever someone posts a question on this forum about using i2c to communicate between 2 arduinos, the first replies are always warnings about it not being suitable to use between 2 separate pcbs or over distances longer than a few feet. While that is correct in theory (according to the spec), in practice the protocol is much more forgiving. Case in point, this is a pic of a recent project I finished. There are 18 arduino slaves and 1 ESP32 master that use i2c to communicate over a bus consisting of 11' of ribbon cable. I have not experienced a single comm error at 400kbps.
That been said, I think i2c (or any serial protocol) might experience some issues going through the slip ring due to the noise. Whatever you decide on, the first thing I'd do is a lot of testing with R2's head spinning.
To answer your questions about i2c, I don't think 2 masters will work, so you'd have to modify the code to make one the master and also have it assume the duties of the other (controlling the pwm driver) . Or, it is possible to run i2c with no master. Not sure if the Adafruit_PWMServoDriver library allows you to do that but it would be a lot easier from a coding standpoint. Note: I've read that i2c with no master can be problematic but have never actually tried it myself. You'll find info on that in Nick's forum.
A possible alternative to the slip ring, if you find it problematic for data communication, might be an optical coupler. I recently dissected a Lidar ranger that had a head (turret) that spun on a stationary body (similar to your R2). The head transmitted the range data to the main circuit in the body via an opto coupler. The problem with that is that the 2 sides of the coupler would need to be located exactly on the axis of rotation, so unless your slip ring has a hole in the center that would allow light to pass through it, that probably wouldn't work.
Thank you for your insight. I also have had (up to this point) 0 problems with the I2c in any of the systems up to this point. I cannot with my limited knowledge say this will always be true, but for now, I have yet to see any issues.
Really this topic kind-of took a turn, as they usually do, but I was really just wanting to know if there was a way, or if it was smart to, try communication using two masters to thank you for that.
honestly this is what I was hoping for but did not have enough understanding of the library or how to code such a thing other then give it the same commands and see if it works, for SOME actions. tho since the dome code is complete I really just wanted an easy way to have one Arduino "simulate" the buttons connected on the other Arduino. essentially working as a "remote" button.
I think I may have to try and use digital Serial bet the two Arduinos. As it seems like a more likely solution.
I looked into an optical coupler as an alternative a long while back however do to design constraints 360 movement in the dome and number of connections it would not work without a redesign.
So far, I have seen 0 problems with any communications between any systems while the head is spinning or stationary. And it's fairly standard with R2 builders to use them.
That's good to know. I didn't think there was any communication between the head and the body yet. I assumed the slip ring was just for power transfer and the head and body were acting autonomously.
Even though it sounds like you are not going to pursue i2c as a means of communication between your two arduino's, I feel like I should clarify my statement about having 2 masters on the same bus. I said it was not possible. I was re-reading some of Nick Gannon's forum and he explains how it is possible. However, in my mind, he is describing using it as peer to peer but he refers to each "peer" as a master. Just wanted to point that out so there was no confusion. In my mind the "master" is the arduino that is not assigned an address at start. Nick describes a way to assign an address to two arduino's and have them each act as a "master".