Arduino not continuously sending all flex sensor readings?

Hi there. I'm using an Arduino Uno to send sensor data to Max MSP. The ultimate plan will be 4 flex sensors and an accelerometer/gyro sent to Max via bluetooth serial, but I'm currently stuck with the flex sensors. At the end of this I hope to ultimately be sending 8 readings at a time - 1 for each sensor and X,Y,Z and Accelerometer data

It looks like Arduino is only sometimes sending all four flex sensor readings, sometimes it sends 3 and sometimes it sends 5 pieces of data (???). This obviously affects my software on the other end, which is conditioned to read the First, Second, Third, and Fourth pieces of data, then sorted into dials to watch their change. It causes some data to hop between dials and is consequently very jumpy. :frowning:

As you can see in the code attached, it is very straightforward. (I've mapped the readings 0-100 as it looks like Max prefers these numbers.) I've read some information about strings being beneficial as it will make sure all pieces of data are sent together, but I'm not sure how to use these at all. I'm a beginner at all of this, so any input would be highly appreciated. Thanks!!

int flexSensorPin0 = A0; //analog pin 0
int flexSensorPin1 = A1; //analog pin 1
int flexSensorPin2 = A2; //analog pin 2
int flexSensorPin3 = A3; //analog pin 3


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

void loop(){
int flexSensorReading0 = analogRead(flexSensorPin0);
int flexSensorReading1 = analogRead(flexSensorPin1);
int flexSensorReading2 = analogRead(flexSensorPin2);
int flexSensorReading3 = analogRead(flexSensorPin3);

Serial.println(flexSensorReading0);
Serial.println(flexSensorReading1);
Serial.println(flexSensorReading2);
 Serial.println(flexSensorReading3);



int Sensor0New = map(flexSensorReading0, 400, 800, 0, 100); 
int Sensor1New = map(flexSensorReading1, 400, 800, 0, 100);
int Sensor2New = map(flexSensorReading2, 400, 800, 0, 100);
int Sensor3New = map(flexSensorReading3, 400, 800, 0, 100);

Serial.write(Sensor0New);
Serial.write(Sensor1New);
Serial.write(Sensor2New);
Serial.write(Sensor3New);
Serial.flush();


delay(250); 
}

_4_Flex_Sensors.ino (982 Bytes)

Serial.println(flexSensorReading0);
Serial.println(flexSensorReading1);
Serial.println(flexSensorReading2);
 Serial.println(flexSensorReading3);

and

Serial.write(Sensor0New);
Serial.write(Sensor1New);
Serial.write(Sensor2New);
Serial.write(Sensor3New);
Serial.flush();

That means you're sending your sensor values as CR/NL separated ASCII strings as well as a series of 4 consecutive bytes over the serial interface. I have no clue what code on the other end will read these values but it's probably confused. There is no delimiter between two complete readings except the delay of a quarter of a second.

Serial.write(Sensor0New);

Sensor0New is an int, so two bytes. You're writing just one of them, I don't know if it's LSB or MSB. That's another issue.

You don't have start/stop codes, making it harder for the receiving side to read your communications.

Check out the Serial communication tutorial on how to improve on this.