This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*@File : DFRobot_Distance_A02.ino | |
*@Brief : This example use A02YYUW ultrasonic sensor to measure distance | |
* With initialization completed, We can get distance value | |
*@Copyright [DFRobot](https://www.dfrobot.com),2016 | |
* GUN Lesser General Pulic License | |
*@version V1.0 | |
*@data 2019-8-28 | |
*/ | |
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(11,10); // RX, TX | |
unsigned char data[4]={}; | |
float distance; | |
SoftwareSerial mySerial1(8,7); // RX, TX | |
unsigned char data1[4]={}; | |
float distance1; | |
SoftwareSerial mySerial2(5,4); // RX, TX | |
unsigned char data2[4]={}; | |
float distance2; | |
void setup() | |
{ | |
Serial.begin(57600); | |
mySerial.begin(9600); | |
mySerial1.begin(9600); | |
mySerial2.begin(9600); | |
} | |
void loop() | |
{ | |
mySerial.listen(); | |
delay(100); | |
if (mySerial.isListening()) { | |
while ((mySerial.available() > 0)) { | |
do{ | |
for(int i=0;i<4;i++) | |
{ | |
data[i]=mySerial.read(); | |
} | |
}while(mySerial.read()==0xff); | |
mySerial.flush(); | |
if(data[0]==0xff){ | |
int sum; | |
sum=(data[0]+data[1]+data[2])&0x00FF; | |
if(sum==data[3]) | |
{ | |
distance=(data[1]<<8)+data[2]; | |
if(distance>30) | |
{ | |
int distance = distance/10; | |
}else | |
{ | |
distance = -1; | |
} | |
}else distance = -2; | |
} | |
} | |
}else{ | |
distance = -3; | |
} | |
mySerial1.listen(); | |
delay(100); | |
if (mySerial1.isListening()) { | |
while ((mySerial1.available() > 0)) { | |
do{ | |
for(int i=0;i<4;i++) | |
{ | |
data1[i]=mySerial1.read(); | |
} | |
}while(mySerial1.read()==0xff); | |
mySerial1.flush(); | |
if(data1[0]==0xff){ | |
int sum1; | |
sum1=(data1[0]+data1[1]+data1[2])&0x00FF; | |
if(sum1==data1[3]) | |
{ | |
distance1=(data1[1]<<8)+data1[2]; | |
if(distance1>30) | |
{ | |
int distance1 = distance1/10; | |
}else | |
{ | |
distance1 = -1; | |
} | |
}else distance1 = -2; | |
} | |
} | |
}else{ | |
distance1 = -3; | |
} | |
mySerial2.listen(); | |
delay(100); | |
if (mySerial2.isListening()) { | |
while ((mySerial2.available() > 0)) { | |
do{ | |
for(int i=0;i<4;i++) | |
{ | |
data2[i]=mySerial2.read(); | |
} | |
}while(mySerial2.read()==0xff); | |
mySerial2.flush(); | |
if(data2[0]==0xff){ | |
int sum2; | |
sum2=(data2[0]+data2[1]+data2[2])&0x00FF; | |
if(sum2==data2[3]) | |
{ | |
distance2=(data2[1]<<8)+data2[2]; | |
if(distance2>30) | |
{ | |
int distance2 = distance2/10; | |
}else | |
{ | |
distance2 = -1; | |
} | |
}else distance2 = -2; | |
} | |
} | |
}else{ | |
distance2 = -3; | |
} | |
Serial.println((String) distance + " " + distance1 + " " + distance2); | |
memset(data, 0, sizeof(data)); | |
memset(data1, 0, sizeof(data1)); | |
memset(data2, 0, sizeof(data2)); | |
int sum=0; | |
int sum1=0; | |
int sum2=0; | |
distance=0; | |
distance1=0; | |
distance2=0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import serial as sr | |
import time | |
import sys | |
debug = int(sys.argv[1]) | |
sensorNum = str(sys.argv[2]) | |
delay = int(sys.argv[3]) | |
if delay > 0 : | |
time.sleep(delay) | |
port_serie = sr.Serial(port = "/dev/ttyUSB0", baudrate = "57600") | |
port_serie.setDTR(False) | |
time.sleep(0.1) | |
port_serie.setDTR(True) | |
port_serie.flushInput() | |
if debug == 1: | |
print(port_serie.readline()) | |
else: | |
val = port_serie.readline().split() | |
if sensorNum == "sensor1": | |
sensor = val[0] | |
elif sensorNum == "sensor2": | |
sensor = val[1] | |
else: | |
sensor = val[2] | |
print(int(float(sensor))) | |
port_serie.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment