Zumo with reflectance relay and ping

I am extremly new to robotics, and I am working on modifying the generic zumo example code to work with a 3 pin ping style ultrasonic sensor. Where can I connect the sensor so that it will not interfere with the reflectance array. In addition, is there an example I can see of this so that I can have a vague clue. Thanks for any help.

Hello.

Do you have the Zumo 32U4 Robot or the Zumo Robot for Arduino?

- Grant

I have the zumo for arduino.

If you are not already using the pins in the front expansion, you should be able to add the sensor connections there. You can find more information in the “Front expansion” section in the Zumo user’s guide which can be found under the “Resources” tab on the Zumo’s product page. Since you are also using the Zumo reflectance sensor array, you should check to make sure the sensor array is not already using the pins. In this post Jon explains which pins are available and where you would check in the user’s guide to find out more about the pins.

- Grant

I did the research and I cannot get it to work. Here is my code.

[code]#include <QTRSensors.h>

#include <ZumoBuzzer.h>
#include <ZumoMotors.h>
#include <Pushbutton.h>
#include <QTRSensors.h>
//#include <ZumoReflectanceSensorArray.h>

#define LED 13

// this might need to be tuned for different lighting conditions, surfaces, etc.
#define QTR_THRESHOLD 1500 // microseconds

// these might need to be tuned for different motor types
#define REVERSE_SPEED 400 // 0 is stopped, 400 is full speed
#define TURN_SPEED 400
#define FORWARD_SPEED 200 // 200 original
#define REVERSE_DURATION 110 // ms
#define TURN_DURATION 200 // ms //150 works
ZumoBuzzer buzzer;
ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON); // pushbutton on pin 12

#define NUM_SENSORS 2
unsigned int sensor_values[NUM_SENSORS];
QTRSensorsRC sensors ((unsigned char[]) {4,5}, NUM_SENSORS);
int turns;
int Maxturns;
const int pingPin = 7;

//ZumoReflectanceSensorArray sensors(QTR_NO_EMITTER_PIN);

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
//int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

//int pingPin = 11; //the digital pin connected to the PIR sensor’s output
int ledPin = 13;

void waitForButtonAndCountDown()
{
digitalWrite(LED, HIGH);
button.waitForButton();
digitalWrite(LED, LOW);

// play audible countdown
for (int i = 0; i < 3; i++)
{
delay(1000);
buzzer.playNote(NOTE_G(3), 200, 15);
}
delay(1000);
buzzer.playNote(NOTE_G(4), 500, 15);
delay(1000);
}

void setup()
{
delay(500);

Serial.begin(9600);
// uncomment if necessary to correct motor directions
motors.flipLeftMotor(true);
motors.flipRightMotor(true);

pinMode(LED, HIGH);
waitForButtonAndCountDown();
//pinMode(pirPin, INPUT);
//pinMode(ledPin, OUTPUT);
//digitalWrite(pirPin, LOW);
turns=0;
turns = random(3,10);

}

void loop()
{
if (button.isPressed())
{
// if button is pressed, stop and wait for another press to go again
motors.setSpeeds(0, 0);
button.waitForRelease();
waitForButtonAndCountDown();
}

sensors.read(sensor_values);

if (sensor_values[0] < QTR_THRESHOLD)
{
if(turns>=Maxturns){
//motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
//delay(REVERSE_DURATION);
int time=random(100,225);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(time);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
turns = random(3,10);
turns=0;
}
// if leftmost sensor detects line, reverse and turn to the right
//motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
//delay(REVERSE_DURATION);
motors.setSpeeds(TURN_SPEED, -TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
turns++;
}
else if (sensor_values[1] < QTR_THRESHOLD)
{
if(turns>=Maxturns){
//motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
//delay(REVERSE_DURATION);
int time=random(100,225);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(time);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
turns = random(3,10);
turns=0;
}
// if rightmost sensor detects line, reverse and turn to the left
motors.setSpeeds(-REVERSE_SPEED, -REVERSE_SPEED);
//delay(REVERSE_DURATION);
motors.setSpeeds(-TURN_SPEED, TURN_SPEED);
delay(TURN_DURATION);
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
turns++;
poop();
}
//else if(digitalRead(7)){
// motors.setSpeeds(0,0);
//}

else
{
// otherwise, go straight
motors.setSpeeds(FORWARD_SPEED, FORWARD_SPEED);
}
}

//The loop for the ultra sonic sensor
void poop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:

long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

delay(100);
}
long microsecondsToInches(long microseconds) {
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
[/code]
Any help would be appreciated as I am rather befuddled…

Hello.

Your code is pretty long and has a lot of things going on. Could you simplify your code to the simplest thing that still demonstrates the issue? Could you also describe the problem you are having? What do you expect to happen and what is happening instead?

- Grant