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() {
//
}