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

jaylfc:
Thanks for your help Tim, I have now attached the sensor to D3 and I am able to get readings via serial. Could you or anybody else help me change this code to work with your library:

#include <ArduinoRobot.h>

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

int sensorPin = M1;  // pin is used by the sensor

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() {
 // If the robot is blocked, turn until free
 while (getDistance() < 40) { // If an obstacle is less than 20cm away
   setFace(false); //shows an unhappy face
   Robot.motorsStop(); // stop the motors
   delay(1000); // wait for a moment
   Robot.turn(90); // turn to the right and try again
   setFace(true); // happy face
 }
 // if there are no objects in the way, keep moving
 Robot.motorsWrite(255, 255);
 delay(100);
}

// return the distance in cm
float getDistance() {
 // read the value from the sensor
 int sensorValue = Robot.analogRead(sensorPin);
 //Convert the sensor input to cm.
 float distance_cm = sensorValue * 1.27;
 return distance_cm;
}

// 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(");
 }
}




This is meant to be used with an analog sensor like a Maxbotix EZ10.

Thanks for your help so far!
Jay.

P.S. All of the LCD stuff isn't important, it is just the sensor working with the motors I need working. Thanks again!

To use NewPing, all you would do is include the library, setup the constructor, and send a ping. The basic NewPing example shows how to do this.

But, basically you would include the library like this:

#include <ArduinoRobot.h>

Setup the constructor:

NewPing sonar(sensorPin, sensorPin, 100);

And initiate a ping and get the result, which in your code would look something like this:

  while (sonar.ping_cm() < 40) { // If an obstacle is less than 40cm away

However, the sensor would need to be connected to the motor board, not the control board. I don't have an Arduino Robot to be able to tell you how to write your code. My guess is that you need two sketches, one for the control board and one for the motor board, and your sketch would need to communicate between the two boards to accomplish this. I have no first-hand usage of the Arduino Robot so there's no way for me to write working code for you. I tried, and it wouldn't compile.

I do know that you would need to connect the ultrasonic senor to one of the pins on the motor board (TK1-TK4). Other than that, you're on your own to write your own code. It should be VERY easy if you know how to program Arduino sketches and have any experience with using the Arduino Robot. If this is your first sketch, maybe you should start with something really basic first.

Tim