Skip to content

Commit f1e223c

Browse files
authored
Merge pull request adafruit#1233 from ladyada/master
some tests
2 parents 765f236 + c2b0393 commit f1e223c

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

DYP_ultrasonics/me007ys.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import time
2+
import serial
3+
4+
SERIAL_PORT = "/dev/ttyS15" # or "COM4" or whatever
5+
6+
serialport = serial.Serial(SERIAL_PORT, 9600)
7+
8+
9+
def read_me007ys(ser, timeout = 1.0):
10+
ts = time.monotonic()
11+
buf = bytearray(3)
12+
idx = 0
13+
14+
while True:
15+
# Option 1, we time out while waiting to get valid data
16+
if time.monotonic() - ts > timeout:
17+
raise RuntimeError("Timed out waiting for data")
18+
19+
c = ser.read(1)[0]
20+
#print(c)
21+
if idx == 0 and c == 0xFF:
22+
buf[0] = c
23+
idx = idx + 1
24+
elif 0 < idx < 3:
25+
buf[idx] = c
26+
idx = idx + 1
27+
else:
28+
chksum = sum(buf) & 0xFF
29+
if chksum == c:
30+
return (buf[1] << 8) + buf[2]
31+
idx = 0
32+
return None
33+
34+
while True:
35+
dist = read_me007ys(serialport)
36+
print("Distance = %d mm" % dist)

DYP_ultrasonics/me007ys/.trinket_m0.test.only

Whitespace-only changes.

DYP_ultrasonics/me007ys/.uno.test.only

Whitespace-only changes.

DYP_ultrasonics/me007ys/me007ys.ino

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Example code for DYP-ME007YS sensor
2+
3+
#if defined(__AVR__) || defined(ESP8266)
4+
// For UNO and others without hardware serial, we must use software serial...
5+
// pin #2 is IN from sensor (WHITE wire)
6+
// Set up the serial port to use softwareserial..
7+
#include <SoftwareSerial.h>
8+
SoftwareSerial mySerial(2, -1);
9+
10+
#else
11+
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
12+
// #0 is white wire, data input
13+
#define mySerial Serial1
14+
15+
#endif
16+
17+
#define CONTROL_PIN 5 // This is the YELLOW wire, can be any data line
18+
19+
int16_t distance; // The last measured distance
20+
bool newData = false; // Whether new data is available from the sensor
21+
uint8_t buffer[4]; // our buffer for storing data
22+
uint8_t idx = 0; // our idx into the storage buffer
23+
24+
void setup() {
25+
// Open serial communications and wait for port to open:
26+
Serial.begin(115200);
27+
while (!Serial) {
28+
delay(10); // wait for serial port to connect. Needed for native USB port only
29+
}
30+
31+
Serial.println("Adafruit DYP-ME007YS Test");
32+
33+
// set the data rate for the Serial port, 9600 for the sensor
34+
mySerial.begin(9600);
35+
pinMode(CONTROL_PIN, OUTPUT);
36+
digitalWrite(CONTROL_PIN, HIGH);
37+
}
38+
39+
40+
void loop() { // run over and over
41+
if (mySerial.available()) {
42+
uint8_t c = mySerial.read();
43+
Serial.println(c, HEX);
44+
45+
// See if this is a header byte
46+
if (idx == 0 && c == 0xFF) {
47+
buffer[idx++] = c;
48+
}
49+
// Two middle bytes can be anything
50+
else if ((idx == 1) || (idx == 2)) {
51+
buffer[idx++] = c;
52+
}
53+
else if (idx == 3) {
54+
uint8_t sum = 0;
55+
sum = buffer[0] + buffer[1] + buffer[2];
56+
if (sum == c) {
57+
distance = ((uint16_t)buffer[1] << 8) | buffer[2];
58+
newData = true;
59+
}
60+
idx = 0;
61+
}
62+
}
63+
64+
if (newData) {
65+
Serial.print("Distance: ");
66+
Serial.print(distance);
67+
Serial.println(" mm");
68+
newData = false;
69+
}
70+
}

DYP_ultrasonics/me007ys_OLED/.trinket_m0.test.only

Whitespace-only changes.

DYP_ultrasonics/me007ys_OLED/.uno.test.only

Whitespace-only changes.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <Adafruit_SSD1306.h>
2+
#include <Fonts/FreeSans9pt7b.h>
3+
4+
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
5+
6+
// Example code for DYP-ME007YS sensor
7+
8+
#if defined(__AVR__) || defined(ESP8266)
9+
// For UNO and others without hardware serial, we must use software serial...
10+
// pin #2 is IN from sensor (WHITE wire)
11+
// Set up the serial port to use softwareserial..
12+
#include <SoftwareSerial.h>
13+
SoftwareSerial mySerial(2, -1);
14+
15+
#else
16+
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
17+
// #0 is white wire, data input
18+
#define mySerial Serial1
19+
20+
#endif
21+
22+
#define CONTROL_PIN 5 // This is the YELLOW wire, can be any data line
23+
24+
int16_t distance; // The last measured distance
25+
bool newData = false; // Whether new data is available from the sensor
26+
uint8_t buffer[4]; // our buffer for storing data
27+
uint8_t idx = 0; // our idx into the storage buffer
28+
29+
void setup() {
30+
// Open serial communications and wait for port to open:
31+
Serial.begin(115200);
32+
33+
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
34+
Serial.println(F("SSD1306 allocation failed"));
35+
for(;;); // Don't proceed, loop forever
36+
}
37+
display.display();
38+
delay(500);
39+
display.setTextColor(WHITE);
40+
display.setFont(&FreeSans9pt7b);
41+
display.setTextSize(1);
42+
43+
Serial.println("Adafruit DYP-ME007YS Test");
44+
45+
// set the data rate for the Serial port, 9600 for the sensor
46+
mySerial.begin(9600);
47+
pinMode(CONTROL_PIN, OUTPUT);
48+
digitalWrite(CONTROL_PIN, HIGH);
49+
}
50+
51+
52+
void loop() { // run over and over
53+
if (mySerial.available()) {
54+
uint8_t c = mySerial.read();
55+
Serial.println(c, HEX);
56+
57+
// See if this is a header byte
58+
if (idx == 0 && c == 0xFF) {
59+
buffer[idx++] = c;
60+
}
61+
// Two middle bytes can be anything
62+
else if ((idx == 1) || (idx == 2)) {
63+
buffer[idx++] = c;
64+
}
65+
else if (idx == 3) {
66+
uint8_t sum = 0;
67+
sum = buffer[0] + buffer[1] + buffer[2];
68+
if (sum == c) {
69+
distance = ((uint16_t)buffer[1] << 8) | buffer[2];
70+
newData = true;
71+
}
72+
idx = 0;
73+
}
74+
}
75+
76+
if (newData) {
77+
Serial.print("Distance: ");
78+
Serial.print(distance);
79+
Serial.println(" mm");
80+
newData = false;
81+
display.clearDisplay();
82+
display.setCursor(0, 13);
83+
display.print("UART Sonar");
84+
display.setCursor(0, 30);
85+
display.print("Dist.: ");
86+
display.print(distance);
87+
display.println(" mm");
88+
display.display();
89+
}
90+
}

0 commit comments

Comments
 (0)