get distance from ultrasonic sensor and print it with circle_UI on Nokia LCD
Dependencies: RangeFinder mbed
main.cpp@1:269a3d942995, 2012-10-18 (annotated)
- Committer:
- ryought
- Date:
- Thu Oct 18 15:46:15 2012 +0000
- Revision:
- 1:269a3d942995
- Parent:
- 0:8dccbeedd000
Second
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryought | 0:8dccbeedd000 | 1 | #include "mbed.h" |
ryought | 0:8dccbeedd000 | 2 | #include "NokiaLCD.h" |
ryought | 0:8dccbeedd000 | 3 | #include "RangeFinder.h" |
ryought | 0:8dccbeedd000 | 4 | |
ryought | 0:8dccbeedd000 | 5 | NokiaLCD lcd(p5, p7, p8, p11, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
ryought | 0:8dccbeedd000 | 6 | RangeFinder rf(p21, 10, 5800.0, 100000); |
ryought | 0:8dccbeedd000 | 7 | Serial xbee(p9, p10); |
ryought | 0:8dccbeedd000 | 8 | #define PI 3.141592653589793 |
ryought | 0:8dccbeedd000 | 9 | |
ryought | 0:8dccbeedd000 | 10 | |
ryought | 0:8dccbeedd000 | 11 | int main() { |
ryought | 0:8dccbeedd000 | 12 | char a = 0; |
ryought | 0:8dccbeedd000 | 13 | float d; //0~0.5 |
ryought | 0:8dccbeedd000 | 14 | |
ryought | 0:8dccbeedd000 | 15 | long double x = 0; |
ryought | 0:8dccbeedd000 | 16 | long double y = 0; |
ryought | 0:8dccbeedd000 | 17 | long double circle_deg; |
ryought | 0:8dccbeedd000 | 18 | long double circle_rad; |
ryought | 0:8dccbeedd000 | 19 | lcd.background(0x000000); |
ryought | 0:8dccbeedd000 | 20 | lcd.cls(); |
ryought | 0:8dccbeedd000 | 21 | /* |
ryought | 0:8dccbeedd000 | 22 | lcd.locate(0,0); |
ryought | 0:8dccbeedd000 | 23 | lcd.printf("Xbee RX OUT"); |
ryought | 0:8dccbeedd000 | 24 | lcd.locate(0,2); |
ryought | 0:8dccbeedd000 | 25 | lcd.printf(">"); |
ryought | 0:8dccbeedd000 | 26 | */ |
ryought | 0:8dccbeedd000 | 27 | while(true){ |
ryought | 0:8dccbeedd000 | 28 | /* |
ryought | 0:8dccbeedd000 | 29 | if(xbee.readable()){ |
ryought | 0:8dccbeedd000 | 30 | a = xbee.getc(); //XBee read |
ryought | 0:8dccbeedd000 | 31 | lcd.printf("%c", a); |
ryought | 0:8dccbeedd000 | 32 | } |
ryought | 0:8dccbeedd000 | 33 | */ |
ryought | 0:8dccbeedd000 | 34 | d = rf.read_m(); |
ryought | 0:8dccbeedd000 | 35 | if (d == -1.0) { |
ryought | 0:8dccbeedd000 | 36 | //pc.printf("Timeout Error.\n"); |
ryought | 0:8dccbeedd000 | 37 | } else if (d > 5.0) { |
ryought | 0:8dccbeedd000 | 38 | // Seeed's sensor has a maximum range of 4m, it returns |
ryought | 0:8dccbeedd000 | 39 | // something like 7m if the ultrasound pulse isn't reflected. |
ryought | 0:8dccbeedd000 | 40 | //pc.printf("No object within detection range.\n"); |
ryought | 0:8dccbeedd000 | 41 | } else { |
ryought | 0:8dccbeedd000 | 42 | /* |
ryought | 0:8dccbeedd000 | 43 | pc.printf("Distance = %f cm.\n", d*100); |
ryought | 0:8dccbeedd000 | 44 | xbee.printf("%f\n", d*100); |
ryought | 0:8dccbeedd000 | 45 | lcd.locate(0,0); |
ryought | 0:8dccbeedd000 | 46 | lcd.printf("Xbee RX OUT"); |
ryought | 0:8dccbeedd000 | 47 | lcd.locate(0,5); |
ryought | 0:8dccbeedd000 | 48 | lcd.printf("%f cm", d*100); |
ryought | 0:8dccbeedd000 | 49 | wait(0.5); |
ryought | 0:8dccbeedd000 | 50 | */ |
ryought | 1:269a3d942995 | 51 | for(int width=55; width < 61; width = width + 2 |
ryought | 1:269a3d942995 | 52 | for(circle_deg=0; circle_deg <= d*720; circle_deg++) { |
ryought | 1:269a3d942995 | 53 | circle_rad = circle_deg * PI / 180; |
ryought | 1:269a3d942995 | 54 | x = 65 + cos(circle_rad)*width; |
ryought | 1:269a3d942995 | 55 | y = 65 - sin(circle_rad)*width; |
ryought | 1:269a3d942995 | 56 | lcd.pixel(x, y, 0x00FFFF); |
ryought | 1:269a3d942995 | 57 | } |
ryought | 0:8dccbeedd000 | 58 | } |
ryought | 0:8dccbeedd000 | 59 | lcd.locate(2, 7); |
ryought | 0:8dccbeedd000 | 60 | lcd.printf("%f m", d); |
ryought | 0:8dccbeedd000 | 61 | wait(0.5); |
ryought | 0:8dccbeedd000 | 62 | lcd.cls(); |
ryought | 0:8dccbeedd000 | 63 | |
ryought | 0:8dccbeedd000 | 64 | } |
ryought | 0:8dccbeedd000 | 65 | } |
ryought | 0:8dccbeedd000 | 66 | } |