This post will detail the current progress of the wireless communication system of our project.
The RN-XV is set to ad-hoc mode by default. This is perfect for us, as we don’t need to connect to the internet for our purposes. We will leave this setting as it is. Next we connect to the RN-XV local network and open a telnet connection. We will use putty for this connection, and the settings can be seen below.
Notice the default IP address of the ad-hoc network is 1.2.3.4 with the port set to 2000.
The current code sketch for the project is posted below
int inByte = 0; // for incoming serial data String inputString = ""; //string flag and contact established flag boolean stringComplete = false, contact = false; //sensor flags boolean startSensor1 = false; const int xin = A2, yin = A1, zin = A0; int sampleDelay = 500; void setup() { Serial.begin(9600); // opens serial port //use AREF pin for 3.3V analogReference(EXTERNAL); //initialize sensor pins pinMode(xin, INPUT); pinMode(yin, INPUT); pinMode(zin, INPUT); //establish connection with client establishContact(); //reserve 200 bytes for the inputString: inputString.reserve(200); } void loop() { // print the string when a newline arrives: if (stringComplete) { //set sensor1 flag if start1 entered if (inputString == "start1") {startSensor1 = true;} //stop sensor if stop1 entered if (inputString == "stop1") {startSensor1 = false;} /* for debugging Serial.println(inputString); */ // clear the string: inputString = ""; stringComplete = false; } if (contact) { if (startSensor1){readSensor1();} } } /* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; break; } // add it to the inputString if it is alphanumeric if ((inChar > 65 && inChar 48 && inChar < 57)) inputString += inChar; } } void establishContact() { while (!Serial.available()) { Serial.println("Enter Input"); // send an initial string delay(1000); } Serial.println("Connection Established"); contact = true; } void readSensor1() { //identifier string Serial.print("Accel: "); Serial.print("\t"); //read xin Serial.print( analogRead(xin) ); Serial.print("\t"); delay(1); //read yin Serial.print( analogRead(yin) ); Serial.print("\t"); delay(1); //read zin Serial.println( analogRead(zin) ); delay(sampleDelay); }
The code will keep prompting the client until it gets a response:
And then will open communication once it receives input:
At this time you can start reading from the accelerometer sensor by issuing the command “start1” and stop it again by typing “stop1.” Right now we are sampling at 2Hz, but we can go higher.
The values are the raw outputs from the analog pins of an ADXL335 accelerometer. These readouts can be converted into something more readable like degrees, or they can control a visual representation of orientation.