Serial Communication not working with value 300+

Hey,

So I’m working on a project which measures the fluctuations in the data of three sensors. This is where the sensor checks if its value has risen, fallen or stayed the same over a set time. I then send the results over to processing via serial communication.

The way this works is that each sensor measures the fluctuations, and then notes this as a number: 1 for an increase in value, 3 for a decrease and 0 for no difference. It then combines the three results (e.g 1,2,2) into one number (122) and sends this number over.

This works fine but for some reason, if the number is 300+ then processing receives two numbers from 77 - 50 instead? Can anyone explain why this happens and how to fix it.

Many thanks! (Code bellow)

Arduino Code:

void setup() {
  Serial.begin(9600);
  Serial.write(1);
  }

int nowTemp;
int oldTemp;
int tempResponse;

int nowWater;
int oldWater;
int waterResponse;

int randomResponse;

void readTemp() {
  nowTemp = analogRead(A0);
  nowTemp = analogRead(A0);
  }

void tempQuestion() {
  readTemp();
  oldTemp = nowTemp;
  tempResponse = 1;
  
  for (int tempTimes = 0; tempTimes < 10; tempTimes++) {
    readTemp();

    if (oldTemp < nowTemp) {
      tempResponse = 3;
      return;
    } else if (oldTemp > nowTemp) {
      tempResponse = 1;
      return; 
      }
      delay(250);
    } tempResponse = 2;
  }

void readWater() {
  nowWater = analogRead(A1);
  nowWater = analogRead(A1);
  }

void waterQuestion() {
  readWater();
  oldWater = nowWater;
  waterResponse = 1;
  
  for (int waterTimes = 0; waterTimes < 10; waterTimes++) {
    readWater();

    if (oldWater < nowWater) {
      waterResponse = 3;
      return;
    } else if (oldWater > nowWater) {
      waterResponse = 1;
      return; 
      }
      delay(250);
    } waterResponse = 2;
  }

int tempDone;
int waterDone;
int randomDone;
int responses;

void loop() {
  if (Serial.available() > 0) {
    char input = Serial.read();
    
    tempQuestion();
    waterQuestion();
    
    tempDone = tempResponse * 100;
    waterDone = waterResponse * 10;
    randomDone = random(1,4);
    
    responses = tempDone + waterDone + randomDone;
    Serial.write(responses);
    }
  }

Processing Code:

import processing.serial.*;

void setup() {
  size(50,50);
  Serial myPort;
  
  String portName = "/dev/ttyACM0";
  myPort =  new Serial(this, portName, 9600);
}

void serialEvent(Serial myPort) {
  //RESPONSE GAINED
  
  int sensorResponses = myPort.read();
  
  println(sensorResponses);
  myPort.write('x');
}

void draw() {
  //
}
1 Like

pls read about the arduino write
https://www.arduino.cc/en/serial/write

anyhow i not understand

responses = tempDone + waterDone + randomDone;

i have no idea what you are up to ( high speed byte data transfer at 9600 bd )
but pls consider to start over with a full text data transfer what you can also READ at terminal( ardunio ide monitor)
int varA,varB,varC 0 … 1023 ( Ain range )
and send as one string with serial.println
1023,1023,1023,
https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

and on Processing side use
https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html

2 Likes

@kll The OP is encoding his three sensor data in a single field. Each digit would represent a value related to each sensor’s state. He is working with base 10 meaning that each digit is extracted based on powers of 10.

@KeirMeDear One solution is to use 0,1,2 instead of 1,0,3. When you encode your values into a single field, all possible values will be below the 255 limit as kll discussed in his post. Another solution is to use a lower base. You are using base 10. You could do base 3 as you only have 3 values. It is more work and it would be a good exercise if you want to try something different.

Kf

3 Likes

Just the response I was looking for! Thank for all the information :slight_smile:

@KeirMeDear

A byte (0-255) is 8 bits (2^8, or 00000000).

You can encode a value from 0-3 in 2 bits (2^2, or 00).

You have three values, so you can encode them in 6 bits (00 00 00), with two bits left over.

If you want to program these as bits, use the reference Bitwise Operators for left shift and right shift:

https://processing.org/reference/leftshift.html
https://processing.org/reference/rightshift.html

So:

// store values
int sig0 = 3;
int sig1 = 1;
int sig2 = 2;
// encode
int message = sig0 + (sig1 << 2) + (sig2 << 4);
println(message);

// transmit

// decode
sig0 = message & 3;
sig1 = (message >> 2) & 3;
sig2 = (message >> 4) & 3;
// display
println(sig0, sig1, sig2);
2 Likes