I am trying to run this code
#include <SoftwareSerial.h>
#include "RoboClaw.h"
#include <stdlib.h>
#include "ping1d.h"
SoftwareSerial serial(2,3);
RoboClaw roboclaw(&serial, 10000);
#define address 0x80
long desiredRPM = 0;
bool skip_zero = false;
static const uint8_t arduinoRxPin = 8;
static const uint8_t arduinoTxPin = 7;
SoftwareSerial pingSerial = SoftwareSerial(arduinoRxPin, arduinoTxPin);
static Ping1D ping { pingSerial };
void setup() {
Serial.begin(115200);
roboclaw.begin(9600);
pingSerial.begin(9600);
}
void loop() {
uint8_t status1, status2;
bool valid1, valid2;
int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
int32_t speed1 = roboclaw.ReadSpeedM1(address, &status2, &valid2);
if (Serial.available() > 0) {
desiredRPM = Serial.parseInt(SKIP_ALL);
Serial.print("<R,HP,");
Serial.print(desiredRPM);
Serial.println(">");
Serial.flush();
// skip_zero is only for parseInt, it returns 0 after every number entered
if (desiredRPM == 0 && skip_zero) {
skip_zero = false;
} else {
skip_zero = true;
if (desiredRPM < 3200) {
desiredRPM = desiredRPM * 120;
roboclaw.SpeedM1(address, desiredRPM);
} else {
Serial.println("!!!! DANGER! setting values more than 1000 RPM !!!!");
}
}
}
// Listen for data from ping sensor
pingSerial.listen();
if (ping.update()) {
Serial.print("<P1D,");
Serial.print(ping.distance());
Serial.print(",");
Serial.print(ping.confidence());
Serial.println(">");
}
else
{
Serial.println("<E,P1D>");
}
// Switch back to listening for data from Roboclaw
roboclaw.listen();
if (valid2) {
Serial.print("<HP,");
Serial.print(float(speed1) / 120.0f);
Serial.println(">");
}
delay(100);
}
But sometimes when i unplug and replug arduino Ping1D stops sending values and goes into the error packet.
When i upload this code, the ping1-D simple in such a situation (no wiring changed)||
/**
* 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(8, arduin7oTxPin);
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));
}
it runs again! and then when i upload my previous code, it runs fine too!
Any help to understand this behaviour would be appreciated!