SHARE
TWEET

Ultrasonic ping-pong

a guest Nov 15th, 2011 139 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int ultraSoundpin1 = 7;
  2. int ultraSoundpin2 = 5;
  3. unsigned long ultrasoundDuration;
  4.  
  5. void setup() {
  6.   Serial.begin(9600);
  7. }
  8.  
  9. void loop() {
  10.  
  11.   // switch pin to output
  12.   pinMode(ultraSoundpin1, OUTPUT);
  13.  
  14.   // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  15.   digitalWrite(ultraSoundpin1, LOW);
  16.   delayMicroseconds(2);
  17.   digitalWrite(ultraSoundpin1, HIGH);
  18.   delayMicroseconds(10);
  19.   digitalWrite(ultraSoundpin1, LOW);
  20.  
  21.    // switch pin to input
  22.   pinMode(ultraSoundpin1, INPUT);
  23.  
  24.   // wait for a pulse to come in as high
  25.   ultrasoundDuration = pulseIn(ultraSoundpin1, HIGH);
  26.  
  27.   // output
  28.   Serial.print(ultrasoundDuration);
  29.   Serial.print(";");
  30.  
  31.  
  32.     // switch pin to output
  33.   pinMode(ultraSoundpin2, OUTPUT);
  34.  
  35.   // send a low, wait 2 microseconds, send a high then wait 10 microseconds
  36.   digitalWrite(ultraSoundpin2, LOW);
  37.   delayMicroseconds(2);
  38.   digitalWrite(ultraSoundpin2, HIGH);
  39.   delayMicroseconds(10);
  40.   digitalWrite(ultraSoundpin2, LOW);
  41.  
  42.    // switch pin to input
  43.   pinMode(ultraSoundpin2, INPUT);
  44.  
  45.   // wait for a pulse to come in as high
  46.   ultrasoundDuration = pulseIn(ultraSoundpin2, HIGH);
  47.  
  48.   // output
  49.   Serial.print(ultrasoundDuration);
  50.   Serial.print(":");
  51.  
  52.   delay(50);
  53. }
  54.  
  55.  
RAW Paste Data
Top