Help with programming Arduino Robot

Hi everyone,

I have made a promise to some kids that I would have an automated R2D2 for an event tomorrow and I have been really struggling to get the ultrasound sensor working. I have soldered the sensor to D3 and have been able to read the results via serial. The goal is to basically use the runaway robot with the NewPing library. The current code is based on the use of an analog sensor. Could anybody please help?

Thanks!
Jay.

/* Runaway Robot

 Play tag with your robot! With an ultrasonic
 distance sensor, it's capable of detecting and avoiding
 obstacles, never bumping into walls again!

 You'll need to attach an untrasonic range finder to M1.

 Circuit:
 * Arduino Robot
 * US range finder like Maxbotix EZ10, with analog output

 created 1 May 2013
 by X. Yang
 modified 12 May 2013
 by D. Cuartielles

 This example is in the public domain
 */

// include the robot 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(");
  }
}
  while (getDistance() < 40) { // If an obstacle is less than 20cm away

If you are going to have useless comments, you must keep them accurate.

I have soldered the sensor to D3 and have been able to read the results via serial.

You may have been able to read the results, and print them to serial. You can't read the results via serial.

The goal is to basically use the runaway robot with the NewPing library.

This makes no sense. The advantage of NewPing over the pulseIn() method of reading ping sensors is that you can do other things while waiting for pulses. Your sensor/device handles all the interaction with the pulse sender/reader, providing a variable voltage as output. So, almost no time is needed to read the sensor data. Not that it matters, since your code is full of delay()s that waste most of the robot's time, anyway.

The code you posted does something. You didn't explain what. You want it to do something. You didn't explain what. That you posted is likely an indication that the two somethings aren't the same thing, but that is all we know.

Hi and thank you for taking the time to look at my post.

The posted code is an example sketch from Arduino (so I apologise for their inaccuracy) that allows the robot to detect obstacles in order to change it's path.

I meant that I could read the results on my computer using the serial monitor.

Again, the delays are in the example sketch.

So, basically I wan't my Arduino Robot to autonomously move around a room without bumping in to stuff using a digital ultrasound sensor as opposed to the analog one used in the example. I thought the NewPing library would be best as it allows me to use 3 pins instead of the sensor's 4.

I am a newbie as you can see from my profile on the left and I am just trying to get to grips with everything. I thought there may be a way to translate to sonar detection side of things to use the NewPing library whilst maintaining the code that controls the motors.

Thanks!

Jay.

PS, just noticed when calculating the ultrasound measurements 40 equates to roughly 20cm, not an error.

I thought the NewPing library would be best as it allows me to use 3 pins instead of the sensor's 4.

Are you using the built in ultrasonic sensor? Or are you adding a sensor? The 3 pin code expects a three pin sensor. You can't just expect to ignore a pin.

If you can read data from the sensor now, using ONE pin, it hardly makes sense to change to another sensor that uses more pins.

PS, just noticed when calculating the ultrasound measurements 40 equates to roughly 20cm, not an error.

Then, that is what the comment should point it.

It still isn't clear what the robot is actually doing, or how that differs from what you want it to do. Pretend we can't see the robot, because we can't.

Hi,

There is no integrated ultrasonic sensor I am adding one, the robot it the official arduino robot as can be found on the site and I basically want it to manoeuvre around a room without bumping into an object, that's pretty much it.

Thanks!