Controlling arduino with another arduino

Hello. I'm just starting to build a simple arduino program to make motors and lights move in sequence. I don't have enough pins on my arduino Duemilanove to control all the motors plus to turn the LED's on/off individually in the way that I want.

Therefore I'm thinking to organise the LEDs on a separate arduino board, controlling this 2nd board from the main board to start the light loops.

I would like to have a number of different light routines programmed on the 2nd board, and to trigger them at different times using different commands. The problem is, I have no idea how to start them talking.

This is how I would set up the code on the main board, Arduino 1. I'm really not sure what command to use to send a signal from 1 to 2 though, so it is rather bare :

#define TOARDUINO2 3 //arduino2 connected to digital pin 3

void setup()
{
  pinMode(TOARDUINO2, OUTPUT) //sets the digital pin as output
}

void loop()
{
digitalWrite???(TOARDUINO2,???) //I want to send different signals, ie '1', '2' and '3' to Arduino board 2
}

And below would be the code for the 2nd board, for the lights:

#define FROMARDUINO1 13 //arduino1 connected to digital pin 13

int state = 0; // to read the value coming from Arduino 1

void setup()
{
  pinMode(FROMARDUINO1, INPUT); //sets the digital pin as input
}

void loop()
{

  state = digitalRead(FROMARDUINO1);
  
 switch(state)//

   {case '1'://
   //code to run light rountine 1
   }

   {case '2':
   //code to run light routine 2
   }

   {case '3':
   //code to run light routine 3
   }
}

I'm using the 'state' command because I used it in a previous programme, to talk to the serial monitor, using:

  if (Serial.available() > 0) { //if there is input from the serial monitor
    {
    state = Serial.read(); // used to read incoming data

and it feels like it is close to what I need.

Any advice gratefully received. Thanks.

You can't do it with one pin using digitalWrite like that. A pin has two states, Off, and On.

There are many options that are easy to implement, including SPI (one as slave, one as master), I2C, Serial.

If you want to stick with just one pin, you could try and implement the 1-Wire Communication Protocol (Google it). You could also try something possibly less reliable and use analog. Have a PWM output of the first arduino connected through an RC low pass filter (a single resistor and a single capacitor) to one of the second arduino's Analog channels. Use the duty cycle to change mode, E.g. 0% would be mode 0, 50% mode 1, 100% mode 2. Then use analogRead() on the second arduino to determine the mode (0-340 would be mode0, 341-680 mode 1, 681-1023 mode2)

I'm gearing up to use I2C between two ATmega328 MCUs. Here are some good pages I found on I2C.

http://www.uchobby.com/index.php/2008/09/16/introduction-to-i2c/

http://absences.sofianaudry.com/node/10

All you need is 1 pin, and Gnd. Use Software serial to send over the sequence you want to command. If you need a response - send a command, change the pin over to receive, wait the reply, change back to transmit. Sounded like you only need 1 way tho.

Then do similar to what you had started:

void loop(){
if (Serial.available()>0){
incomingByte = Serial.read;
}
// else {incomingByte = 0;}  // add if sequence is to only run 1 time
switch (incomingByte){
case 0:
// do nothing
break;
case 1:
// do whatever
break;
case 2:
// or whatever
break;
case 3:
// or other whatever
break;
} //end switch
} //end loop

Hi,

Look up "EasyTransfer". It's a very smooth implementation of ino-ino communication.

You just create a struct with any data you want shipped across, set the variables and call "ship it across, please". The data of the struct "magically" appears on the other side, where you can pick the bits you need.

I had a similar problem, and it took less than 30 minutes to get this up & running.

It supports Serial, I2C and softserial. The Serial is bidirectional, the I2C isn't, but you can do that manually with "Wire", if needed. All I needed to ship back was an Ack.

Both my ino's have other devices attached to the I2C bus already, so the bus-sharing works, too.

In the first instance serial is the simplest approach - you can test the slave device directly from the Arduino software first, then work on the master.

For higher bandwidth something like SPI is reasonable - the ATmega chips support SPI slave mode (upto 4MHz on a 16MHz system) as well as master so you can have interrupt-driven buffered comms that way.

Wonder if I should look at EasyTransfer and figure out if I can add SPI support for it???

Well, personally - before SPI I'd like to see the I2C version turned bidirectional.

In my mind a sort of "shared memory" between the two ino's. The master transfers, the slave does it's stuff, and returns the update. Or a different struct.

As I said, I've sorted my problem with using plain Wire. for the return, but expanding with an easy way to return a struct, in the same elegant way as ET already does it, would be nice.