How to add Ping))) to this code

I was wondering how and where would I put the code for PING))) in the code below and how would I wire this to a L298N H-Bridge?

int PWM1  = 6;  // PWM Pin Motor 1
int PoM1  = 7;  // Polarity Pin Motor 1
int PWM2  = 5;  // PWm Pin Motor 2
int PoM2  = 4;  // Polarity Pin Motor 2

int ValM1  = 0;  // Initial Value for PWM Motor 1
int ValM2  = 0;  // Initial Value for PWM Motor 2

int i = 25;  // increment 
boolean goUp = true;  // Used to detect acceleration or deceleration

void setup()
  pinMode(PWM1,  OUTPUT);
  pinMode(PoM1,  OUTPUT);
  pinMode(PWM2, OUTPUT);
  pinMode(PoM2, OUTPUT);
  digitalWrite(PoM1, LOW);  // Both motors with the sam polarity
  digitalWrite(PoM2, LOW);
  analogWrite(PWM1, ValM1);  // Stops both motors => ValMx = 0
  analogWrite(PWM2, ValM2);  
  Serial.begin(9600);        // Used to check value

// Main program

void loop()
  delay (500);                 // Give time to theh motor to adapt to new value 
  if ((ValM1 < 250) && goUp)  // First phase of acceleration
    ValM1 = ValM1 + i;  // Increase PWM value => Acceleration
    ValM1 = ValM1 + i;
    goUp = false;        // Accerleration completed
    ValM1 = ValM1 - i;  // Decrease PWM => deceleration
    ValM2 = ValM2 - i;
    if (ValM1 < 75)
      ValM1 = 0;
      ValM2 = 0;
      goUp =
  if ((ValM1 > 75) && ((ValM1 < 255)))
    analogWrite(PWM1, ValM1);
    analogWrite(PWM2, ValM2);

Why do you want to wire a Ping to an H-bridge?

I doubt it can be done.


I want to attach the Ping))) and L289N to an Arduino board to control two motors.

Have you had a look at the Playground?

Here's the basic code to use the PING))) The tone is to hook up to a piezo speaker, and it changes pitch based off the distance. I'm not entirely sure what you want to do with it, but if you needed the code, here you go.

 int tone_freq=0;
  long duration, inches;
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(3, LOW);
  pinMode(3, INPUT);
  duration = pulseIn(3, HIGH);
  Serial.print(148, BYTE);
  inches = (duration/2)/74;
  Serial.print(137, BYTE);
  tone_freq= inches*100;

[quote author=Nick Gammon link=topic=104141.msg781299#msg781299 date=1336034493]

I doubt it can be done. [/quote]

I think he meant this

Ping != Ping)))

I have searched the internet for about four hours trying to find a good example on how to wire and Arduino Mega, L289N Motor Control, and a Parallax Ping))) all together? I was tired of searching the internet and didn't look on the forum. If you know how or know of a good/great example that shows how and with some code, could you point me in that direction?

Thank you.

P.S. I did wire every but I didn't power it up since I am afraid that I would fry my Mega and I have already did that once.

If I have to merge another of your duplicate posts, I'm going to start deleting them.

I was wondering why are you merging them? My first question was about coding and the second question was about wire them altogether.

Or, in the first case, both

Tonyodonnell01: how to wire and Arduino Mega, L289N Motor Control, and a Parallax Ping))) all together?

How about a link to the datasheet for this Ping thing?