March Madness - Three Amigos

OK, this is only a slight change from yesterday but its pretty cool anyway.

One Arduino (in the middle underneth) is talking to two other Arduinos at the same time via I2C. If you look closely, the top display has even numbers and the bottom has odd.


Click to see full size image
//************************************************************************
//*	I2C transmission test
//*	this tests sending data from one Arduino to anohter via I2C
//************************************************************************

#include	<stdlib.h>


#include	"WProgram.h"
#include	"HardwareSerial.h"

#include "Wire.h"
extern "C" { 
	#include "utility/twi.h"	// from Wire library, so we can do bus scanning
	//#include "twi.h"			// from Wire library, so we can do bus scanning
}

#define	kLCDserial_I2C	77

void ScanI2CBus(byte from_addr, byte to_addr) ;

int	i2cTestCounter;

//************************************************************************
void setup()
{
	Serial.begin(9600);           // set up Serial library at 9600 bps
	Serial.println("Test I2C (TWI) transmission");

	//*	join the I2C bus as the master
	Wire.begin();

	i2cTestCounter	=	0;
	ScanI2CBus(0, 100);

}



//************************************************************************
void loop()
{
char	tempString[32];
int		destI2Caddr;


	Serial.println("loop");

	destI2Caddr	=	kLCDserial_I2C;
	if ((i2cTestCounter % 2) == 1)
	{
		destI2Caddr	+=	1;
	}

	Wire.beginTransmission(destI2Caddr);
	Wire.send("I2C test ");

	sprintf(tempString, "%d\r", i2cTestCounter);

	Wire.send(tempString);

	Wire.endTransmission();
	
	
	i2cTestCounter++;
	
	delay(1000);

}





//****************************************************************************
//*	Scan the I2C bus between addresses from_addr and to_addr.
//*	On each address, call the callback function with the address and result.
//*	If result==0, address was found, otherwise, address wasn't found
//*	(can use result to potentially get other status on the I2C bus, see twi.c)
//*	Assumes Wire.begin() has already been called
//*	2009, Tod E. Kurt, http://todbot.com/blog/
//****************************************************************************
void ScanI2CBus(byte from_addr, byte to_addr) 
{
byte addr;
byte rc;
byte data; // not used, just an address to feed to twi_writeTo()
int	foundCount;

	Serial.print("starting scanning of I2C bus from ");
	Serial.print(from_addr, DEC);
	Serial.print(" to ");
	Serial.print(to_addr, DEC);
	Serial.println("...");

	data		=	0;
	foundCount	=	0;
	for(addr = from_addr; addr <= to_addr; addr++ )
	{
		Serial.print("addr:");
		Serial.print(addr,DEC);
		rc	=	twi_writeTo(addr, &data, 0, 1);
		if (rc == 0)
		{
			foundCount++;
		}
		Serial.print( (rc==0) ? " found!":"       ");
		Serial.print( (addr%4) ? "\t":"\r\n");
	}
	Serial.println();

	Serial.print("I2C device count = ");
	Serial.println(foundCount);
}