ELEC2645 (2015/16) / Mbed 2 deprecated el14ml_project

Dependencies:   N5110 SRF02-Mateusz mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * @file main.cpp
00003 * @Distance Sensor Project
00004 * @brief Main file containing functions and int main().
00005 * @author Mateusz Loboda
00006 * @date April 2016
00007 */
00008 
00009 #include "main.h"
00010 
00011 
00012 int main()
00013 
00014 {
00015     init_K64F();
00016     lcd.init();  ///initialize display
00017 
00018     initialScreen(); ///function for start up screen
00019     out.attach(timeout_isr,4);  ///intial screen displayed for 4 seconds
00020     lcd.refresh();
00021     sleep();
00022     lcd.clear();
00023     lcd.refresh();
00024     initialArray();
00025     ticker.attach(&ticker_isr,0.1);  /// interrupt, time triggered event
00026 
00027 
00028     while(1) {
00029         delay = rate;  ///value obtained from potentiometer to control the volume of buzzer
00030 
00031         buzzerPeriod = 1/((4200-(2*averageDistance))+200); ///frequency proportional to average distance, when distance = 200cm, frequency = 4kHz ie centre frequency
00032 
00033         if (g_ticker_flag ) {
00034 
00035             g_ticker_flag  = 0;  /// reset flag
00036             if ( g_button2_flag  == 0) {
00037 
00038                 buzzer = delay; /// duty cycle
00039                 buzzer.period(buzzerPeriod); //set pwm to my freq
00040                 get_averageDistance();
00041                 redLedIndicator();
00042                 drawDistanceBars();
00043 
00044 
00045             } else if (g_button2_flag  == 1) {  ///button pressed, set flag
00046 
00047                 moveArrayElements();
00048                 get_averageDistance();
00049                 adjacentArrayElements();
00050                 plotAxes();
00051                 plotDistancePoint();
00052 
00053                 lcd.refresh();
00054 
00055             }
00056             lcd.refresh();
00057             sleep();  /// reducing power consumption
00058         }
00059     }
00060 }
00061 void init_K64F()
00062 {
00063     /// on-board LEDs are active-low, so set pin high to turn them off.
00064     r_led = 1;
00065     g_led = 1;
00066     b_led = 1;
00067     button2.fall(&mode); ///under this condition call function where mode is a function that sets flag
00068     button2.mode(PullUp); ///enable internal pull up resistor
00069     button1.fall(&units);  ///under this condition call function where mode is a function that sets flag
00070     button1.mode(PullUp); ///enable internal pull up resistor
00071 
00072 }
00073 void units()
00074 {
00075     g_button1_flag  =!g_button1_flag  ;   /// set button 1 flag in ISR
00076 }
00077 void mode()
00078 {
00079     g_button2_flag  =!g_button2_flag  ;    ///set button 2 flag in ISR
00080 }
00081 void timeout_isr()
00082 {
00083 }
00084 void ticker_isr()
00085 {
00086     g_ticker_flag  = 1;   /// set ticker flag in ISR
00087 }
00088 void initialScreen() /// initial screen printed
00089 {
00090     lcd.printString("DISTANCE",18,0);
00091     lcd.printString("SENSOR",22,1);
00092     lcd.printString("Mateusz Loboda",0,3);
00093     lcd.printString("200843098",16,4);
00094     lcd.refresh();
00095 }
00096 void initialArray()
00097 {
00098     for (int i=0; i<83; i++) { /// ensures that points are not being plotted before the button is pressed
00099         graphArray[i] = 201; /// has to be more than 200 so points are only plotted from the point when user presses button
00100     }
00101     lcd.clear();
00102 }
00103 void get_averageDistance() /// to minimize error, 5 readings are taken at regular intervals and averaged out
00104 {
00105     for ( int i=0 ; i<5; i++) {
00106         int distance = sensor.getDistanceCm(); /// obtaining sensor distance reading
00107         if (distance < 400) { /// better averages random anonymous values not even considered
00108             averageDistance += distance; /// assignment by sum
00109         } else {
00110             i--;  ///if distance > 400 do not take that reading into account go again so range of sensor is 30-400cm
00111         }
00112     }
00113     averageDistance = averageDistance/5; //sum of 5 readings divided by 5 to get average
00114     lcd.clear();
00115 }
00116 
00117 void redLedIndicator() /// this function evaluates the average distance reading, displays it and controls the auditory + visual alerts
00118 {
00119     if (averageDistance<=30) {
00120         myled = 1;  /// warning led comes on
00121         lcd.clear(); /// clear previous distance values displayed
00122         lcd.printString(" ***COLLISION ",2,1); //warning text
00123         lcd.printString(" WARNING***   ",8,3);
00124         lcd.refresh();
00125         buzzer = delay; /// duty cycle, volume of buzzer controlled with pot
00126         buzzer.period(0.001); ///highest frequency pitch when warning message comes on
00127     } else {
00128         myled = 0;
00129     }
00130     if (averageDistance >30) {  /// above critical distance
00131         lcd.clear();
00132         char str[10]; ///keep the string as character array in local stack so can be manipulated
00133         if (g_button1_flag  == 0) {  ///button 1 flag is set 0 by default so distance displayed in units of cm
00134 
00135             sprintf(str,"%.2f",averageDistance); /// printing first 2 digits of average distance as float
00136             lcd.printString("cm",62,4); // printing units
00137 
00138         } else  if (g_button1_flag  == 1) {  ///button 1 pressed, flag set 1
00139             sprintf(str,"%.2f",averageDistance*0.393701); //converting from cm to inches
00140             lcd.printString("in",60,4); /// printing the units
00141 
00142         }
00143         lcd.printString("                                  ",0,3);
00144         lcd.printString(str,20,4);
00145         lcd.refresh();
00146     }
00147 }
00148 void drawDistanceBars() /// function draws bars so as to indicate the approximate distance to nearest target object
00149 {
00150     if(averageDistance>380) {
00151         lcd.drawRect(67,2,5,16,1); ///Draw 8 bars
00152 
00153     }
00154     if(averageDistance>330) {
00155         lcd.drawRect(59,2,5,16,1); ///Draw 7 bars
00156 
00157     }
00158     if(averageDistance>280) {
00159         lcd.drawRect(51,2,5,16,1); ///Draw 6 bars
00160 
00161     }
00162     if(averageDistance>230) {
00163         lcd.drawRect(43,2,5,16,1); ///Draw 5 bars
00164 
00165     }
00166     if(averageDistance>180) {
00167         lcd.drawRect(35,2,5,16,1); ///Draw 4 bars
00168 
00169     }
00170     if(averageDistance>130) {
00171         lcd.drawRect(27,2,5,16,1);  //Draw 3 bars
00172 
00173     }
00174     if(averageDistance>80) {
00175         lcd.drawRect(19,2,5,16,1); ///Draw 2 Bars
00176 
00177     }
00178     if(averageDistance>30) {
00179         lcd.drawRect(12,2,5,16,1); ///Draw one rectangle
00180     }
00181     lcd.refresh();
00182 }
00183 void moveArrayElements()
00184 {
00185 
00186     for (int i=81 ; i>0; i--) {
00187         graphArray[i] = graphArray[i-1];  ///moving each element of array to right
00188     }
00189 }
00190 void adjacentArrayElements() /// assigning float distance to first element of graph matrix
00191 {
00192 
00193     graphArray[0]= averageDistance; ///array element is average distance float
00194     if ((int)graphArray[0] != (int)graphArray[1]) { ///if value of current array element is different to previous one, red led and buzzer comes on ie visual and auditory alerts
00195         myled=1; ///red led comes on
00196         buzzer = delay; /// duty cycle
00197         buzzer.period(buzzerPeriod); ///set period of buzzer to my function
00198 
00199     } else { /// if no movement alerts are off
00200         myled=0;
00201         buzzer=0;
00202     }
00203 }
00204 void plotAxes() ///plotting both the x and y axis which are 2 pixels thick, i represents the pixels
00205 {
00206     ///i is pixel
00207     for ( int i=0; i<84; i++) { /// plotting x axis
00208         lcd.setPixel(i,46);
00209         lcd.setPixel(i,47);
00210     }
00211     ///plotting y axis
00212     for ( int i=0; i<48; i++) { /// plotting y axis
00213         lcd.setPixel(0,i);
00214         lcd.setPixel(1,i);
00215     }
00216 }
00217 void plotDistancePoint() ///function converts float distance to integer pixel
00218 {
00219 
00220     /// i in this loop is element of the array NOT pixel
00221     for (int i=0; i<82; i++) { /// array is 82 elements long as 2 pixels are used for axes
00222         ///x position + y position
00223         int p = (int)(45-(graphArray[i]/(200/46))); ///convert array element float distance to integer pixel
00224         lcd.setPixel(83-i,p); /// plot in pixel 84 at the right height , this pixel is then moved to right and new pixel is plotted
00225     }
00226     lcd.refresh();
00227 }
00228 
00229