I want to use the Mega since it has extra hardware serial ports.
The example includes reducing the baud rate of the sonar to 9600, since this is the maximum for the SoftwareSerial function. I want to run the Ping using hardware serial ports at 115200 baud.
I changed the TX and RX pins in the same code to hardware port 1 (pins 18 and 19 on the Mega) and ran the code. All I got on the serial monitor was error messages (ping did not initialize).
#include "ping1d.h"
static const uint8_t arduinoRxPin = 19; //Serial1 rx
static const uint8_t arduinoTxPin = 18; //Serial1 tx
static Ping1D ping { Serial1 };
static const uint8_t ledPin = 13;
void setup()
{
Serial1.begin(115200);
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));
}