
Ultrasonic sensor to measure the bag in cellophane film
Dependencies: PCF2119_16X2_LCD SRF02 mbed
main.cpp
00001 #include "mbed.h" 00002 #include "SRF02.h" 00003 #include "PC2119_16X2_LCD.h" 00004 00005 // ultrasonc sensor has a resolution of 1 cm at best so can only be used for rough positional measurements 00006 00007 // Batron LCD - this code is a bit rough and ready and doesn't cope with the second line of the dsiplay very well 00008 00009 // Adresse I2C standard des afficheurs LCD 00010 #define LCDADDR 0x76 00011 00012 DigitalOut led1(LED1); 00013 DigitalOut led2(LED2); 00014 DigitalOut led3(LED3); 00015 DigitalOut led4(LED4); 00016 DigitalIn multiSensor(p20); 00017 //I2C i2c(p9, p10); 00018 //DigitalOut resetLCD(p11); 00019 //I2C ultraS(p28,p27); 00020 Serial pc(USBTX, USBRX); // tx, rx 00021 SRF02 srf02c(p28, p27, 0xE0, 0x52); 00022 SRF02 srf02l(p28, p27, 0xE2, 0x52); 00023 SRF02 srf02r(p28, p27, 0xE4, 0x52); 00024 AnalogOut anOut(p18); 00025 //PC2119_16X2_LCD lcd(p9, p10, p11); 00026 00027 float filteredDistance = 1000.0; 00028 float range = 4000.0; 00029 00030 // procedure declarations 00031 00032 00033 00034 00035 int main() 00036 { 00037 // char str[10]; 00038 // char msb = 0; 00039 // char lsb = 0; 00040 float distanceC; 00041 float distanceL = range; 00042 float distanceR = range; 00043 bool validReading; 00044 00045 // set i2c frequency to 100KHz 00046 //i2c.frequency(100000); 00047 00048 //pc.printf("clear lcd display\n"); 00049 multiSensor.mode(PullUp); 00050 00051 // do forever 00052 while (1) 00053 { 00054 validReading = false; 00055 00056 distanceC = srf02c.read(); 00057 00058 if (multiSensor == true) 00059 { 00060 distanceL = srf02l.read(); 00061 distanceR = srf02r.read(); 00062 } 00063 00064 if (distanceC < range) 00065 { 00066 led1 = 1; 00067 validReading = true; 00068 } 00069 else 00070 { 00071 led1 = 0; 00072 } 00073 00074 if (multiSensor == true){ 00075 if (distanceL < range) 00076 { 00077 led2 = 1; 00078 validReading = true; 00079 } 00080 else 00081 { 00082 led2 = 0; 00083 } 00084 00085 if (distanceR < range) 00086 { 00087 led3 = 1; 00088 validReading = true; 00089 } 00090 else 00091 { 00092 led3 = 0; 00093 } 00094 } 00095 00096 //pc.printf("Distances %5.3f, %5.3f, %5.3f\n", distanceC, distanceL, distanceR); 00097 00098 if (validReading) 00099 { 00100 float shortestDistance = 0.0; 00101 00102 if (multiSensor == true){ 00103 if (distanceC < distanceL && distanceC < distanceR) 00104 shortestDistance = distanceC; 00105 else 00106 if (distanceL < distanceR) 00107 shortestDistance = distanceL; 00108 else 00109 shortestDistance = distanceR; 00110 } 00111 else 00112 shortestDistance = distanceC; 00113 00114 filteredDistance = (0.7 * filteredDistance) + (0.3 * shortestDistance); 00115 00116 pc.printf("Average distance %5.3f\n", filteredDistance); 00117 00118 anOut = filteredDistance / range; 00119 } 00120 else 00121 { 00122 pc.printf("Out of range\n"); 00123 00124 anOut = 0.0; 00125 } 00126 00127 // wait a while then loop again - was 20 ms 00128 //wait_ms(100); 00129 } 00130 } 00131
Generated on Mon Jul 18 2022 01:13:40 by
