Code to run 2x HC-SR04 Ultrasonic sensors
Dependencies: mbed HC_SR04_Ultrasonic_Library
Revision 25:2e7a847a4432, committed 2020-02-17
- Comitter:
- rwhite3
- Date:
- Mon Feb 17 12:03:20 2020 +0000
- Parent:
- 24:7f14b70fc9ef
- Commit message:
- 2_Sensor_Ultrasonic
Changed in this revision
| Ultrasonic_Libs.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7f14b70fc9ef -r 2e7a847a4432 Ultrasonic_Libs.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ultrasonic_Libs.lib Mon Feb 17 12:03:20 2020 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
diff -r 7f14b70fc9ef -r 2e7a847a4432 main.cpp
--- a/main.cpp Mon Apr 08 11:03:25 2019 +0100
+++ b/main.cpp Mon Feb 17 12:03:20 2020 +0000
@@ -1,12 +1,49 @@
#include "mbed.h"
+#include "ultrasonic.h"
-DigitalOut myled(LED1);
+DigitalOut RED(LED1);
+DigitalOut GREEN(LED2);
+DigitalOut BLUE(LED3);
+
+void dist1(int distance1_mm)
-int main() {
+{
+ RED=1;
+ GREEN=1;
+ BLUE=1;
+ //put code here to happen when the distance is changed
+ int dis1_cm;
+ dis1_cm=distance1_mm/10;
+ printf("Distance of left sensor is %dcm\r\n", dis1_cm);
+ if (dis1_cm<100) {
+ RED=0;
+ } else (RED=1);
+}
+void dist2(int distance2_mm)
+{
+ RED=1;
+ GREEN=1;
+ BLUE=1;
+ int dis2_cm;
+ dis2_cm=distance2_mm/10;
+ printf("Distance of right sensor is %dcm\r\n", dis2_cm);
+ if (dis2_cm<100) {
+ GREEN=0;
+ } else (GREEN=1);
+}
+ultrasonic mu1(D8, D9, .1, 1, &dist1); //Set the trigger pin to D8 and the echo pin to D9
+ultrasonic mu2(D6, D7, .1, 1, &dist2); //have updates every .1 seconds and a timeout after 1
+//second, and call dist when the distance changes
+
+int main()
+{
+ mu1.startUpdates(); //start mesuring the distance for each sensor
+ mu2.startUpdates();
+
while(1) {
- myled = 1;
- wait(0.2);
- myled = 0;
- wait(0.2);
+
+ mu1.checkDistance(); //call checkDistance() as much as possible, as this is where
+ mu2.checkDistance(); //the class checks if dist needs to be called.
}
}
+