(DA) Internet of Things and Smart Electronics- ELE3006M2122
/
ObjectDetection_LiamCox
First Draft, serial print change based on distance
main.cpp@1:a1795335ef8c, 2021-12-16 (annotated)
- Committer:
- liam94
- Date:
- Thu Dec 16 11:41:38 2021 +0000
- Revision:
- 1:a1795335ef8c
- Parent:
- 0:506531d0531c
- Child:
- 2:3ace5b4ae9a7
Vnice
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
liam94 | 0:506531d0531c | 1 | #include "mbed.h" |
liam94 | 0:506531d0531c | 2 | #include "ultrasonic.h" |
liam94 | 0:506531d0531c | 3 | #include "N5110.h" |
liam94 | 0:506531d0531c | 4 | |
liam94 | 1:a1795335ef8c | 5 | void dist(int distance) |
liam94 | 1:a1795335ef8c | 6 | { |
liam94 | 1:a1795335ef8c | 7 | if (distance > 250){ |
liam94 | 1:a1795335ef8c | 8 | printf("Safe Distance %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 9 | } |
liam94 | 1:a1795335ef8c | 10 | else if (distance <= 250 and distance > 150){ |
liam94 | 1:a1795335ef8c | 11 | printf("object detected %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 12 | } |
liam94 | 1:a1795335ef8c | 13 | else if (distance <= 150 and distance > 80){ |
liam94 | 1:a1795335ef8c | 14 | printf("object detected Closer %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 15 | } |
liam94 | 1:a1795335ef8c | 16 | else if (distance <= 80 and distance > 60){ |
liam94 | 1:a1795335ef8c | 17 | printf("Even Closer %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 18 | } |
liam94 | 1:a1795335ef8c | 19 | else if (distance <= 60 and distance > 40){ |
liam94 | 1:a1795335ef8c | 20 | printf("Very Very Close %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 21 | } |
liam94 | 1:a1795335ef8c | 22 | else if (distance <= 40 and distance > 0){ |
liam94 | 1:a1795335ef8c | 23 | printf("You've Hit a Wall %dmm\r\n", distance); |
liam94 | 1:a1795335ef8c | 24 | } |
liam94 | 1:a1795335ef8c | 25 | } |
liam94 | 0:506531d0531c | 26 | |
liam94 | 1:a1795335ef8c | 27 | ultrasonic mu(PTD0, PTC12, .1, 1, &dist); //Set the trigger pin to PTD0 and the echo pin to PTC12 |
liam94 | 0:506531d0531c | 28 | //have updates every .1 seconds and a timeout after 1 |
liam94 | 0:506531d0531c | 29 | //second, and call dist when the distance changes |
liam94 | 0:506531d0531c | 30 | |
liam94 | 0:506531d0531c | 31 | int main() |
liam94 | 0:506531d0531c | 32 | { |
liam94 | 0:506531d0531c | 33 | mu.startUpdates();//start mesuring the distance |
liam94 | 0:506531d0531c | 34 | while(1) |
liam94 | 0:506531d0531c | 35 | { |
liam94 | 1:a1795335ef8c | 36 | //Do something else here |
liam94 | 0:506531d0531c | 37 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
liam94 | 0:506531d0531c | 38 | //the class checks if dist needs to be called. |
liam94 | 0:506531d0531c | 39 | } |
liam94 | 0:506531d0531c | 40 | } |