Hello, I'm after some advice on arduino to arduino communication.
Firstly, apologies if my post/question/topic is in the wrong place, this is my first time posting.
I am fairly new to the world of programming/micro-controllers (as I am a mechanical engineer by trade), and have come to a problem that I can't google my way out of. I already have a rather large program to control two stepper motors and a laser. The stepper motors control the tilt and twist of a mirror which the laser beam reflects off of, so that you can draw shapes and patterns on the wall. The program is nearing capacity (Mega); custom stepper motor acceleration curves, reading and writing to eeprom, speed controls, multiple serial commands with parsing, digital end stops, analogue in, drawing of multiple shapes with the laser (and hopefully words soon).
I recently bought a OLED 128x64 display and a cheap joystick. I would like to add this interface to my laser program, but a) I haven't got the memory and b) I don't want to slow it down with code that updates the OLED display. I'd like to program a separate arduino [lets say a Nano] as a remote. It would display menus, options and draw out the patterns/shapes of the main program [Mega] in real time. As well as allowing you to draw 'free-hand' via the joystick. Ideally the Nano could take on some of the real-time processing of the shapes, while the first [Mega] concentrates on moving/controlling the stepper motors as fast as it can.
Assuming I have my Nano programmed, what is my best bet in terms of communication? I need to send commands from the Nano such as " Move 10000,-200100 " [Move motors the respective amount], " SetRPM 310 " [Adjust speed] and " CaliX " [Re-calibrate the X axis movement]. I also need to ask the Mega, " ?RPM " [what is the current speed], " ?Max " [Position of the end stops] and " ?Pos " [Current position], to update the display and menus on the Nano. As far as I can make out I2C is my best bet with the Nano being the master, but I can only find tutorials where single characters are sent, or set responses are requested? Can I2C handle multiple commands? Is SPI or something else going to be any easier/better? Any and all help would be appreciated!! I hope I have supplied enough information.
First, it's pretty difficult to run out of memory on a Mega. There may be some simple strategies you can apply. Are you using the F() macro for any long strings? Nick Gammon's PROGMEM page has a lot of useful suggestions.
Large pre-programmed images/sequences should be put on an SD card. This makes it easier to edit them on the PC instead of re-uploading code.
While it is possible to run any Arduino as an SPI slave, it's not done very often. As you said, it seems I2C is more common. I2C can send unlimited-length messages although it's probably best to keep it under 32 bytes as that's the size of one of the buffers defined in the Arduino library.
Why not Serial? It seems you already have some serial involved. The Mega has spare hardware serial ports.
For a small Arduino, my current favourite is the Micro. It gives you a "free" hardware serial port in addition to the USB communications, plus extra analog inputs which aren't available on the standard R3 layout.
MorganS,
I have employed a few code saving strategies, such as direct port manipulation, but this is the first I've heard of the F() macro; I shall be checking out the PROGMEN page once I've finished thanking you.
I think my large code comes from a lot of serial commands, long(ish) shape functions, and my extensive use of error/status reporting to the serial port at any significant point. Not coming from a programming background I like to see what my code is doing, where it is and what variables have been passed/calculated.
I am not using an SD card, but it may be something I can look into and learn.
I suggested a Nano simply because I have one. I looked at the Micro (online) a few weeks ago, and the fact it doesn't come with a USB port put me off. I'm still learning, so baby steps. I worked out it may be a better choice if I want to make this a wireless remote (with a bluetooth module?), since it can be programmed with sleep modes to draw less than 1 mA (I'm guessing due to the lack of a UART?); so it can last a few months on batteries. For now i'd like to use the Nano and keep it wired.
As for the main question; another serial port! Of course! Thank you, thank you, thank you! Yes, my other serial ports on the Mega are free, and I can tap into the RX and TX lines on the Nano. Perfect! Thank you for taking the time to help.