MAS.863:How To Make ( almost ) Anything

Networking and communication

I implemented this week as a part of my final project with Andrew Bahle. The final project involved devices that communicate with user over serial (RX-TX line) port and read the sequence that they need to use to play the tones. Each device is getting a unique start and stop signal as identifiers, which is defining in the microcontroller's program, and only update the sequence when start and stop characters matches the the microcontrollers code. below is the demo of the network and sample code we worked on for final project


#include  
#define RX  1
#define TX  0 
#define echoPin 7
#define trigPin 6
SoftwareSerial Serial(RX,TX);
int pauseBetweenNotes;
int Sol_1 = 2;
int Sol_2 = 3;

long distance;
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;


void setup() 
{
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);  
  pinMode(Sol_1, OUTPUT);
  pinMode(Sol_2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  establishContact();   

}

void loop()
{
    recvWithStartEndMarkers();
    PlayNewSeq();
}



void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '(';
    char endMarker = ')';
    char rc;
 
    while (Serial.available() > 0 \ newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
    
}

void showNewData() {
    if (newData == true) {  
        Serial.print("This just in...");   
        Serial.println(receivedChars); 
         for(int i=0;i<8;i++){
            if (receivedChars[i] == '1'){
                 digitalWrite(Sol_1,HIGH);
                delay(50);
                digitalWrite(Sol_1,LOW);
                distance = calc_distance();
                delay(distance);
            }
            else{
                digitalWrite(Sol_2,HIGH);
                delay(50);
                digitalWrite(Sol_2,LOW);
                distance = calc_distance();
                delay(distance);
            }
          }
       newData = false;
    }
    else {
           for(int i=0;i<8;i++){
            if (receivedChars[i] == '1'){
                 digitalWrite(Sol_1,HIGH);
                delay(50);
                digitalWrite(Sol_1,LOW);
                distance = calc_distance();
                delay(distance);
            }
            else{
                digitalWrite(Sol_2,HIGH);
                delay(50);
                digitalWrite(Sol_2,LOW);
                distance = calc_distance();
                delay(distance);
            }
          }
    }
}

void PlayNewSeq() {
  while (Serial.available() <= 0) {
  Serial.println("A");   // send a capital A
  delay(300);
  }
}

int calc_distance()    
{   
  long duration, distance;
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  duration=pulseIn(echoPin,HIGH);
  distance = duration/10;
  return distance;
}