I am having problems "viewing" data in correct order using a mux and msp430.

Hello hope everyone is doing well. What I am doing is quite easy but I lack programming experience with serial communications. So here is the run down: At the moment I have 4 sensors going into a mux. The switching of the mux is controlled by the msp430. Once the mux is switched the msp430 reads the analog signal and stores it, then the mux switches to the next input and msp430 stores it as another value. When all 4 sensor values are stored it waits for another app to receive, in this case a "A" to transfer. On the processing side, the sensor values are read and displayed as a "bar graph". The code works but the sensor values are out of order. For example sensor data for sensor 1 is sent first but in processing it is the third to arrive so my bar graphs are out of order. I guess it would be fine and I could just rearrange the bars to be in the correct order but.. that would be bad development and I would rather understand what is going on than trying to make it work.. I will post the code which I got from the "serial event" example in Energia. Energia code:

int sensor1 = 0; 
int sensor2 = 0; 
int sensor3 = 0;
int sensor4 = 0;

int inByte = 0; // incoming serial byte



void setup() {
 pinMode(P8_2, OUTPUT);
 pinMode(P3_7, OUTPUT);
 pinMode(P4_0, OUTPUT);
 pinMode(P4_3, OUTPUT);

 // initialize serial communication at 9600 bits per second:

  Serial.begin(9600); // msp430g2231 must use 4800

   establishContact(); // send a byte to establish contact until receiver responds 

}

// the loop routine runs over and over again forever:
void loop() 
{

  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();


  //switch Mux to sensor 1
  digitalWrite(P8_2, LOW);
  digitalWrite(P3_7, LOW);
  digitalWrite(P4_0, LOW);
  digitalWrite(P4_3, LOW);
  delay(10);
  sensor1 = analogRead(A0)/10;

  //switch Mux to sensor 2
  digitalWrite(P8_2, HIGH);
  digitalWrite(P3_7, LOW);
  digitalWrite(P4_0, LOW);
  digitalWrite(P4_3, LOW);
  sensor2 = analogRead(A0)/10;

  delay(10);
  //switch Mux to sensor 3
  digitalWrite(P8_2, LOW);
  digitalWrite(P3_7, HIGH);
  digitalWrite(P4_0, LOW);
  digitalWrite(P4_3, LOW);
 delay(10);
  sensor3 = analogRead(A0)/10;

  //switch Mux to sensor 4
  digitalWrite(P8_2, HIGH);
  digitalWrite(P3_7, HIGH);
  digitalWrite(P4_0, LOW);
  digitalWrite(P4_3, LOW);
  delay(10);
  sensor4 = analogRead(A0)/10;


  //Write to serial 
    Serial.write(sensor1);
    Serial.write(sensor2);
    Serial.write(sensor3);
    Serial.write(sensor4);

    }
}

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

Code from Processing:

import processing.serial.*; //Start serial comm
Serial port;
int[] serialInArray = new int[4]; //vairable high will be brought in
int serialCount = 0; //keep track of received bytes
int ch1,ch2,ch3,ch4;
boolean firstContact = false; //Listening to MC

void setup ()
{
  size(500,500); //widow size
 ch1 = 0;
 ch2 = 0;
 ch3 = 0;
 ch4 = 0;

 String portName =  Serial.list()[0];
  port =  new Serial(this, "COM9", 9600); // comm port and baud rate

}



void draw()
{
  background(255);
 //for channel 1 
  pushMatrix();
  translate(400, 400); 
  rotate(radians(180));
  stroke(0);
  fill(0,255,0);
  rect(240,40,10,ch1);
  popMatrix();
//for channel 2
  pushMatrix();
  translate(400, 400); 
  rotate(radians(180));
  stroke(0);
  fill(0,255,0);
  rect(20,40,10,ch2);
  popMatrix();
//for channel 3
  pushMatrix();
  translate(400, 400); 
  rotate(radians(180));
  stroke(0);
  fill(0,255,0);
  rect(200,40,10,ch3);
  popMatrix();
  //for channel 4
  pushMatrix();
  translate(400, 400); 
  rotate(radians(180));
  stroke(0);
  fill(0,255,0);
  rect(180,40,10,ch4);
  popMatrix();




}

void serialEvent (Serial port)
{
int inByte = port.read();



 if (firstContact == false) {
    if (inByte == 'A') { 
      port.clear(); // clear the serial port buffer
      firstContact = true; // you've had first contact from the microcontroller
      port.write('A'); // ask for more
    } 
  } 
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have bytes:
    if (serialCount > 3 ) {
      ch1 = serialInArray[0];
      ch2 = serialInArray[1];
      ch3 = serialInArray[2];
      ch4 = serialInArray[3];

      // Send a capital A to request new sensor readings:
      port.write('A');
     // Reset serialCount:
     serialCount = 0;
    }
  }
}

Thanks for all your help and also thanks for looking!!!!

Sign In or Register to comment.