analogRead and Serial Print Issue

Hi!

I’m having an issue with analogRead and printing to the serial port. I am saving the stream of data that is going into the serial monitor with a Python program. About half of the time, there will be a gap in the data or shift in the strings. For instance, a graph of my sensor’s data will look like the attached photo. Does anyone know why this might happen? For some context, I am reading data from a load cell that is in motion and being impacted.

Arduino Code:

String datastring="";

void setup() {

  Serial.begin(9600);
}


void loop() {
  // read the input on analog pin 0:
  int sensorValue0 = analogRead(A0);
    int sensorValue2 = analogRead(A2);
    datastring=String(millis())+","+sensorValue0+","+sensorValue2;
  // print out the value you read:
  Serial.println(datastring);
  delay(1);        // delay in between reads for stability

Python code running on my Mac’s command line:

import serial

serial_port = '/dev/cu.usbmodem14101';
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
write_to_file_path = "output5.txt";

output_file = open(write_to_file_path, "w+");
ser = serial.Serial(serial_port, baud_rate)
while True:
    line = ser.readline();
    line = line.decode("utf-8") #ser.readline returns a binary, convert to string
    print(line);
    output_file.write(line)'

Is that the whole Arduino code that you improperly posted? I notice that the discontinuity is around 32000. 32767 is the largest number that a signed i t data type can hold. Maybe a clue?

Read the how to use this forum stickies that appear, in bold, at the top of each topic section to see how to post code and some advice on how to get themost from the forum.

That is the whole Arduino code -- I fixed the post. And the discontinuity happens at 32442, but happens later or earlier in other runs of the code.

Serial.begin(9600);

What happens if you up the baud rate? Arduino hardware serial goes way faster (>200000 baud).

Your Arduino code is not complete. It will not compile without the missing loop() closing curly bracket.

I don't know dick about Python so can't help with that part.