NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

Thanks Tim,

I actually managed it before seeing this post but. I have actually soldered the sensor to the control board as this passes the commands to the motor board. The only problem I seem to be having is that once it detects an object within the desired range the pinging stops as do the measurements to the serial monitor. So when it is supposed to rotate and then carry on doing the same again it just seems to end and then the motors go into a wiggling action?

Here is my code:

#include <ArduinoRobot.h>
#include <NewPing.h>
#include <SPI.h>
#include <Wire.h>

#define TRIGGER_PIN  D3
#define ECHO_PIN  D3// Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

void setup() {
  // initialize the Robot, SD card, and display
  Serial.begin(9600);
  Robot.begin();
  Robot.beginTFT();
  Robot.beginSD();
  Robot.displayLogos();

  // draw a face on the LCD screen
  setFace(true);
}

void loop() 
{
    {
    unsigned int cm = sonar.ping_cm();
    Serial.print("Distance: ");
    Serial.print(cm);
    Serial.println("cm");
    delay(500);
    }
     
 
  // If the robot is blocked, turn until free
  while (sonar.ping_cm() < 20) { // If an obstacle is less than 20cm away
    setFace(false); //shows an unhappy face
    Robot.motorsStop(); // stop the motors
    delay(500); // wait for a moment
    Robot.turn(45); // turn to the right and try again
    setFace(true); // happy face
  }
  // if there are no objects in the way, keep moving
  Robot.motorsWrite(155, 155);
  delay(100);
}


// make a happy or sad face
void setFace(boolean onOff) {
  if (onOff) {
    // if true show a happy face
    Robot.background(0, 0, 255);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print(":)");
  } else {
    // if false show an upset face
    Robot.background(255, 0, 0);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print("X(");
  }
}

Thanks again!

Jay.