0

I am using Arduino Uno that has 3 ultrasonic sensors and I have successfully gotten my raspberry pi to print out those values, but I don't know how to make those into into variables.

Here is the Arduino Code

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


     void loop() {


 digitalWrite(trigPin1, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin1, HIGH);
 delayMicroseconds(2);
 digitalWrite(trigPin1, LOW);
 duration1 = pulseIn(echoPin1, HIGH);
 distance1 = (duration1/2) / 29.1;

 digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(2);
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);
 distance2 = (duration2/2) / 29.1;

 digitalWrite(trigPin3, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin3, HIGH);
 delayMicroseconds(2);
 digitalWrite(trigPin3, LOW);
 duration3 = pulseIn(echoPin3, HIGH);
 distance3 = (duration3/2) / 29.1;

 Serial.print(distance1);
 Serial.print(" distance1 - ");
 Serial.print(distance2);
 Serial.print("distance2 - ");
 Serial.print(distance3);
 Serial.println("distance3 - ");

Here is the Python Code on the Raspberry Pi

import serial

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.reset_input_buffer()

    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            print(line)

Also the raspberry pi and the Arduino are connected through a USB.

Thank you for your help and ask any questions if something mentioned doesn't make sense

  • Are you able to change the arduino code to simplify the output - or is the arduino code fixed? – cguk70 Jun 10 '22 at 12:45
  • I could change the code if I need to, but I am working as a group and my friend did the arduino coding. – GoldenBee Jun 10 '22 at 23:05

2 Answers2

0

Are you sure the USB port you are connecting is /dev/ttyACM0 right port? If you type ls /dev/tty* in Raspberry terminal it will show you connected ports.

Halil Sahin
  • 568
  • 1
  • 6
  • 25
  • Yes I have checked that and I have successfully gotten it to show the data, but Im just not sure how to turn that data into a variable because I need that to then open an mp4 file if a certain distance is reached. – GoldenBee Jun 10 '22 at 23:07
  • @GoldenBee Then you have to code a buffer method that collects all the bits. – Halil Sahin Jun 10 '22 at 23:24
  • Sorry I'm new to python. How would I go about doing that? – GoldenBee Jun 10 '22 at 23:58
  • I'm sorry but if I answer this question it goes beyond the end of the topic. For this, opening a new topic and explaining the problem in detail will enable even people who are more knowledgeable than me to help you. If my answer solved your problem, you can confirm the answer. See you in the next thread :) – Halil Sahin Jun 11 '22 at 11:32
0

Following my comment, I would suggest you simplify the serial protocol to just the values and a separator.

So on the arduino you could have something like this:

String distance_output ;
distance_output = distance1 ;
distance_output += ":" ;
distance_output += distance2 ;
distance_output += ":" ;
distance_output += distance3 ;
Serial.println(distance_output.c_str());

This would produce an output string of something like "21:18:10"

Then on the pi you could just have the code:

while True:
   if ser.in_waiting > 0:

      line = ser.readline().decode('utf-8').rstrip()

      values = line.split(":")

      if len(values) == 3:   
         dist1 = int(values[0])
         dist2 = int(values[1])
         dist3 = int(values[2])

Going forward, you may want to extend this protocol to identify different sensors etc.

but hopefully this will help you progress.

cguk70
  • 611
  • 3
  • 7