Attempting asynchronous communication between Arduino and Processing...it's not going well. :(
in
Integration and Hardware
•
1 year ago
I've been basing both programs off of the SerialCallResponse example in Arduino 1.0 (
http://arduino.cc/en/Tutorial/SerialCallResponse ) to get them to talk. Perhaps it was never meant for something like this though.
In a nutshell, I want to first send 15 Arduino sensor values to Processing; then write 6 Processing values to the Arduino; lastly, I was looking to return those last 6 values to Processing to confirm that they were recorded.
Any advice or corrections would be greatly appreciated. I've been struggling with this for a bit now. At any rate here are the println responses generated through my debugging efforts:
-
Stable Library=========================================Native lib Version = RXTX-2.1-7Java lib Version = RXTX-2.1-7firstContact == falseinByte == 'A'secondContact == falsethirdContact == falsedidn't receive sensor datagoes on like this for a whiledidn't receive sensor datasecondContact == falsethirdContact == false138 //confirmation of the first value in the array receivedserialCount == 15; 'Z' has been given to arduino; it should begin getData() nowsecondContact == falsethirdContact == falsefirstContact == falseinByte == 'A'secondContact == falsethirdContact == falsedidn't receive sensor datagoes on like this for a whilethirdContact == falsedidn't receive sensor datasecondContact == falseinByte == 'B' + 'B'thirdContact == falsedidn't receive sensor datasecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsedidn't receive sensor datasecondContact == falsegoes on like this for a whilethirdContact == falsedidn't receive sensor datasecondContact == falseinByte == 'B' + 'B'thirdContact == falsedidn't receive sensor datasecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsedidn't receive sensor datasecondContact == falsegoes on like this for a whiledidn't receive sensor datasecondContact == falsethirdContact == falsedidn't receive sensor datasecondContact == falsethirdContact == false131serialCount == 15; 'Z' has been given to arduino; it should begin getData() nowsecondContact == falsethirdContact == falsefirstContact == falsegoes on like this for a whilethirdContact == falsefirstContact == falsesecondContact == falseinByte == 'B' + 'B'thirdContact == falsefirstContact == falsesecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsefirstContact == falsesecondContact == falsegoes on like this for a whilesecondContact == falsethirdContact == falsefirstContact == falsesecondContact == falseinByte == 'B' + 'B'thirdContact == falsefirstContact == falsesecondContact == trueweather and preferences and 'Y' have been sent to arduino //it's odd that it's skippingthirdContact == falsefirstContact == falsesecondContact == falsethirdContact == falsefirstContact == falsesecondContact == falseinByte == 'B' + 'B'thirdContact == falsefirstContact == falsesecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsefirstContact == falsesecondContact == falseinByte == 'B' + 'B'thirdContact == falsefirstContact == falsesecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsefirstContact == falsesecondContact == falsegoes on like this for a whilethirdContact == falsefirstContact == falsesecondContact == falseinByte == 'B' + 'B'thirdContact == falsefirstContact == falsesecondContact == trueweather and preferences and 'Y' have been sent to arduinothirdContact == falsefirstContact == falsekeeps going until I kill the program
Code for both Arduino and Processing classes are here (password = Adaptive):
http://robofacades.wordpress.com/code-hosting/acm11-facadeagent14/
Here is my processing code:
- int[] serialInArray = new int[15]; // Where we'll put what we receive
- int[] serialInArray3 = new int[6];
- int serialCount = 0; // A count of how many bytes we receive
- boolean firstContact = false; // Whether we've heard from the microcontroller
- boolean secondContact = false;
- boolean thirdContact = false;
- void serialEvent(Serial comPort4){
- int inByte = comPort4.read();
- if (firstContact == false) {
- delay(25);
- println("firstContact == false");
- if (inByte == 'A') {
- comPort4.clear(); // clear the serial port buffer
- firstContact = true; // you've had first contact from the microcontroller
- comPort4.write('A'); // ask for more
- delay(25);
- println("inByte == 'A'");
- }
- }
- else if (firstContact = true){
- delay(25);
- // Add the latest byte from the serial port to array:
- serialInArray[serialCount] = inByte;
- serialCount++;
- if (serialCount > 14 ) { //reference to arduino values
- //println("serialCount > 14");
- intLightSens1 = serialInArray[0];
- intLightSens2 = serialInArray[1]; //intLightSens1, intLightSens2, intLightSens3, avgInteriorLight, extLightSens1, extLightSens2, extLightSens3, extLightSens4, extLightSens5, solarPosition, actuatorPercent
- intLightSens3 = serialInArray[2];
- avgInteriorLight = serialInArray[3]; //temp_c_indoor 11, temp_f_indoor 12, dewpoint_f_indoor 13, relative_humidity_indoor 14
- extLightSens1 = serialInArray[4];
- extLightSens2 = serialInArray[5];
- extLightSens3 = serialInArray[6];
- extLightSens4 = serialInArray[7];
- extLightSens5 = serialInArray[8];
- solarPosition = serialInArray[9];
- actuatorPercent = serialInArray[10];
- temp_c_indoor = serialInArray[11];
- temp_f_indoor = serialInArray[12];
- dewpoint_f_indoor = serialInArray[13];
- relative_humidity_indoor = serialInArray[14];
- //comPort4.clear();
- delay (50);
- comPort4.write('Z');
- firstContact = false; //reset boolean
- serialCount = 0;
- println(intLightSens1);
- println("serialCount == 15; 'Z' has been given to arduino; it should begin getData() now");
- }
- else{
- println("didn't receive sensor data");
- }
- }
- if (secondContact == false) {
- delay(25);
- println("secondContact == false");
- //comPort4.clear();
- if (inByte == 'B' + 'B') {
- comPort4.clear(); // clear the serial port buffer
- secondContact = true; // you've had second contact from the microcontroller
- comPort4.write('B' + 'B'); // ask for more
- delay(25);
- println("inByte == 'B' + 'B'");
- }
- }
- else if (secondContact = true) {
- delay(25);
- comPort4.write(temp_f);
- comPort4.write(relative_humidity);
- comPort4.write(dewpoint_f);
- comPort4.write(desiredInteriorLight);
- comPort4.write(desiredIndoorHumidity);
- comPort4.write(desiredIndoorTemp_f);
- delay(25);
- //comPort4.clear();
- secondContact = false; //reset boolean
- comPort4.write('Y');
- serialCount = 0;
- println("secondContact == true");
- println("weather and preferences and 'Y' have been sent to arduino");
- }
- if (thirdContact == false) {
- println("thirdContact == false");
- if (inByte == 'C' + 'C' + 'C') {
- comPort4.clear(); // clear the serial port buffer
- thirdContact = true; // third contact from the microcontroller
- comPort4.write('C' + 'C' + 'C'); // ask for more
- delay(25);
- println("inByte == 'C' + 'C' + 'C'");
- }
- }
- else if (thirdContact == true) {
- delay(25);
- println("thirdContact == true");
- serialInArray3[serialCount] = inByte;
- serialCount++;
- if (serialCount > 5 ) {
- println (serialInArray3[0]);
- println (serialInArray3[1]);
- println (serialInArray3[2]);
- println (serialInArray3[3]);
- println (serialInArray3[4]);
- println (serialInArray3[5]);
- println("serialCount == 6; 'X' was sent and loop should restart");
- //comPort4.clear();
- delay (50);
- comPort4.write('X');
- serialCount = 0; // Reset serialCount
- thirdContact = false; //reset boolean
- }
- else{
- println("didn't receive serialInArray3 from arduino");
- }
- }
- }
And the Arduino code:
- //int inByte = 0;
- //boolean secondContact = false;
- //int serialInArray[6]; // Where we'll put what we receive
- //int serialCount = 0; // A count of how many bytes we receive
- void logData() {
- Serial.read();
- establishContact(); // send a byte to establish contact until receiver responds
- if (Serial.available() > 0) {
- //int writeSensorArray[] = {photoCell1, photoCell2, photoCell3, avgInteriorLight, photoCell4, photoCell5, photoCell6, photoCell7, photoCell8, solarPosition, actuatorPercent, temp_c_indoor, temp_f_indoor, relative_humidity_indoor, dewpoint_f_indoor};
- Serial.write(photoCell1);
- Serial.write(photoCell2);
- Serial.write(photoCell3);
- Serial.write(avgInteriorLight);
- Serial.write(photoCell4);
- Serial.write(photoCell5);
- Serial.write(photoCell6);
- Serial.write(photoCell7);
- Serial.write(photoCell8);
- Serial.write(solarPosition);
- Serial.write(actuatorPercent);
- Serial.write(temp_c_indoor);
- Serial.write(temp_f_indoor);
- Serial.write(relative_humidity_indoor);
- Serial.write(dewpoint_f_indoor);
- }
- if (Serial.read() == 'Z'){
- confirmReceipt();
- getData();
- }
- if (Serial.read() == 'Y'){
- confirmReceipt2();
- delay(50);
- confirmGetData();
- }
- if (Serial.read() == 'X'){
- Serial.end();
- }
- }
- void getData(){
- int serialInArray[6] = {Serial.read()};
- temp_f = serialInArray[0];
- relative_humidity = serialInArray[1];
- dewpoint_f = serialInArray[2];
- desiredInteriorLight = serialInArray[3];
- desiredIndoorHumidity = serialInArray[4];
- desiredIndoorTemp_f = serialInArray[5];
- }
- void confirmGetData() {
- Serial.write(temp_f);
- Serial.write(relative_humidity);
- Serial.write(dewpoint_f);
- Serial.write(desiredInteriorLight);
- Serial.write(desiredIndoorHumidity);
- Serial.write(desiredIndoorTemp_f);
- }
- void establishContact() {
- while (Serial.available() <= 0) {
- Serial.print('A'); // send a capital A
- delay(300);
- }
- }
- void confirmReceipt() {
- while (Serial.available() <= 1) {
- Serial.write('B' + 'B'); // send a capital B
- delay(300);
- }
- }
- void confirmReceipt2() {
- while (Serial.available() <= 2) {
- Serial.print('C' + 'C' + 'C'); // send a capital C
- delay(300);
- }
- }
1