here is a project that i used sonar: Digitalduino: Ultrasonic Theremin
it has the sonar only code under "Code for Just Reading Sonar Value:"
the sonar reading code is:
//send a trigger signal
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
//receive the trigger signal echo, and calculate cm to the object
valueSensor= pulseIn(echo, HIGH);
dist= valueSensor/58; //converts raw to cm, valueSensor/74/2 = inches
Serial.println(dist);
so you could do:
//the following four lins for each of the A,B,C,D
#define trig 7 //trigger pin on sonar module
#define echo 6 //echo pin
int dist; //how far the object is away from the module(cm)
int valueSensor = 0; //see here, just added this line
const int buttonPin = 2; // the number of the pushbutton pin
// Variables will change:
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
digitalWrite(trigA, HIGH);
delayMicroseconds(10);
digitalWrite(trigA, LOW);
valueSensorA= pulseIn(echoA, HIGH);
distB= valueSensorA/58;
digitalWrite(trigB, HIGH);
delayMicroseconds(10);
digitalWrite(trigB, LOW);
valueSensorB= pulseIn(echoB, HIGH);
distB= valueSensorB/58;
digitalWrite(trigC, HIGH);
delayMicroseconds(10);
digitalWrite(trigC, LOW);
valueSensorC= pulseIn(echoC, HIGH);
distC= valueSensorC/58;
digitalWrite(trigD, HIGH);
delayMicroseconds(10);
digitalWrite(trigD, LOW);
valueSensorD= pulseIn(echoD, HIGH);
distD = valueSensorD/58;
buttonState = reading;
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
valueSensorX/58; is just for converting to inches