Ping))) sensor

I need help combining the two example codes to help sweep a servo and run a ping sensor at the same time. the two codes i have are these… first the servo

#include <ServoTimer1.h>

ServoTimer1 servo1;
ServoTimer1 servo2;

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Servo test!”);
servo1.attach(10);
servo2.attach(9);
}

void loop() {
Serial.print(“tick”);
servo1.write(180);
servo2.write(0);
delay(1000);

Serial.print(“tock”);
servo1.write(0);
servo2.write(180);
delay(1000);
}

Next code is the PING

int pingPin = 7;

void setup()
{
Serial.begin(9600);
}

void loop()
{
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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;
}

This is the code that i came up with but it only sweeps the servos then scans one time then sweeps the area again it dont do it all at the same time…

#include <ServoTimer1.h>

int pingPin = 2;
ServoTimer1 servo1;
ServoTimer1 servo2;

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Servo test!”);
servo1.attach(10);
servo2.attach(9);
}

void loop() {
Serial.print(“tick”);
servo1.write(180);
servo2.write(0);
delay(1000);

Serial.print(“tock”);
servo1.write(0);
servo2.write(180);
delay(1000);

long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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;
}

Untested, uncompiled:

#include <ServoTimer1.h>

int pingPin = 2;
ServoTimer1 servo1;
ServoTimer1 servo2;

void setup() {
 Serial.begin(9600);           // set up Serial library at 9600 bps
 Serial.println("Servo test!");
 servo1.attach(10);
 servo2.attach(9);
}


void loop() {
 Serial.print("tick");
 servo1.write(180);
 servo2.write(0);
 report (ping ());
 delay(1000);

 Serial.print("tock");
 servo1.write(0);
 servo2.write(180);
 report (ping ());
 delay(1000);
}

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

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

long ping ()
{
 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // We 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);
 return pulseIn(pingPin, HIGH);
}


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;
}

thank you that has really helped me allot!

but what if i want it to report the ping the entire time it sweeps??

but what if i want it to report the ping the entire time it sweeps??

As they say in the textbooks, "an exercise for the reader". ;)

Hint: You don't have to sweep 180 degrees every time.

I have wrote a whole Principle of Operation for my robot.

ROBOT ACTION OF EVENTS AND PRINCIPLE OF OPERATION.

Arduino sweeps servo 180 degrees while reading reading from ping sensor (ping sensor runs for entire sweep) Arduino determines the distance to the objects around it and decides which direction is the clearest or furthest away and heads in that direction by setting the 2 DC motors connected to the Adafruit motor shield. If the Arduino decides to go left it must tun left motor backwards and right motor forward if Arduino decides to turn right the right motor must urn backwards and the left motor would turn forward. (you must turn like this because it is a tank)

As the Arduino turns and takes off in its chosen direction the ping sensor will tell the Arduino how close it is to the nearest objects in front of it. When the Arduino decides it has gotten to its chosen destination, it will scan servo from left to right while using the ping sensor to scan the area for the next clearest path.... and this whole process just repeats over and over until i cut the power

I am so lost in all of this and have no idea where to start :-/

and have no idea where to start

To quote Julie Andrews*, “Let’s start at the very beginning (a very good place to start)”

  • Always worth good quote, either as Mary Poppins or Maria von Trapp.

Arduino sweeps servo 180 degrees while reading reading from ping sensor

So:

for (int angle = 0; angle < 180; angle += 10) [
  servo.write (angle);
  delay(15);
  report (ping ());
}

Why “+= 10”?
Well, the ping beam is probably at least that wide, so little point in going by degree increments.

Any help?

Of course, this doesn’t find you the direction you want, but it does report the values back to you.
So, now you either need an array for all the readings, or you need to keep a record of the largest/smallest distance and the angle you found it at, updating it each time you get a new reading.

I don't know I am lost in all of it, I will probably never get it moving.

I think you should take one step at a time with this one :)

The first step could be to get a sensor moving.

Then you could add some kind of control to the movement. Maybe a potentiometer, or a switch for direction or something.

The next step could be to get the ping() working. The serial monitor is your best friend. Debug all useful paramters to it while testing and doing 'proof of concept' work.

