moved stuff to UltrasonicInterrupt
moved stuff to UltrasonicInterrupt

--- a/UltrasonicSensor/UltrasonicSensor.ino
+++ b/UltrasonicSensor/UltrasonicSensor.ino
@@ -23,33 +23,30 @@
 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 float MicrosecondsToMillimetres  = (1.0 / MicrosecondsPerMillimetre);
+const float MicrosecondsToMillimetres2 = MicrosecondsToMillimetres / 2.0; // beam travels the distance twice... so halve the time.
 
-const int SIGNAL_PIN	= 2; 	// digital pin connected to the trigger port on the module 
-                                // connect the echo port to the pin for interrupt 0 (pin 2 on Uno) 
-const int INTERRUPT_PIN  = 2;  // custom interrupt pin, see above
+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			= 500; // milliseconds between ping pulses
+int pingDelay			= 300; // milliseconds between ping pulses
 
-float millimetres 		= 0.0;
+float millimetres_s1 		= 0.0;
 
+UltrasonicInterrupt s1;
 
 void setup() {
 
   Serial.begin(9600);
 
-  pinMode(SIGNAL_PIN, OUTPUT);
-  digitalWrite(SIGNAL_PIN, LOW);
-
-  UltrasonicInterrupt::setup(pingPulseComplete, INTERRUPT_PIN);
-  // start listening out for the echo pulse on interrupt 0
-  UltrasonicInterrupt::begin();  
+  s1.setup(INTERRUPT1, INTERRUPT1_PIN, SIGNAL1_PIN, callback_s1);
+  // start listening out for the echo pulse on interrupt pin
+  s1.begin();
 }
-
 
 void loop() {
   
@@ -60,43 +57,32 @@
   pingTimer += dt;
   if(pingTimer > pingDelay){
 	pingTimer = 0;
-	ping();
+        s1.ping();
   }
   
   // do stuff...  
+  if ( millimetres_s1 < 200 ) {
+    Serial.println ("obstacle ahead in under 200mm");
+  }
 }
 
-
 /**
-* Trigger the outward ping pulse(s)
-*/
-void ping(){
-
- // Serial.println("ping out");
-  
-  digitalWrite(SIGNAL_PIN, HIGH);
-  delayMicroseconds(10); // I think I can cope with blocking for a whole 10us here...
-  digitalWrite(SIGNAL_PIN, LOW);
-}
-
-
-/**
-* Pulse complete callback hanlder for PulseInZero 
+* Pulse complete callback hanlder for UltrasonicInterrupt s1 instance
 * @param duration - pulse length in microseconds
 */
-void pingPulseComplete(unsigned long duration) {
+void callback_s1(unsigned long duration) {
 
-  millimetres = MicrosecondsToMillimetres2 * duration;
-  
-  if(millimetres > 4000){
-       Serial.println("out of range");
+  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 = 4000;
+       millimetres_s1 = 4000;
   } else {
-      Serial.print("duration: ");
+      Serial.print("s1 duration: ");
       Serial.print(duration);
       Serial.print(" us, distance: ");
-      Serial.print(millimetres);
+      Serial.print(millimetres_s1);
       Serial.println(" mm");
   }
 }


--- a/UltrasonicSensor/libraries/UltrasonicInterrupt/UltrasonicInterrupt.cpp
+++ b/UltrasonicSensor/libraries/UltrasonicInterrupt/UltrasonicInterrupt.cpp
@@ -4,8 +4,6 @@
 // initialise static vars
 bool UltrasonicInterrupt::state = false;
 bool UltrasonicInterrupt::active = false;
-int UltrasonicInterrupt::interrupt = 0;
-
 void (*UltrasonicInterrupt::onComplete)(unsigned long) = NULL;
 
 
@@ -13,10 +11,14 @@
 *
 *
 **/
-void UltrasonicInterrupt::setup(void (*pulseComplete)(unsigned long), int customInterrupt ){
-	onComplete 	= pulseComplete;
+void UltrasonicInterrupt::setup(int _interrupt, int _interruptPin, int _signalPin, void (*callback)(unsigned long) ){
+	onComplete = callback;
 	active 		= false;
-	interrupt   = customInterrupt;
+	interrupt   = _interrupt;
+	interruptPin = _interruptPin;
+	signalPin = _signalPin;
+	pinMode(signalPin, OUTPUT);
+	digitalWrite(signalPin, LOW);
 }
 
 
@@ -28,6 +30,16 @@
 	state		= false;
 	active 		= true;
 	attachInterrupt(interrupt, pinChange, HIGH);
+}
+
+/**
+* @public
+* Trigger the outward ping pulse(s)
+*/
+float UltrasonicInterrupt::ping(){
+	digitalWrite(signalPin, HIGH);
+	delayMicroseconds(10); // I think I can cope with blocking for a whole 10us here...
+	digitalWrite(signalPin, LOW);
 }
 
 

--- a/UltrasonicSensor/libraries/UltrasonicInterrupt/UltrasonicInterrupt.h
+++ b/UltrasonicSensor/libraries/UltrasonicInterrupt/UltrasonicInterrupt.h
@@ -7,15 +7,17 @@
 
 	public:
 
-		static void setup(void (*callback)(unsigned long), int customInterrupt);
+		void setup(int interrupt, int interruptPin, int signalPin, void (*callback)(unsigned long));
 
-		static void begin();
-		static void abandon();
+		void begin();
+		float ping();
+		void abandon();
 
 		static bool active;
 		static bool state;
-		static int interrupt;
-
+		int interrupt;
+		int interruptPin;
+		int signalPin;
 		static void (*onComplete)(unsigned long);
 
 	    // interrupt handler

comments