In an attempt to keep the processing of the GPS and radio and the ballast drops seperate I've decided to create a second board running an AVR Atmega168 to control the new sensors and also control the actually ballast dropping mechanism. To communicate between the 2 AVRs I'm using i2c.
i2c relies on 2 wires; on the Arduino these are:
An i2c library is included with the arduino environment as is known as the Wire library - it makes communicating between the 2 boards very easy. Information on the various functions can be found on the arduino website:
Wire Lib Reference
i2c relies on a master/slave setup and to define master it is usually necessary to use some pullup resistors, as i2c is a function built into the AVR the library sets the internal pullups so all you need to do is connect the 2 AVRs directly.
Once the master and slave are set up using Wire.begin() and Wire.begin(2) you can begin to pass data between the 2 micros. The present setup the master (Atlas) sends a request to the slave (Ballastboard) for data, the slave then replies with the latest reading from the temperature sensor connected via one-wire. I originally tried to have the slave collect the temperature data when the master requested it however found that i2c timed out as it took too long so instead the slave collects the data every 5 seconds and updates a buffer which is then requested by the master.
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// master side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Serial.print("master sleeping...");
delay(2000);
Serial.println("go");
}
void loop()
{
Wire.requestFrom(2, 1); // request data from slave device #2
while(Wire.available())
{
char c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
}
delay(100);
}
// i2c/wiring proof of concept code - tie two Arduinos together via
// i2c and do some simple data xfer.
//
// slave side code
//
// Jason Winningham (kg4wsv)
// 14 jan 2008
#include <Wire.h>
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest(requestEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void requestEvent()
{
static char c = '0';
Wire.send(c++);
if (c > 'z')
c = '0';
}