4-pin Ultrasonic Sensor to 3-pin Ultrasonic Sensor Conversion

I’m trying to make the instructable for the Backpack Anti-Theft device (http://www.instructables.com/id/Backpack-Anti-Theft-Alarm/), but I have some problems with the coding due the face that I am using the PING Ultrasonic Sensor from Parallax rather than the HC-SR04 that is listed in the instructions list. I’m trying to convert the code from 4-pin to 3-pin but I’m unsure as to how to achieve said goal.

The code provided was also modified by me to include 2 lines of code in order to be uploaded onto the ATtiny85.
#include <Arduino.h>
#include <WProgram.h>

//pin definitions
#define trigPin 2
#define echoPin 4
#define buzzerPin 0
//constant definitions
#define DELAY_BEFORE_INITIAL 5000
#define ALARM_DISTANCE 35
#define BUZZER_FREQ 500
//variables
#include <Arduino.h>
#include <WProgram.h>
boolean alarm = false;
long currentDistance;

void setup() {
  //setup pins
  pinMode(buzzerPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //delay before beginning loop
  delay(DELAY_BEFORE_INITIAL);
}

void loop() {
  if(!alarm){
  //check distance
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;  
  //if distance is greater than ALARM_DISTANCE begin alarm
  if(distance>=ALARM_DISTANCE)
    alarm=true;
  }
  else{
    delay(BUZZER_FREQ);
    digitalWrite(buzzerPin, HIGH);
    delay(BUZZER_FREQ);
    digitalWrite(buzzerPin,LOW);
  }
}

Any help would be greatly appreaciated.
Thanks in advanced :slight_smile:

Why not look at the Ping example code?

Oh , didn't even know that existed :cold_sweat: :sweat_smile: I'll see if I can figure out how to integrate it in.

I made some modifications to my code, unsure if it works but the compile button doesn’t say anything is wrong with it. Gonna upload it and give it a try after finishing my wire wrapping.

//pin definitions
#define trigPin 2
#define echoPin 4
#define buzzerPin 0
//constant definitions
#define DELAY_BEFORE_INITIAL 5000
#define ALARM_DISTANCE 35
#define BUZZER_FREQ 500
//variables
#include <Arduino.h>
#include <WProgram.h>
boolean alarm = false;
long currentDistance;
const int pingPin = 2;

void setup() {
  //setup pins
  pinMode(buzzerPin, OUTPUT);
  //delay before beginning loop
  delay(DELAY_BEFORE_INITIAL);
}

void loop() {
   pinMode(pingPin, OUTPUT);
  if(!alarm){
  //check distance
  long duration, distance;
  digitalWrite(pingPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5); // Added this line
  digitalWrite(pingPin, LOW);
  duration = pulseIn(pingPin, HIGH);
  distance = (duration/2) / 29.1;  
  pinMode(pingPin,INPUT);
  //if distance is greater than ALARM_DISTANCE begin alarm
  if(distance>=ALARM_DISTANCE)
    alarm=true;
  }
  else{
    delay(BUZZER_FREQ);
    digitalWrite(buzzerPin, HIGH);
    delay(BUZZER_FREQ);
    digitalWrite(buzzerPin,LOW);
  }
}