forked from bluerobotics/ping-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping1d-simple.ino
60 lines (53 loc) · 1.93 KB
/
ping1d-simple.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* This example is targeted toward the arduino platform
*
* This example demonstrates the most simple usage of the Blue Robotics
* Ping1D c++ API in order to obtain distance and confidence reports from
* the device.
*
* This API exposes the full functionality of the Ping1D Echosounder
*
* Communication is performed with a Blue Robotics Ping1D Echosounder
*/
#include "ping1d.h"
#include "SoftwareSerial.h"
// This serial port is used to communicate with the Ping device
// If you are using and Arduino UNO or Nano, this must be software serial, and you must use
// 9600 baud communication
// Here, we use pin 9 as arduino rx (Ping tx, white), 10 as arduino tx (Ping rx, green)
static const uint8_t arduinoRxPin = 9;
static const uint8_t arduinoTxPin = 10;
SoftwareSerial pingSerial = SoftwareSerial(arduinoRxPin, arduinoTxPin);
static Ping1D ping { pingSerial };
static const uint8_t ledPin = 13;
void setup()
{
pingSerial.begin(9600);
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
Serial.println("Blue Robotics ping1d-simple.ino");
while (!ping.initialize()) {
Serial.println("\nPing device failed to initialize!");
Serial.println("Are the Ping rx/tx wired correctly?");
Serial.print("Ping rx is the green wire, and should be connected to Arduino pin ");
Serial.print(arduinoTxPin);
Serial.println(" (Arduino tx)");
Serial.print("Ping tx is the white wire, and should be connected to Arduino pin ");
Serial.print(arduinoRxPin);
Serial.println(" (Arduino rx)");
delay(2000);
}
}
void loop()
{
if (ping.update()) {
Serial.print("Distance: ");
Serial.print(ping.distance());
Serial.print("\tConfidence: ");
Serial.println(ping.confidence());
} else {
Serial.println("No update received!");
}
// Toggle the LED to show that the program is running
digitalWrite(ledPin, !digitalRead(ledPin));
}