MAX PATCHES FOR MIDTERM
Here is the first patch that Raphael and I worked on, which attributes one different instrument to each radio coming into the range of the base station:

And this is the patch that Eric worked on that adds a little excitement to our demonstration and works off of Rob's radio metronome:

Code for the Base station here:
//Rory Nugent, Raphael Zollinger, Eric Beug, Benedetta Piantella Simeonidis
//Collaborative Mesh Networking
#define timecodeLED 13
#define receiveLED 12
#define nodeOneLED 8
#define nodeTwoLED 7
#define nodeThreeLED 6
#define nodeFourLED 5
byte availByte = B10000;
boolean oneAvailable = false;
boolean twoAvailable = false;
boolean threeAvailable = false;
boolean fourAvailable = false;
unsigned long lastOneAvailable = 0;
unsigned long lastTwoAvailable = 0;
unsigned long lastThreeAvailable = 0;
unsigned long lastFourAvailable = 0;
const unsigned long nodeTimeout = 5000;
unsigned long lastReceive = 0;
unsigned long lastBroadcast = 0;
const unsigned long freqBroadcast = 1000;
int beat = 0; // used to store the current beat number
unsigned long timeCode = 0; // used to store the current time code value
byte inByte = 0;
void setup()
{
pinMode(timecodeLED,OUTPUT);
pinMode(receiveLED,OUTPUT);
pinMode(nodeOneLED,OUTPUT);
pinMode(nodeTwoLED,OUTPUT);
pinMode(nodeThreeLED,OUTPUT);
pinMode(nodeFourLED,OUTPUT);
digitalWrite(nodeOneLED,LOW);
digitalWrite(nodeTwoLED,LOW);
digitalWrite(nodeThreeLED,LOW);
digitalWrite(nodeFourLED,LOW);
Serial.begin(9600);
setupXBee();
Serial.flush();
blinkLED(receiveLED,2,250);
}
void loop()
{
checkforBroadcast();
checkNodeTime();
setAvailByte();
}
void setAvailByte()
{
availByte = B10000;
if(fourAvailable)
{
availByte = availByte + (B1 << 3);
}
if(threeAvailable)
{
availByte = availByte + (B1 << 2);
}
if(twoAvailable)
{
availByte = availByte + (B1 << 1);
}
if(oneAvailable)
{
availByte = availByte + B1;
}
}
void checkforBroadcast()
{
if(Serial.available() > 2)
{
inByte = Serial.read();
if(inByte == '#')
{
inByte = Serial.read();
if(inByte == '1')
{
lastOneAvailable = millis();
digitalWrite(nodeOneLED,HIGH);
oneAvailable = true;
blinkLED(receiveLED,1,20);
}
else if(inByte == '2')
{
lastTwoAvailable = millis();
digitalWrite(nodeTwoLED,HIGH);
twoAvailable = true;
blinkLED(receiveLED,1,20);
}
else if(inByte == '3')
{
lastThreeAvailable = millis();
digitalWrite(nodeThreeLED,HIGH);
threeAvailable = true;
blinkLED(receiveLED,1,20);
}
else if(inByte == '4')
{
lastFourAvailable = millis();
digitalWrite(nodeFourLED,HIGH);
fourAvailable = true;
blinkLED(receiveLED,1,20);
}
}
else if(inByte == '*')
{
blinkLED(timecodeLED,1,20);
Serial.print('$');
Serial.print(availByte,BIN); //outputs availability status to Max, format can be changed accordingly
}
}
}
void checkNodeTime()
{
if((millis() - lastOneAvailable > nodeTimeout) && oneAvailable == true)
{
digitalWrite(nodeOneLED,LOW);
oneAvailable = false;
}
else if((millis() - lastTwoAvailable > nodeTimeout) && twoAvailable == true)
{
digitalWrite(nodeTwoLED,LOW);
twoAvailable = false;
}
else if((millis() - lastThreeAvailable > nodeTimeout) && threeAvailable == true)
{
digitalWrite(nodeThreeLED,LOW);
threeAvailable = false;
}
else if((millis() - lastFourAvailable > nodeTimeout) && fourAvailable == true)
{
digitalWrite(nodeFourLED,LOW);
fourAvailable = false;
}
}
// this function blinks andddd LED light as many times as requested
void blinkLED(int targetPin, int numBlinks, int blinkRate) {
for (int i=0; i
delay(blinkRate); // waits for a blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
///////////////////////////////// UTILITY FUNCTIONS //////////////////////////////////////////
// this function checks for a specific response on the serial port
// it accepts a string to look for and a timeout in milliseconds
int checkFor(char* desiredResponse, int timeout) {
int result = 0;
int length = 40;
char incomingResponse[41];
memset(incomingResponse,0,length); // initialize all incomingResponse string positions to null
char inByte = NULL;
long startTime = millis();//makes the start time = to now
char* ptr_incomingResponse = incomingResponse;
// while we haven't timed out or gotten back the string that we are looking for
while (millis() - startTime < timeout && strstr(incomingResponse,desiredResponse) == NULL ) { //strstr compares strings
if (Serial.available() > 0) { // if there are any bytes waiting to be read
inByte = Serial.read(); // read one byte
if (incomingResponse > ptr_incomingResponse-length) { // if we haven't read in 80 characters yet
*ptr_incomingResponse = inByte; // put the byte into the current position in the string
ptr_incomingResponse++; // advance to the next position in the string
}
else {
//move the last char to be next to last, and so forth until we reach the end of the array.
for (int i = 0; i < length; i++) {
incomingResponse[i] = incomingResponse[i+1];
}
incomingResponse[length-1] = inByte; // put the byte into the current position in the string
}
}
}
if (strstr(incomingResponse,desiredResponse) != NULL ) { // if the desired string is found
result = 1;
}
else {
result = 0;
}
return result;
}
void setupXBee() {
boolean success = false;
int ctr = 0;
while (success == false && ctr < 100) {
// blink the status LED
blinkLED(receiveLED, 2, 250);
// an arbitrary byte to wake up the XBee
//Serial.print("X");
//delay(1100);
// put the XBee in command mode
Serial.print("+++");
delay(1100);
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes
// set the PAN (personal area network) ID number
// set the MY (16 bit address)
// set the Destination High to 0x0
// set the Destination Low (16 bit address)
// exit command mode (using a Serial.printLN to end the command with a linefeed)
Serial.println("ATRE,ID333A,MYBEEF,DH0,DLBEEF,CN");
if (checkFor("OK", 1000)) {
// if an OK was received then continue
success = true;
}
else {
success = false;
}
ctr++;
}
}
Code for the 4 Nodes here:
//Rory Nugent, Raphael Zollinger, Eric Beug, Benedetta Piantella Simeonidis
//Collaborative Mesh Networking
#define broadcastLED 13
unsigned long lastBroadcast = 0;
const unsigned long freqBroadcast = 1000;
const char* broadcastID = "#1"; //change depending on the number of the node
byte inByte = 0;
void setup()
{
pinMode(broadcastLED,OUTPUT);
Serial.begin(9600);
setupXBee();
Serial.flush();
blinkLED(broadcastLED,2,250);
}
void loop()
{
broadcast();
}
void broadcast()
{
if(millis() - lastBroadcast > freqBroadcast)
{
blinkLED(broadcastLED,1,20);
Serial.print(broadcastID);
lastBroadcast = millis();
}
}
// this function blinks the an LED light as many times as requested
void blinkLED(int targetPin, int numBlinks, int blinkRate) {
for (int i=0; i
delay(blinkRate); // waits for a blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
///////////////////////////////// UTILITY FUNCTIONS //////////////////////////////////////////
// this function checks for a specific response on the serial port
// it accepts a string to look for and a timeout in milliseconds
int checkFor(char* desiredResponse, int timeout) {
int result = 0;
int length = 40;
char incomingResponse[41];
memset(incomingResponse,0,length); // initialize all incomingResponse string positions to null
char inByte = NULL;
long startTime = millis();//makes the start time = to now
char* ptr_incomingResponse = incomingResponse;
// while we haven't timed out or gotten back the string that we are looking for
while (millis() - startTime < timeout && strstr(incomingResponse,desiredResponse) == NULL ) { //strstr compares strings
if (Serial.available() > 0) { // if there are any bytes waiting to be read
inByte = Serial.read(); // read one byte
if (incomingResponse > ptr_incomingResponse-length) { // if we haven't read in 80 characters yet
*ptr_incomingResponse = inByte; // put the byte into the current position in the string
ptr_incomingResponse++; // advance to the next position in the string
}
else {
//move the last char to be next to last, and so forth until we reach the end of the array.
for (int i = 0; i < length; i++) {
incomingResponse[i] = incomingResponse[i+1];
}
incomingResponse[length-1] = inByte; // put the byte into the current position in the string
}
}
}
if (strstr(incomingResponse,desiredResponse) != NULL ) { // if the desired string is found
result = 1;
}
else {
result = 0;
}
return result;
}
void setupXBee() {
boolean success = false;
int ctr = 0;
while (success == false && ctr < 100) {
// blink the status LED
blinkLED(broadcastLED, 2, 250);
// an arbitrary byte to wake up the XBee
//Serial.print("X");
//delay(1100);
// put the XBee in command mode
Serial.print("+++");
delay(1100);
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes
// set the PAN (personal area network) ID number
// set the MY (16 bit address)
// set the Destination High to 0x0
// set the Destination Low (16 bit address)
// exit command mode (using a Serial.printLN to end the command with a linefeed)
Serial.println("ATRE,ID333A,MYBEEF,DH0,DLBEEF,CN");
if (checkFor("OK", 1000)) {
// if an OK was received then continue
success = true;
}
else {
success = false;
}
ctr++;
}
}