1

This is my code:

int trigPin=13; //Sensor Trig pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
float pingTime; //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.
int trigPin1=2; 
int echoPin1=3; 
float pingTime1; //time for ping to travel from sensor to target and return 1
float targetDistance1; //Distance to Target in inches 1
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin1, INPUT);
}
 
void loop() {
 // put your main code here, to run repeatedly:
 
 digitalWrite(trigPin, LOW); //Set trigger pin low
 delayMicroseconds(2000); //Let signal settle
 digitalWrite(trigPin, HIGH); //Set trigPin high
 delayMicroseconds(15); //Delay in high state
 digitalWrite(trigPin, LOW); //ping has now been sent
 delayMicroseconds(10); //Delay in low state
 digitalWrite(trigPin1, LOW); //Set trigger pin low
 delayMicroseconds(2000); //Let signal settle
 digitalWrite(trigPin1, HIGH); //Set trigPin high
 delayMicroseconds(15); //Delay in high state
 digitalWrite(trigPin1, LOW); //ping has now been sent
 delayMicroseconds(10); //Delay in low state
 
 pingTime1 = pulseIn(echoPin1, HIGH); //pingTime is presented in microceconds
 pingTime1=pingTime1/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
 pingTime1=pingTime1/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
 targetDistance1= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
 targetDistance1=targetDistance1/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
 targetDistance1= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)
 pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
 pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
 pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
 targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
 targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
 targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)
 
 Serial.println(targetDistance);
 
 Serial.println(targetDistance1);
 
 
 delay(100); //delay tenth of a second to slow things down a little.
}

Python code:

import serial #Import Serial Library
arduinoSerialData = serial.Serial('com3',9600) #Create Serial port object called arduinoSerialData
while (1==1):
 if (arduinoSerialData.inWaiting()>0):
 myData = arduinoSerialData.readline()
 myData=str(myData)
 myData= myData.replace("b'",'')
 myData=myData.replace("\\r\\n'",'')
 if myData<"3":
 print( myData)

When I compile the sketch, it shows no error.But when I run the file in python, output=0.However, when they are run individually, python gives me a valid input.so is this code correct?Also, is there a way to call for the exact c++file in python?Thanks for the help!

asked Jul 30, 2020 at 10:16
8
  • 1. What file you are you running in python? You didn't show your python sketch. 2. How do you distinguish between the data from the first sensor and from the second sensor? Or doesn't that matter for you? Commented Jul 30, 2020 at 10:44
  • Edit:outputs are distinguished by targetDistance1 and targetDistance(one variable has a'1' and one doesn't) Commented Jul 30, 2020 at 10:54
  • I meant it your python program. But as you are just printing every line, that doesn't really apply here. Have you checked, that the Serial output of your Arduino sketch is as you expect it to be? You could check that simply in the Serial monitor. And why are you doing if myDATA<"3" in your python program? Why do you only want to display lines, which are lexically at a lower order as "3"? Commented Jul 30, 2020 at 11:27
  • I was testing if my idea was gonna work..Cus the program that i am building,triggers a series of actions if distance is lesser than 3 Commented Jul 30, 2020 at 11:33
  • You are not checking for the distance being lesser than 3. You first need to convert the data to a number, before you can compare it that way. Currently you are comparing two strings lexically, not 3 numbers. Commented Jul 30, 2020 at 11:45

1 Answer 1

1

You are trying to measure with 2 ultrasonic sensors at once. You cannot do that so easily. You need to measure sequentially for 2 reasons:

  1. The ultrasonic sensors might get confused by the others signal
  2. You will miss the pulse of the one sensor, while you are reading the pulse of the other via pulseIn().

So the order of actions is like this:

  1. Trigger sensor 1
  2. Read sensor 1 with pulseIn()
  3. Trigger sensor 2
  4. Read sensor 2 with pulseIn()
answered Jul 30, 2020 at 14:22
5
  • Thanks so much!It works perfectly! Commented Jul 30, 2020 at 16:11
  • Is there a way i can differentiate the 2 inputs? Commented Jul 30, 2020 at 16:34
  • You can print an identifier before it, for example 1:100 for the value 100 of the first sensor. Then you can split the string in python at the colon to get the sensor number and the value individually Commented Jul 30, 2020 at 16:50
  • Is it possible to add for eg "a" and "b" to differentiate the 2 values? Commented Aug 7, 2020 at 11:51
  • Sure, just Serial.print them before each corresponding value Commented Aug 7, 2020 at 12:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.