get distance from ultrasonic sensor and print it with circle_UI on Nokia LCD

Dependencies:   RangeFinder mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "NokiaLCD.h"
00003 #include "RangeFinder.h"
00004 
00005 NokiaLCD lcd(p5, p7, p8, p11, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
00006 RangeFinder rf(p21, 10, 5800.0, 100000);
00007 Serial xbee(p9, p10);
00008 #define PI 3.141592653589793
00009 
00010 
00011 int main() {
00012     char a = 0;
00013     float d; //0~0.5
00014     
00015     long double x = 0;
00016     long double y = 0;
00017     long double circle_deg;
00018     long double circle_rad;
00019     lcd.background(0x000000);
00020     lcd.cls();
00021     /*
00022     lcd.locate(0,0);
00023     lcd.printf("Xbee RX OUT");
00024     lcd.locate(0,2);
00025     lcd.printf(">");
00026     */
00027     while(true){
00028         /*
00029         if(xbee.readable()){
00030             a = xbee.getc(); //XBee read
00031             lcd.printf("%c", a);
00032         }
00033         */
00034         d = rf.read_m();
00035         if (d == -1.0)  {
00036             //pc.printf("Timeout Error.\n");   
00037         } else if (d > 5.0) {  
00038             // Seeed's sensor has a maximum range of 4m, it returns
00039             // something like 7m if the ultrasound pulse isn't reflected. 
00040             //pc.printf("No object within detection range.\n");
00041         } else  {
00042             /*
00043             pc.printf("Distance = %f cm.\n", d*100);
00044             xbee.printf("%f\n", d*100);
00045             lcd.locate(0,0);
00046             lcd.printf("Xbee RX OUT");
00047             lcd.locate(0,5);
00048             lcd.printf("%f cm", d*100);
00049             wait(0.5);
00050             */
00051             for(int width=55; width < 61; width = width + 2
00052                 for(circle_deg=0; circle_deg <= d*720; circle_deg++) {
00053                     circle_rad = circle_deg * PI / 180;
00054                     x = 65 + cos(circle_rad)*width;
00055                     y = 65 - sin(circle_rad)*width;
00056                     lcd.pixel(x, y, 0x00FFFF);
00057                 }
00058             }
00059             lcd.locate(2, 7);
00060             lcd.printf("%f m", d);
00061             wait(0.5);
00062             lcd.cls();
00063             
00064         }
00065     }
00066 }