Coding for vehicle rear assistant

Dependencies:   beep mbed

Fork of Vehicle_Rear_Assistant by amin omar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.h Source File

main.h

Go to the documentation of this file.
00001 /**
00002 @file main.h
00003 @brief Header file containing functions prototypes, defines and global variables.
00004 @brief The software of Vehicle Rear Assistant.
00005 @brief ELEC 2645
00006 @author Aminuddin Omar
00007 @date   APRIL 2015
00008 */
00009 
00010 #ifndef MAIN_H
00011 #define MAIN_H
00012 
00013                                      //Declare libraries to be used 
00014 #include "mbed.h"
00015 #include "PowerControl/PowerControl.h"
00016 #include "PowerControl/EthernetPowerControl.h"
00017 #include "N5110.h"
00018 #include "SRF02.h"
00019 #include "beep.h"
00020 
00021 /**  
00022 @namespace lcd
00023 @brief GPIO pins for LCD display
00024 @namespace sensor
00025 @brief GPIO pins for SRF02
00026 @namespace buzzer
00027 @brief PWM pin for piezo buzzer
00028 @namespace danger
00029 @brief PWM pin for RED LED
00030 @namespace caution
00031 @brief PWM pin for YELLOW LED
00032 @namespace safe
00033 @brief PWM pin for GREEN LED
00034 @namespace lcdBrightness
00035 @brief PWM pin for LCD backlight
00036 @namespace potBrightness
00037 @brief AnalogIn pin for potentiometer
00038 @namespace button
00039 @brief InterruptIn pin for button
00040 */                                    
00041 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);                  //(LCD_VCC, sce, rst, dc, mosi, sck, backlight)
00042 SRF02 sensor(p28, p27);
00043 Beep buzzer(p21);                       
00044 PwmOut danger(p24);                                  //danger = red
00045 PwmOut caution(p23);                                 //caution = yellow
00046 PwmOut safe(p22);                                    //safe = green
00047 PwmOut lcdBrightness(p26);                           //lcd backlight (pin26)     
00048 AnalogIn potBrightness(p20);                         //potentiometer (pin20)
00049 InterruptIn button(p17);                             //push button to display value of distance
00050 
00051                                     //variables                            
00052 int distance ;             /*!< measure distance between car and object */
00053 int buttonFlag  = 0;       /*!< print flag set in ISR */
00054 int displayReading  = 0;   /*!< display the value of distance on LCD */
00055 
00056                                      //Initialise all the functions
00057                                      
00058 /**
00059 Print welcome message when switch is turned on
00060 @param a - set number of letters that can be written including space
00061 @param message - Print HELLO
00062 @param rear - Print REAR
00063 @param assistant - Print ASSISTANT
00064 @param name - Print A.OMAR
00065 @param number - Print 200906568
00066 */
00067 void welcomeText();
00068 
00069 /**
00070 Measure distance between car and objects
00071 @param distance - use SRF02 to measure distance
00072 */
00073 void getDistance();
00074 
00075 /**
00076 Print "CAR" and a block on LCD according to distance measured
00077 @param lcd.drawRect - draw rectangle to represent object 
00078 @param lcd.printString - draw "CAR" to represent car
00079 @param lcd.refresh() - refresh LCD display
00080 @param distanceSafe() - GREEN LED and buzzer is activated in safe mode
00081 @param distanceCaution() - YELLOW LED and buzzer is activated in caution mode
00082 @param distanceDanger() - RED LED and buzzer is activated in danger mode
00083 */
00084 void readDistance();
00085 
00086 /**
00087 This function is called when in safe mode
00088 @param safe - only GREEN LED light up
00089 @brief buzzer is called and beep according to distance
00090 */
00091 void distanceSafe();
00092 
00093 /**
00094 This function is called when in caution mode
00095 @param safe - only YELLOW LED light up
00096 @brief buzzer is called and beep according to distance
00097 */
00098 void distanceCaution();
00099 
00100 /**
00101 This function is called when in danger mode
00102 @param safe - only RED LED light up
00103 @brief buzzer is called and beep according to distance
00104 */
00105 void distanceDanger();
00106 
00107 /**
00108 This function is called when button is pressed
00109 @param buttonFlag - set the button flag to 1
00110 */
00111 void buttonPressed();
00112 
00113 
00114                                     //Print welcome message (letters are print one by one)
00115 void welcomeText()
00116 {     
00117     int a[9] = {12,18,24,30,36,42,48,54,60};
00118     const char message[6] = {'H','E','L','L','O','!'};
00119     char rear[4] = {'R','E','A','R'};
00120     char assistant[9] = {'A','S','S','I','S','T','A','N','T'};
00121     char name[6] = {'A','.','O','M','A','R'};
00122     char number[9] = {'2','0','0','9','0','6','5','6','8'};
00123     int i;
00124     for(i=0; i<6; i++) {
00125         lcd.printChar(message[i],a[i],0);
00126         wait(0.05);
00127     }
00128     for(i=0; i<4; i++) {
00129         lcd.printChar(rear[i],a[i],2);
00130         wait(0.05);
00131     }
00132     for(i=0; i<9; i++) {
00133         lcd.printChar(assistant[i],a[i],3);
00134         wait(0.05);
00135     }
00136     for(i=0; i<6; i++) {
00137         lcd.printChar(name[i],a[i],4);
00138         wait(0.05);
00139     }
00140     for(i=0; i<9; i++) {
00141         lcd.printChar(number[i],a[i],5);
00142         wait(0.05);
00143     }
00144     lcd.refresh();
00145     wait(2.5); 
00146 }
00147 
00148                                     //sensor is set to get the distance
00149 void getDistance()
00150 {
00151     distance  = sensor.getDistanceCm();
00152 }
00153 
00154                                     //sensor read distance & lcd display 'CAR' and 'rectangle' 
00155 void readDistance()
00156 {
00157         if (distance  >= 230) 
00158     {
00159         lcd.drawRect(76,0,7,48,1);  //WALL   lcd.drawRect(76(x),0(y),7(w),48(h),1(type))
00160         lcd.printString("CAR",5,3); //CAR
00161         lcd.refresh();
00162         distanceSafe();
00163     }
00164     else if (distance  >= 190 & distance  <= 229) 
00165     {
00166         lcd.drawRect(76,0,7,48,1);  //WALL
00167         lcd.printString("CAR",15,3);//CAR
00168         lcd.refresh();
00169         distanceSafe();
00170     }
00171     else if (distance  >= 150 & distance  <= 189) 
00172     {
00173         lcd.drawRect(76,0,7,48,1);  //WALL
00174         lcd.printString("CAR",25,3);//CAR
00175         lcd.refresh();
00176         distanceSafe();
00177     }
00178     else if (distance  >= 120 & distance  <= 149) 
00179     {
00180         lcd.drawRect(76,0,7,48,1);  //WALL
00181         lcd.printString("CAR",30,3);//CAR
00182         lcd.refresh();
00183         distanceCaution();
00184     }
00185     else if (distance  >= 100 & distance  <= 119) 
00186     {
00187         lcd.drawRect(76,0,7,48,1);  //WALL
00188         lcd.printString("CAR",39,3);//CAR
00189         lcd.refresh();
00190         distanceCaution();
00191     }
00192     else if (distance  >= 70 & distance  <= 99) 
00193     {
00194         lcd.drawRect(76,0,7,48,1);  //WALL
00195         lcd.printString("CAR",48,3);//CAR
00196         lcd.refresh();
00197         distanceCaution();
00198     }
00199     else if (distance  >=  45& distance  <= 69) 
00200     {
00201         lcd.drawRect(76,0,7,48,1);  //WALL
00202         lcd.printString("CAR",55,3);//CAR
00203         lcd.refresh();
00204         distanceDanger();
00205     }
00206     else if (distance  <= 44) 
00207     {
00208         lcd.printString("!!STOP!!",25,3);//print "STOP" only
00209         lcd.refresh();
00210         distanceDanger();
00211     }
00212 }
00213 
00214                                 //safe to reverse car
00215 void distanceSafe()         
00216 {
00217     danger = 0;             
00218     caution = 0;
00219     safe = 1;               //>>>>>>>>>safe = green led light up in safe mode
00220     buzzer.beep(1000,1);
00221     wait(1);
00222     buzzer.nobeep();
00223 }
00224 
00225                                 //caution : reverse car carefuly
00226 void distanceCaution()      
00227 {
00228     danger = 0;
00229     caution = 1;           //>>>>>>caution = yellow led light up in caution mode
00230     safe = 0;
00231     buzzer.beep(1000,1);
00232     wait(0.4);
00233     buzzer.nobeep();
00234 }
00235 
00236                                 //danger : stop reverse car
00237 void distanceDanger()       
00238 {
00239     danger = 1;             //>>>>>>>danger = red led light up in danger mode
00240     caution = 0;
00241     safe = 0;
00242     buzzer.beep(1000,1);
00243     wait(0.2);
00244     buzzer.nobeep();
00245 }
00246 
00247 
00248                                 //INTERRUPT SERVICE ROUTINE
00249 void buttonPressed() 
00250 {
00251     buttonFlag  = 1; //set flag
00252 }
00253 
00254 #endif