an ultrasonic sensor used as a car sensor
Dependencies: N5110 PowerControl beep mbed sensor
Revision 2:9328053b2ba7, committed 2015-05-11
- Comitter:
- sjaffal3
- Date:
- Mon May 11 19:49:17 2015 +0000
- Parent:
- 1:475a5b7258fa
- Commit message:
- final code
Changed in this revision
diff -r 475a5b7258fa -r 9328053b2ba7 main.cpp --- a/main.cpp Fri May 08 15:41:29 2015 +0000 +++ b/main.cpp Mon May 11 19:49:17 2015 +0000 @@ -8,107 +8,132 @@ #include "main.h" + int main() -{ - - // the LCD display is initiated first when switch is on - +{ + + + PHY_PowerDown(); /// the Ethernet cable is powered down + lcd.init(); /// the LCD display is initialised when switch is on + welcomeMessage(); /// the welcome message will appear + button.rise(&buttonPressed); /// event generated on rising edge - PHY_PowerDown(); - lcd.init(); - welcomeMessage(); - pot.read(); - lcd.clear(); - button.rise(&buttonPressed); - while (1) { - - lcd.setBrightness(pot.read()); - - - if (i==0) - { - getDistanceCm(); - compDist(); - - - } - else if (i==1) - { - getDistanceIn(); - compDist(); - // Sleep(); - - - } - else if (i==2) - { - getDistanceUs(); - compDist(); - // Peripheral_PowerUp(0xFFFFFFFF); - } - else { - - getAcc(); - compDist(); + + mypotVal = mypot; ///sets a variable to change as the pot is changing + lcd.setBrightness(mypotVal); ///sets the brightness to the pot value + compDist(); ///compares the distance to set it in a specific range - } - } - } -void buttonPressed() +void buttonPressed() { - wait(0.2); + wait(0.2); - if (i>3) - { - i=0; - - + if (i>1) ///when button is pressed it will check if i >1 + { i=0; /// if it is it will set it to 0 } - else { - i++; + else + { i++; /// if not it will increment i by one } - } + +} - //Print welcome message (Change pixel coordinate later) + + + ///Print welcome message void welcomeMessage() { - lcd.printString("200911007", 0,1); - lcd.printString("Saleh Jaffal", 0,0); - lcd.refresh(); - getDistanceCm(); + + lcd.printString("University of ",5,0); + lcd.printString("Leeds",27,1); + + lcd.refresh(); + + lcd.drawRect(59,36,5,11,0);///bottom left rectangle is set + lcd.drawRect(72,36,5,11,0);///bottom right rectangle is set + lcd.drawRect(64,34,8,13,0);///bottom middle rectangle is set + + lcd.drawRect(60,33,16,3,0); /// is set over the bottom rectangles + + lcd.drawCircle(68,40,2,0); /// circle is set on bottom middle box + + lcd.drawRect(61,26,14,7,0);///big middle box is set over everything + + lcd.drawRect(63,27,10,0.5,1);///top black rectangle is set + + lcd.drawRect(64.5,28,1,0.5,1);///left middle black rect is set + lcd.drawRect(67.5,28,1,0.5,1);///middle black rect is set + lcd.drawRect(70,28,1,0.5,1);///right middle black rect is set + lcd.drawRect(64.5,28,0.5,3.5,1);///left bottom black rect is set + lcd.drawRect(67.5,28,0.5,3.5,1);///middle bottom black rect is set + lcd.drawRect(70,28,0.5,3.5,1);///right bottom black rect is set + + lcd.drawRect(62,21,12,5,0);///rectangle is set over big middle box + + lcd.drawRect(66,22,0.5,0.5,1);///left top black rect is set + lcd.drawRect(68,22,0.5,0.5,1);///top black rect is set + lcd.drawRect(70,22,0.5,0.5,1);///right top black rect is set + lcd.drawRect(66,22,0.5,2,1);///left bottom black rect is set + lcd.drawRect(68,22,0.5,2,1);///middle bottom black rect is set + lcd.drawRect(70,22,0.5,2,1);///right bottom black rect is set + + lcd.drawRect(64,17,8,4,0);///rectangle under triangle is set + + lcd.drawLine(64,17,68,13.5,1);///left line to complete triangle + + lcd.drawLine(72,17,68,13.5,1);///right line to complete triangle + + + + lcd.clearPixel(65, 36); ///clears intersecting lines from rectangle + lcd.clearPixel(66, 36); + lcd.clearPixel(67, 36); + lcd.clearPixel(68, 36); + lcd.clearPixel(69, 36); + lcd.clearPixel(70, 36); + lcd.clearPixel(71, 36); + + lcd.refresh(); + + + + wait(3.0); + lcd.clear(); + + + + lcd.printString("Saleh Jaffal",8,2); + lcd.printString("200911007",15,3); + + lcd.refresh(); + + lcd.drawRect(6,12,73,21,0); + lcd.drawRect(4,10,77,25,0); + + lcd.refresh(); + + + + getDistanceCm(); ///gets the distance after the message getDistanceIn(); getDistanceUs(); + + + } -void getAcc() -{ - getDistanceCm(); - getDistanceUs(); - - dist = sensor.getDistanceCm(); - tim = sensor.getDistanceUs(); - - - Vel2 = ((dist)*10000/tim); - Vel1 = ((distanceCm*10000)/distanceUs); - Acc = (Vel2-Vel1)/(tim-distanceUs); - - - } void getDistanceCm() { - distance1 = sensor.getDistanceCm(); - distance2 = sensor.getDistanceCm(); + distance1 = sensor.getDistanceCm(); ///adds up 10 distances read by the sensor + distance2 = sensor.getDistanceCm(); ///there is a 70 ms delay between each reading distance3 = sensor.getDistanceCm(); distance4 = sensor.getDistanceCm(); distance5 = sensor.getDistanceCm(); @@ -119,9 +144,9 @@ distance10 = sensor.getDistanceCm(); sum = distance1+distance2+distance3+distance4+distance5+distance6+distance7+distance8+distance9+distance10; - + ///all of them are added to the sum - distanceCm = sum/10; + distanceCm = sum/10; ///the sum is divided by 10 to get the average } @@ -132,8 +157,8 @@ void getDistanceIn() { - distance1 = sensor.getDistanceIn(); - distance2 = sensor.getDistanceIn(); + distance1 = sensor.getDistanceIn(); ///adds up 10 distances read by the sensor + distance2 = sensor.getDistanceIn(); ///there is a 70 ms delay between each reading distance3 = sensor.getDistanceIn(); distance4 = sensor.getDistanceIn(); distance5 = sensor.getDistanceIn(); @@ -145,16 +170,17 @@ sum = distance1+distance2+distance3+distance4+distance5+distance6+distance7+distance8+distance9+distance10; - - distanceIn = sum/10; + ///all of them are added to the sum + + distanceIn = sum/10; ///the sum is divided by 10 to get the average } void getDistanceUs() { - distance1 = sensor.getDistanceUs(); - distance2 = sensor.getDistanceUs(); + distance1 = sensor.getDistanceUs(); ///adds up 10 distances read by the sensor + distance2 = sensor.getDistanceUs(); ///there is a 70 ms delay between each reading distance3 = sensor.getDistanceUs(); distance4 = sensor.getDistanceUs(); distance5 = sensor.getDistanceUs(); @@ -166,240 +192,253 @@ sum = distance1+distance2+distance3+distance4+distance5+distance6+distance7+distance8+distance9+distance10; + ///all of them are added to the sum - distanceUs = sum/10; + distanceUs = sum/10; ///the sum is divided by 10 to get the average } +void compDist() ///used to compare the distances +{ + +switch (i) +{ + + case 0: + { + getDistanceCm(); + ///calls this action to show the reading + char buffer[14]; + /// each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + int length = sprintf(buffer," %d cm",distanceCm); + /// print formatted data to buffer + if(length<=14); + /// if string will fit on display + + lcd.clear(); /// clears LCD + lcd.drawRect(6,12,73,21,0);///sets 2 rectangles + lcd.drawRect(4,10,77,25,0); + lcd.printString("Distance =",8,2);///prints distance and the reading below it + lcd.printString(buffer,8,3); + + lcd.refresh(); ///refreshes LCD screen + + + + ///compares distances and sets them in their specified range + if (distanceCm >= 100) { + A=0; ///sets case + distanceA(); + } + else if (distanceCm >= 80 && distanceCm <= 99) { + A=1; ///sets case + distanceA(); + } + else if (distanceCm >= 60 && distanceCm <= 79) { + A=2; ///sets case + distanceA(); + } + else if (distanceCm >= 40 && distanceCm <= 59) { + A=3; ///sets case + distanceA(); + } + else if (distanceCm >= 20 && distanceCm <=39 ) { + A=4; ///sets case + distanceA(); + } + else { + A=5; ///sets case + distanceA(); + } + } + break; + + case (1): +{ + getDistanceIn(); + ///calls this action to show the reading + char buffer[14]; + /// each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + int length = sprintf(buffer," %d in",distanceIn); + /// print formatted data to buffer + if(length<=14); + /// if string will fit on display + + lcd.clear(); /// clears LCD + lcd.drawRect(6,12,73,21,0);/// sets 2 rectangles + lcd.drawRect(4,10,77,25,0); + lcd.printString("Distance =",8,2);/// prints distance and the reading below it + lcd.printString(buffer,8,3); + + lcd.refresh(); /// refreshes LCD screen + + ///compares distances and sets them in their specified range + if (distanceIn >= 40) { + A=0; ///sets case + distanceA(); + } + else if (distanceIn >= 32 && distanceIn <= 39) { + A=1; ///sets case + distanceA(); + } + else if (distanceIn >= 24 && distanceIn <= 31) { + A=2; ///sets case + distanceA(); + } + else if (distanceIn >= 16 && distanceIn <= 23) { + A=3; ///sets case + distanceA(); + } + else if (distanceIn >= 8 && distanceIn <=15 ) { + A=4; ///sets case + distanceA(); + } + else { + A=5; ///sets case + distanceA(); + } + } + break; + + case(2): + { + getDistanceUs(); + ///calls this action to show the reading + char buffer[14]; + /// each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + int length = sprintf(buffer," %d us",distanceUs); + /// print formatted data to buffer + if(length<=14); + /// if string will fit on display + + lcd.clear(); /// clears LCD + lcd.drawRect(6,12,73,21,0);/// sets 2 rectangles + lcd.drawRect(4,10,77,25,0); + lcd.printString("Distance =",8,2);/// prints distance and the reading below it + lcd.printString(buffer,8,3); + + lcd.refresh(); /// refreshes LCD screen + + + ///compares distances and sets them in their specified range -void distanceA() + if (distanceUs >= 5900) { + A=0; ///sets case + distanceA(); + } + else if (distanceUs >= 4680 && distanceUs <= 5899) { + A=1;///sets case + distanceA(); + } + else if (distanceUs >= 3400 && distanceUs <= 4679) { + A=2;///sets case + distanceA(); + } + else if (distanceUs >= 2430 && distanceUs <= 3339) { + A=3;///sets case + distanceA(); + } + else if (distanceUs >= 1220 && distanceUs <=2429 ) { + A=4;///sets case + distanceA(); + } + else { + A=5;///sets case + distanceA(); + } + + } + break; + + + + } + } + +void distanceA() //sets leds and buzzer for each case { + switch (A){ + + case(0): + { //case 0 would have 1 LED on green1=1; green2=0; yellow1=0; yellow2=0; red1=0; red2=0; - buzzer.beep(2000,0.1); -} + lcd.printString("Out of range",0,0); + buzzer.beep(2000, 0.1); ///@see This code was from a library I found + -void distanceB() -{ +} +break; + +case(1): +{ ///case 1 would have 2 LED's on green1=1; green2=1; yellow1=0; yellow2=0; red1=0; red2=0; - - - buzzer.beep(2000,0.15); + lcd.printString("In range",0,0); + buzzer.beep(2000, 0.1); ///@see This code was from a library I found } +break; -void distanceC() -{ green1=1; +case(2): +{ //green1=1; ///case 2 would have 3 LED's on green2=1; yellow1=1; yellow2=0; red1=0; red2=0; - - - buzzer.beep(2000,0.2); + lcd.printString("Take Care!",0,0); + buzzer.beep(2000, 0.2); } +break; -void distanceD() +case(3): { - green1=1; + green1=1; ///case 3 would have 4 LED's on green2=1; yellow1=1; yellow2=1; red1=0; red2=0; - buzzer.beep(2000,0.25); + lcd.printString("Closer!",0,0); + buzzer.beep(2000, 0.2); ///@see This code was from a library I found } +break; -void distanceE() +case(4): { - green1=1; + green1=1; ///case 4 would have 5 LED's on green2=1; yellow1=1; yellow2=1; red1=1; red2=0; - - - buzzer.beep(2000,0.8); + lcd.printString("VERY CLOSE!",0,0); + buzzer.beep(2000, 0.4); ///@see This code was from a library I found } +break; -void distanceF() +case(5): { - green1=1; + green1=1; ///case 5 would have 6 LED's on green2=1; yellow1=1; yellow2=1; red1=1; red2=1; - - - buzzer.beep(2000,200); + lcd.printString("WATCH OUT!",0,0); + buzzer.beep(2000, 1.2); ///@see This code was from a library I found } - -void compDist() -{ - - - - if (i==0) - { - - char buffer[14]; - int length = sprintf(buffer,"D= %d cm",distanceCm); - if(length<=14); - - lcd.clear(); - lcd.printString(buffer,0,0); - - - - wait(0.2); - - - if (distanceCm >= 100) { - - lcd.refresh(); - distanceA(); - } - else if (distanceCm >= 80 && distanceCm <= 99) { - - lcd.refresh(); - distanceB(); - } - else if (distanceCm >= 60 && distanceCm <= 79) { - - lcd.refresh(); - distanceC(); - } - else if (distanceCm >= 40 && distanceCm <= 59) { - - lcd.refresh(); - distanceD(); - } - else if (distanceCm >= 20 && distanceCm <=39 ) { - - lcd.refresh(); - distanceE(); - } - else { - - lcd.refresh(); - distanceF(); - } - - } - else if (i==1) -{ - char buffer[14]; - int length = sprintf(buffer,"D= %d in",distanceIn); - if(length<=14); - lcd.clear(); - lcd.printString(buffer,0,0); - wait(0.2); - - if (distanceIn >= 40) { - - lcd.refresh(); - distanceA(); - } - else if (distanceIn >= 32 && distanceIn <= 39) { - - lcd.refresh(); - distanceB(); - } - else if (distanceIn >= 24 && distanceIn <= 31) { - - lcd.refresh(); - distanceC(); - } - else if (distanceIn >= 16 && distanceIn <= 23) { - - lcd.refresh(); - distanceD(); - } - else if (distanceIn >= 8 && distanceIn <=15 ) { - - lcd.refresh(); - distanceE(); - } - else { - lcd.refresh(); - distanceF(); - } - } - - else if (i==2) - { - char buffer[14]; - int length = sprintf(buffer,"D= %d us",distanceUs); - if(length<=14); - lcd.clear(); - lcd.printString(buffer,0,0); - wait(0.2); - - if (distanceUs >= 5900) { - - lcd.refresh(); - distanceA(); - } - else if (distanceUs >= 4680 && distanceUs <= 5899) { - - lcd.refresh(); - distanceB(); - } - else if (distanceUs >= 3400 && distanceUs <= 4679) { - - lcd.refresh(); - distanceC(); - } - else if (distanceUs >= 2430 && distanceUs <= 3339) { - - lcd.refresh(); - distanceD(); - } - else if (distanceUs >= 1220 && distanceUs <=2429 ) { - - lcd.refresh(); - distanceE(); - } - else { - lcd.refresh(); - distanceF(); - } - - } - - - - - else { - - char buffer[14]; - int length = sprintf(buffer,"Acc= %d m/s",Vel2); - if(length<=14); - char buffer2[14]; - int length2 = sprintf(buffer2,"Acc= %d m/s",Vel1); - if(length2<=14); - lcd.clear(); - lcd.printString(buffer,0,0); - lcd.printString(buffer2,0,2); - - wait(0.2); - - } - - -} \ No newline at end of file +break; +}} \ No newline at end of file
diff -r 475a5b7258fa -r 9328053b2ba7 main.h --- a/main.h Fri May 08 15:41:29 2015 +0000 +++ b/main.h Mon May 11 19:49:17 2015 +0000 @@ -8,17 +8,15 @@ #ifndef MAIN_H #define MAIN_H - +//#include "beep.h" #include "mbed.h" #include "SRF02.h" #include "N5110.h" -#include "beep.h" +#include "beep.h" ///@see a library I found to initiate the buzzer #include "PowerControl/PowerControl.h" #include "PowerControl/EthernetPowerControl.h" #define USR_POWERDOWN (0x104) - - N5110 lcd(p7,p8,p9,p10,p11,p13,p26); SRF02 sensor(p28, p27); /** @@ -60,7 +58,7 @@ @namespace pot @brief GPIO input for lcd brightness */ -AnalogIn pot(p20); +AnalogIn mypot(p20); /** @namespace button @brief GPIO input to switch values from the sensor @@ -70,24 +68,56 @@ Serial pc(USBTX, USBRX); -void welcomeMessage(); -void getDistanceCm(); -void getDistanceIn(); -void getDistanceUs(); -void getAcc(); -void distanceA(); -void distanceB(); -void distanceC(); -void distanceD(); -void distanceE(); -void distanceF(); -void buttonPressed(); -void compDist(); -int distanceCm; -int distanceIn; -int distanceUs; +void beepOff(); + +void beepOn(float t); + +/** +Shows the welcome message when the LCD is on +*/ +void welcomeMessage(); +/** +gets the distance in cm by dividing +10 read values by 10 +@returns the average distance in cm +*/ +void getDistanceCm(); +/** +gets the distance in inches by dividing +10 read values by 10 +@returns the average distance in inches +*/ +void getDistanceIn(); +/** +gets the distance in us by dividing +10 read values by 10 +@returns the average distance in us +*/ +void getDistanceUs(); + +/** +when the button is pressed this +action is called +*/ +void buttonPressed(); +/** +looks for where the range of the reading is +to set its conditions +*/ +void compDist(); +/** +compares the cases of each distance +to set the Led's values and the buzzer +*/ +void distanceA(); + + +int distanceCm; /*!< distanceCm set in ISR */ +int distanceIn; /*!< distanceIn set in ISR */ +int distanceUs; /*!< distanceUs set in ISR */ +int A; /*!< used to compare the cases after being set in compDist() */ /** All distances used to add @@ -96,7 +126,6 @@ per second, so that it will cancel out the noise. */ - int distance1; /*!< distance1 set in ISR */ int distance2; /*!< distance2 set in ISR */ int distance3; /*!< distance3 set in ISR */ @@ -107,12 +136,10 @@ int distance8; /*!< distance8 set in ISR */ int distance9; /*!< distance9 set in ISR */ int distance10; /*!< distance10 set in ISR */ -int dist; -int tim; -int Vel1; -int Vel2; -int Acc; + +float mypotVal; /*!< mypotVal is used to transfer the vlaue of the pot + to lcd brightness */ int i=0; -int sum; - +int sum; /*!< used to add all the distances together (ie distance 1, + distance 2, ...*/ #endif \ No newline at end of file
diff -r 475a5b7258fa -r 9328053b2ba7 sensor.lib --- a/sensor.lib Fri May 08 15:41:29 2015 +0000 +++ b/sensor.lib Mon May 11 19:49:17 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/eencae/code/SRF02/#d571cee468e9 +http://developer.mbed.org/users/sjaffal3/code/sensor/#d571cee468e9