Langues

Répondre au commentaire

i2c Loop

I whipped this up very quickly, if there's only ever 2 devices on the BUS then is should prevent collisions. I don't have 2 or 3 arduino's wired up to try it at the moment.

// Wire Receiver / Sender

#include 

int myNodeRef = 1;
int nextNode = myNodeRef+1;

void setup()
{
  Wire.begin(myNodeRef);			// join i2c bus with address #1
  Wire.onReceive(receiveEvent);		// register event
  Serial.begin(9600);				// start serial for output
}

void loop()
{
	delay(100);
	targetNode=5;			// want to send a message to device #5
	sendaMsg();			// this is used if I'm sending a message
	
}


// RECEIVE
void receiveEvent(int howMany) {
	while(1 < Wire.available()) {			// loop through all but the last
		int targetNode = Wire.receive();	// get node number message is intended for
		char c = Wire.receive();		// receive byte as a character
		if (targetNode == myNodeRef) {
			Serial.print(c);		// message is for me, print the character
		} else {
			passOnMessage;		// else, pass the message on to the next node
		}
	 }				
}

// SEND
void sendaMsg()
{
  Wire.beginTransmission(nextNode);		// transmit to device next door (#2)
  Wire.send(targetNode)			// tell #2 message is for targetNode (#5)	
  Wire.send("A");				// send one byte
  Wire.endTransmission();			// stop transmitting
  delay(50);
}

// PASS THE MESSAGE ON
void passOnMessage()
{
  Wire.beginTransmission(nextNode);		// transmit to the node next door (device #2)
  Wire.send(targetNode)			// tell #2 message is for someone elses node (#5)	
  Wire.send(c);				// pass on the byte received
  Wire.endTransmission();			// stop transmitting
  delay(50);
}

Répondre

Le contenu de ce champ ne sera pas montré publiquement.
  • Les adresses de pages web et de messagerie électronique sont transformées en liens automatiquement.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h3> <h4> <h5> <h6>
  • Les lignes et les paragraphes vont à la ligne automatiquement.

Plus d'informations sur les options de formatage

CAPTCHA
Cette question permet de vérifier si vous êtes un visiteur humain afin d'empêcher les envois automatisés de pourriel.
Fill in the blank
By submitting this form, you accept the Mollom privacy policy.