Languages

Circular counting

Arduino serial window showing the counting of modules

This is an important proof-of-concept I wanted to implement with respect to circular serial communication. For nodes in the circular network to be able to send messages to other nodes, they need to be able to send and receive addressed packets. Those packets are relayed through the network until they reach their final destination.

The first kind of packet that I wished to implement is very simple: it allows the node to be aware of the number of nodes in the network. Here is the general algorithm that is run:

generate a unique key
send a packet containing:
  this unique key and
  a counter with value zero (0)
when receiving a packet:
  add one (1) to the counter
  compare its key with my unique key
  if it's the same key then:
    the number of nodes is equal to the counter
  else then:
    just send the packet to the next node

We can explain it easily with an analogy. Suppose I am in a circular network and I want to know how many nodes there are in that network.

choose a unique color
send an empty basket colored with my unique color
when receiving a basket:
  add a rock in it
  compare the basket's color with my unique color
  if it's the same color then:
    the number of nodes is equal to the number of rocks
  else then:
    just send the basket to the next node

The source code

This is an important proof-of-concept I wanted to implement with respect to circular serial communication. For nodes in the circular network to be able to send messages to other nodes, they need to be able to send and receive addressed packets. Those packets are relayed through the network until they reach their final destination.The first kind of packet that I wished to implement is very simple: it allows the node to be aware of the number of nodes in the network. Here is the general algorithm that is run:generate a unique key

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);
}

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Fill in the blank
By submitting this form, you accept the Mollom privacy policy.