The final step, for the proofing of the concepts, would be to get the movement right. Create some code that makes the threads cause forward/backward movement and rotation. The rotation should be implemented in such a way that you can control the degree/amount of rotation in a relatively precise manner.

After this, you are ready for combining the code.

i pretty much have every aspect of the system running individually i can get the ping to run and return values to you serial monitor and i can get the DC motors moving, also the servo to pan the area... i am just having trouble trying to understand how to combine all of the code, and getting it all to work together.

I would definitly use an FSM.

If you have working code, this would be wery easy to implement.

Make each working code into a function. For instance, the scan code could be moved to a scan() function.
The Move code could be moved to a move() function.

Then, using the FSM library would be easy.
This library is intended to do exactly what you need. Making many concepts/goals/behaviors cooperate.

[edit]
I made a framework / dummy implementation of what I think is within the realm of you goal.

/*
A framework based the robot idea by lavite

Implement the scan and move functions, and this should be close to finished.
*/
//FSM_beta.zip
#include <FiniteStateMachine.h>

//configuration
const byte PING_PIN = 2;
boolean useMetricMeasure = true;

//constants
const byte uS_PER_CM = 29;
const byte uS_PER_INCH = 74;

//states
State Scan = State(scan);
State Move = State(move);

//initialize state machine, start with a Scan
FSM scanbot = FSM(Scan);

void setup(){ /nothing/ }

void loop(){
scanbot.update(); //process state changes and update current state
}

//this funciton is called by Scan state, when updated
void scan(){
//implement scan code

//if (scan is complete){
scanbot.transitionTo( Move );
//}
}

//this funciton is called by Move state, when updated
void move(){
//implement move code

//if (goal reached){
scanbot.transitionTo( Scan );
//}
}

//return distance
long ping(){
long duration = 0;
pinMode(PING_PIN, OUTPUT);
digitalWrite(PING_PIN, LOW);
delayMicroseconds(2);
digitalWrite(PING_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(PING_PIN, LOW);
pinMode(PING_PIN, INPUT);
duration = pulseIn(PING_PIN, HIGH);

return (duration / (useMetricMeasure?uS_PER_CM:uS_PER_INCH) / 2);
}

This code, as is, will simply alternate between calling scan() and move().
You are free to implement these functions in order to provide functionality to this code.

Only execute the lines that says transitionTo( x ); if you want to change state. (Either done with scanning, or done with moving).

My final tip, is to make the scan() function unatomic (that is most likely a wrong use of the word, if it even is a word). But what I mean is that you should design the scan() (and move()) in such a way that you will need to call the function a couple of times before the transitionTo( x ); triggers.
This way, you can do other ‘stuff’ in the loop, and fake a sense of ‘multitasking’.[/edit]

Thanks for your code, it looks good. I am starting to understand this more and more as i read through the codes everyone has given me. I'm gonna have to sit down and read through this stuff some more and try to see if i can come up with anything workable.

quick question about implementing the codes... for example here is the example code to get the Ping up and running. How would i go about implementing this into your code where it says to implement the code.

int pingPin = 2;

void setup() { Serial.begin(9600); }

void loop() { long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // We 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; }

The code I posted here already implements ping.

You can do this from the code already.

int distance = ping();

Maybe not a clear/good name. Coud rename it to getDistance() or something.

int distance = ping();

{raise hand} Erm, that one was mine. {mumbles} Sorry. :-[

int distance = ping();

{raise hand} Erm, that one was mine. {mumbles} Sorry. :-[

What do you mean? :)

[edit]Was it a joke? Or have I done something?

Not easy to decode emotions and/or irony/humor on forums. >:([/edit]

I mean it was me who called it "ping" instead of "getDistance"

Here is a link to a picture of the bot

http://i81.photobucket.com/albums/j208/tylerlavite/DSC_0544.jpg

I figured id post a link and let you guys see what it looks like.

Your code looks good but i don't understand how to implement the servo code. I also still am having trouble trying to figure out how to get the Adafruit motor shield to control 2 dc motors, I know it can but I'm having trouble trying to figure out how.