Hello, I am using an arduino to connect 6 hc-sr04 ultrasonic sensors and then send the output over serial to a rapsberry pi. The pi reads the arduino using serial with python. My issue is that the first line will always be less than 6 sensors but after that all 6 sensors output their distances. How would I fix this on the arduino side so that the first line will output all 6 sensors data?
The output looks like this:
0 2 4 (only 3 of the sensors)
0 2 4 5 6 8 (all 6)
1 5 22 10 12 14 (all 6)
Here is my arduino code using the NewPing library:
#include <NewPing.h>
#define SONAR_NUM 6 // Number of sensors.
#define MAX_DISTANCE 500 // Maximum distance (in cm) to ping.
NewPing sonar[SONAR_NUM] = {
NewPing(12, 13, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(10, 11, MAX_DISTANCE),
NewPing(8, 9, MAX_DISTANCE),
NewPing(6, 7, MAX_DISTANCE),
NewPing(4, 5, MAX_DISTANCE),
NewPing(2, 3, MAX_DISTANCE)
};
void setup() {
Serial.begin(115200);
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) {
delay(10);
Serial.print(" ");
Serial.print(sonar[i].ping_in());
}
Serial.println();
}
Here is the python code:
import serial
ser=serial.Serial('/dev/ttyACM0',115200)
while True:
read_serial=ser.readline()
val= read_serial.decode()
val =val.strip()
print (val)