better example master
better example

--- a/UltrasonicSensor/UltrasonicSensor.ino
+++ b/UltrasonicSensor/UltrasonicSensor.ino
@@ -20,32 +20,42 @@
 
 #include <UltrasonicInterrupt.h>
 
+#define DEBUG true
+
 const float SpeedOfSound     	= 343.2; // ~speed of sound (m/s) in air, at 20°C
 const float MicrosecondsPerMillimetre 	= 1000.0 / SpeedOfSound; // microseconds per millimetre - sound travels 1 mm in ~2.9us
 
 const float MicrosecondsToMillimetres  = (1.0 / MicrosecondsPerMillimetre);
 const float MicrosecondsToMillimetres2 = MicrosecondsToMillimetres / 2.0; // beam travels the distance twice... so halve the time.
 
-const int SIGNAL1_PIN	= 2; 	// digital pin connected to the trigger port on the module 
-const int INTERRUPT1  = 2;    // interrupt
-const int INTERRUPT1_PIN  = 2;  // custom interrupt pin, see above
-
 unsigned long lastTime	= 0;
 
 int pingTimer			= 0;
 int pingDelay			= 300; // milliseconds between ping pulses
 
+UltrasonicInterrupt s1;
+const int SIGNAL1_PIN	= 2; 	// digital pin connected to the trigger port on the module 
+const int INTERRUPT1  = 2;    // interrupt
+const int INTERRUPT1_PIN  = 2;  // custom interrupt pin, see above
 float millimetres_s1 		= 0.0;
 
-UltrasonicInterrupt s1;
+UltrasonicInterrupt s2;
+const int SIGNAL2_PIN	= 4; 	// digital pin connected to the trigger port on the module 
+const int INTERRUPT2  = 0;    // interrupt
+const int INTERRUPT2_PIN  = 3;  // custom interrupt pin, see above
+float millimetres_s2 		= 0.0;
 
 void setup() {
 
   Serial.begin(9600);
 
+  // start s1 sensor instance
   s1.setup(INTERRUPT1, INTERRUPT1_PIN, SIGNAL1_PIN, callback_s1);
-  // start listening out for the echo pulse on interrupt pin
   s1.begin();
+
+  // start s2 sensor instance
+  s2.setup(INTERRUPT2, INTERRUPT2_PIN, SIGNAL2_PIN, callback_s2);
+  s2.begin();
 }
 
 void loop() {
@@ -58,12 +68,18 @@
   if(pingTimer > pingDelay){
 	pingTimer = 0;
         s1.ping();
+        s2.ping();        
   }
   
   // do stuff...  
   if ( millimetres_s1 < 200 ) {
-    Serial.println ("obstacle ahead in under 200mm");
+    if (DEBUG)
+      Serial.println ("obstacle ahead in under 200mm");
   }
+  if ( millimetres_s2 < 200 ) {
+    if (DEBUG)
+      Serial.println ("obstacle ahead in under 200mm");
+  }  
 }
 
 /**
@@ -75,15 +91,44 @@
   millimetres_s1 = MicrosecondsToMillimetres2 * duration;
 
   if(millimetres_s1 > 4000){
-       Serial.println("s1 out of range");
-       // out of range (http://users.ece.utexas.edu/~valvano/Datasheets/HCSR04b.pdf)
-       millimetres_s1 = 4000;
+    if (DEBUG){
+      Serial.println("s1 out of range");
+    }
+      // out of range (http://users.ece.utexas.edu/~valvano/Datasheets/HCSR04b.pdf)
+      millimetres_s1 = 4000;
   } else {
+    if (DEBUG){
       Serial.print("s1 duration: ");
       Serial.print(duration);
       Serial.print(" us, distance: ");
       Serial.print(millimetres_s1);
       Serial.println(" mm");
+    }
+  }
+}
+
+/**
+* Pulse complete callback hanlder for UltrasonicInterrupt s2 instance
+* @param duration - pulse length in microseconds
+*/
+void callback_s2(unsigned long duration) {
+
+  millimetres_s2 = MicrosecondsToMillimetres2 * duration;
+
+  if(millimetres_s2 > 4000){
+    if (DEBUG){
+      Serial.println("s2 out of range");
+    }
+    // out of range (http://users.ece.utexas.edu/~valvano/Datasheets/HCSR04b.pdf)
+    millimetres_s2 = 4000;
+  } else {
+    if (DEBUG) {
+      Serial.print("s2 duration: ");
+      Serial.print(duration);
+      Serial.print(" us, distance: ");
+      Serial.print(millimetres_s2);
+      Serial.println(" mm");
+    }
   }
 }

 

comments