/* * This is sample code for the "Espionage" project in Networked Objects * http://www.faludi.com/teaching/networked-objects/assignments/ * by Rob Faludi http://faludi.com * */ #define NAME "Espionage Sample Code" #define VERSION "1.00" // software serial #include #define RX_PIN 2 #define TX_PIN 3 SoftwareSerial softSerial = SoftwareSerial(RX_PIN, TX_PIN); #define LED_PIN 13 // status definitions #define GET 0 #define READ 1 #define COMBINE 2 #define ANNOUNCE 3 #define SEND 4 #define DONE 5 #define MESSAGE "What hath God wrought?" // YOUR PART OF THE MESSAGE GOES HERE #define BUFFER_SIZE 255 // maximum length of the entire message being transmitted char buffer[BUFFER_SIZE]; // create a character array for storing incoming strings int idx = 0; // an index variable to put characters into the proper place in the buffer int status = 0; // keeps track of what mode we're currently in void setup() { pinMode(RX_PIN,INPUT); pinMode(TX_PIN,OUTPUT); softSerial.begin(9600); pinMode(LED_PIN,OUTPUT); blinkLED(LED_PIN,2,100); Serial.begin(9600); Serial.print(NAME); Serial.print(" v"); Serial.print(VERSION); } void loop() { switch (status) { case GET: getStart(); // check for incoming information on h/w serial break; case READ: readData(); break; case COMBINE: combineMessages(); // Add my part of the sentence to the buffer break; case ANNOUNCE: announce(); break; case SEND: sendData(); // continuously send information to the next hop break; case DONE: done();// indicate task completed break; } } void getStart() { if (Serial.available() > 0) { char inChar = Serial.read(); // read a character if (inChar == '~') { // look for start byte, in this case 0x7E aka the tilde aka ~ status= READ; // change to the next mode } } } void readData() { if (Serial.available() > 0) { char inChar = Serial.read(); // read a character if (inChar == '\n') { // if we've reached the end of the message status = COMBINE; // change to the next mode } else { buffer[idx] = inChar; // copy the incoming character into the buffer idx++; // increment to the next position in the receive string } } } void combineMessages() { strcat(buffer, " "); // add a space between each word in the message strcat(buffer, MESSAGE); // add your part of the message status = ANNOUNCE; } void announce() { ////////// THIS IS WHERE YOU PUT YOUR FANTASTIC ACTION /////////////////// blinkLED(LED_PIN,2,1000);// perform a fantastic action to indicate receipt ///////////////////////////////////////////////////////////////////////// status = SEND; } void sendData() { for (int i=0; i<1000; i++) { // repeatedly sends the message onward softSerial.print("~"); // print the start byte for (int i=0; i