0

To all of this, hope to formulate the question right.

In my project I have 5 sensors connected to Arduino, which I want to interpret via Seria.write() --> Processing.

Arduino:

firstSens = 100 + (155 * digitalRead(8));
secondSens = analogRead(A1)/4;
thirdSens = analogRead(A2)/4;
fourthSens = humData;
fifthSens = tempData;
int sensData[6] = {firstSens, secondSens, thirdSens, fourthSens, fifthSens};

int i;
for (i = 0; i < 5; i = i + 1) {
  Serial.write(sensData[i]);
}

Processing:

serialInArray[serialCount] = inByte;
    serialCount++;

if (serialCount > 4 ) {
  firstSens = serialInArray[0];
  secondSens = serialInArray[1];
  thirdSens = serialInArray[2];
  fourthSens = serialInArray[3];
  fifthSens = serialInArray[4];

  // print the values (for debugging purposes only):
  println(firstSens + "\t" + secondSens + "\t" + thirdSens + "\t" + fourthSens + "\t" + fifthSens);

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

My problem is that sensors find their place in Processing's array randomly, so for example arduino's firstSens could be Processing's secondSens or fifthSens

Why is this happening and how can I control it?

2 Answers 2

1

What is happening is you are just spewing out a list of numbers:

4132
132
1928
3240
1239
238
3048
391
... etc ...

There is nothing anywhere to say what anything is.

You need to add that kind of information. Either as a "start of data" tag before each block, or with each line. For instance:

---START OF DATA---
4132
132
1928
3240
1239
---START OF DATA---
238
3048
391
... etc ...

Or:

0:4132
1:132
2:1928
3:3240
4:1239
0:238
1:3048
2:391

It is then up to your processing application to interpret the data and work out what is what.

Personally I like using the second method since it means you don't have to send all the data every time - you can send different values at different speeds of update and maintain a "current value" at the Processing end. For instance if you have one value that updates frequently and others that don't:

3:4132
1:132
3:1928
3:3240
3:1239
2:238
3:3048
0:391

You don't even have to use numbers, you could use words:

pressure:4132
temperature:132
pressure:1928
pressure:3240
pressure:1239
humidity:238
pressure:3048
rainfall:391
0

I have done similar code for our car project dash display

you're looking for something like this on your Arduino side:

  Serial.print("U*"); //A header
  Serial.print("A");  //a token to indicate the message payload
  Serial.print(avgRPM);
  Serial.println("");

  Serial.print("U*"); //A header
  Serial.print("B");  //a token to indicate the message payload
  Serial.print(avgVoltage);
  Serial.println("");

  Serial.print("U*"); //A header
  Serial.print("C");  //a token to indicate the message payload
  Serial.print(avgCurrent);
  Serial.println("");

  Serial.print("U*"); //A header
  Serial.print("D");  //a token to indicate the message payload
  Serial.print(avgThrottle);
  Serial.println("");

  Serial.print("U*"); //A header
  Serial.print("E");  //a token to indicate the message payload
  Serial.print(avgBrake);
  Serial.println("");

and something like this on processing side:

 if (port.available() > 0) {
    data = port.readStringUntil('\n');
  }
  String payload = "";
  if (data != ""){
    int offset = data.indexOf("U*"); //this is our Header (0x552A)
    if(offset>=0){
      payload = data.substring(offset+3, data.indexOf('\n'));
      switch(data.substring(offset+2, offset+3)){
        case "A":
          RPM = payload;
          break;
        case "B":
          voltage = payload;
          break;
        case "C":
          current = payload;
          break;
        case "D":
          throttle = payload;
          break;
        case "E":
          brake = payload;
          break;
        case "F":
          telemBits = payload;
          break;
        default:
          //packet not recognised
      }
    }
} 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.