Embedded Systems Project Mateusz Loboda 200843098

Dependencies:   N5110 SRF02-Mateusz mbed

Committer:
el14ml
Date:
Thu May 05 09:54:30 2016 +0000
Revision:
3:ee005c9f0348
Parent:
2:0dfa60f22f07
final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14ml 0:3403a3415306 1 /*
el14ml 0:3403a3415306 2 * @file main.cpp
el14ml 0:3403a3415306 3 * @Distance Sensor Project
el14ml 0:3403a3415306 4 * @brief Main file containing functions and int main().
el14ml 0:3403a3415306 5 * @author Mateusz Loboda
el14ml 0:3403a3415306 6 * @date April 2016
el14ml 0:3403a3415306 7 */
el14ml 0:3403a3415306 8
el14ml 0:3403a3415306 9 #include "main.h"
el14ml 0:3403a3415306 10
el14ml 0:3403a3415306 11
el14ml 0:3403a3415306 12 int main()
el14ml 0:3403a3415306 13
el14ml 0:3403a3415306 14 {
el14ml 0:3403a3415306 15 init_K64F();
el14ml 3:ee005c9f0348 16 lcd.init(); ///initialize display
el14ml 0:3403a3415306 17
el14ml 3:ee005c9f0348 18 initialScreen(); ///function for start up screen
el14ml 3:ee005c9f0348 19 out.attach(timeout_isr,4); ///intial screen displayed for 4 seconds
el14ml 0:3403a3415306 20 lcd.refresh();
el14ml 0:3403a3415306 21 sleep();
el14ml 0:3403a3415306 22 lcd.clear();
el14ml 0:3403a3415306 23 lcd.refresh();
el14ml 0:3403a3415306 24 initialArray();
el14ml 3:ee005c9f0348 25 ticker.attach(&ticker_isr,0.1); /// interrupt, time triggered event
el14ml 0:3403a3415306 26
el14ml 0:3403a3415306 27
el14ml 0:3403a3415306 28 while(1) {
el14ml 3:ee005c9f0348 29 delay = rate; ///value obtained from potentiometer to control the volume of buzzer
el14ml 0:3403a3415306 30
el14ml 3:ee005c9f0348 31 buzzerPeriod = 1/((4200-(2*averageDistance))+200); ///frequency proportional to average distance, when distance = 200cm, frequency = 4kHz ie centre frequency
el14ml 0:3403a3415306 32
el14ml 0:3403a3415306 33 if (g_ticker_flag) {
el14ml 0:3403a3415306 34
el14ml 3:ee005c9f0348 35 g_ticker_flag = 0; /// reset flag
el14ml 0:3403a3415306 36 if ( g_button2_flag == 0) {
el14ml 0:3403a3415306 37
el14ml 3:ee005c9f0348 38 buzzer = delay; /// duty cycle
el14ml 0:3403a3415306 39 buzzer.period(buzzerPeriod); //set pwm to my freq
el14ml 0:3403a3415306 40 get_averageDistance();
el14ml 0:3403a3415306 41 redLedIndicator();
el14ml 0:3403a3415306 42 drawDistanceBars();
el14ml 3:ee005c9f0348 43
el14ml 3:ee005c9f0348 44
el14ml 3:ee005c9f0348 45 } else if (g_button2_flag == 1) { ///button pressed, set flag
el14ml 0:3403a3415306 46
el14ml 3:ee005c9f0348 47 moveArrayElements();
el14ml 3:ee005c9f0348 48 get_averageDistance();
el14ml 3:ee005c9f0348 49 adjacentArrayElements();
el14ml 3:ee005c9f0348 50 plotAxes();
el14ml 3:ee005c9f0348 51 plotDistancePoint();
el14ml 0:3403a3415306 52
el14ml 0:3403a3415306 53 lcd.refresh();
el14ml 0:3403a3415306 54
el14ml 0:3403a3415306 55 }
el14ml 0:3403a3415306 56 lcd.refresh();
el14ml 3:ee005c9f0348 57 sleep(); /// reducing power consumption
el14ml 0:3403a3415306 58 }
el14ml 0:3403a3415306 59 }
el14ml 0:3403a3415306 60 }
el14ml 0:3403a3415306 61 void init_K64F()
el14ml 0:3403a3415306 62 {
el14ml 3:ee005c9f0348 63 /// on-board LEDs are active-low, so set pin high to turn them off.
el14ml 3:ee005c9f0348 64 r_led = 1;
el14ml 0:3403a3415306 65 g_led = 1;
el14ml 0:3403a3415306 66 b_led = 1;
el14ml 3:ee005c9f0348 67 button2.fall(&mode); ///under this condition call function where mode is a function that sets flag
el14ml 3:ee005c9f0348 68 button2.mode(PullUp); ///enable internal pull up resistor
el14ml 3:ee005c9f0348 69 button1.fall(&units); ///under this condition call function where mode is a function that sets flag
el14ml 3:ee005c9f0348 70 button1.mode(PullUp); ///enable internal pull up resistor
el14ml 0:3403a3415306 71
el14ml 0:3403a3415306 72 }
el14ml 0:3403a3415306 73 void units()
el14ml 0:3403a3415306 74 {
el14ml 3:ee005c9f0348 75 g_button1_flag =!g_button1_flag ; /// set button 1 flag in ISR
el14ml 0:3403a3415306 76 }
el14ml 0:3403a3415306 77 void mode()
el14ml 0:3403a3415306 78 {
el14ml 3:ee005c9f0348 79 g_button2_flag =!g_button2_flag ; ///set button 2 flag in ISR
el14ml 0:3403a3415306 80 }
el14ml 0:3403a3415306 81 void timeout_isr()
el14ml 0:3403a3415306 82 {
el14ml 0:3403a3415306 83 }
el14ml 0:3403a3415306 84 void ticker_isr()
el14ml 0:3403a3415306 85 {
el14ml 3:ee005c9f0348 86 g_ticker_flag = 1; /// set ticker flag in ISR
el14ml 0:3403a3415306 87 }
el14ml 3:ee005c9f0348 88 void initialScreen() /// initial screen printed
el14ml 0:3403a3415306 89 {
el14ml 0:3403a3415306 90 lcd.printString("DISTANCE",18,0);
el14ml 0:3403a3415306 91 lcd.printString("SENSOR",22,1);
el14ml 0:3403a3415306 92 lcd.printString("Mateusz Loboda",0,3);
el14ml 0:3403a3415306 93 lcd.printString("200843098",16,4);
el14ml 0:3403a3415306 94 lcd.refresh();
el14ml 0:3403a3415306 95 }
el14ml 0:3403a3415306 96 void initialArray()
el14ml 0:3403a3415306 97 {
el14ml 3:ee005c9f0348 98 for (int i=0; i<83; i++) { /// ensures that points are not being plotted before the button is pressed
el14ml 3:ee005c9f0348 99 graphArray[i] = 201; /// has to be more than 200 so points are only plotted from the point when user presses button
el14ml 0:3403a3415306 100 }
el14ml 0:3403a3415306 101 lcd.clear();
el14ml 0:3403a3415306 102 }
el14ml 3:ee005c9f0348 103 void get_averageDistance() /// to minimize error, 5 readings are taken at regular intervals and averaged out
el14ml 0:3403a3415306 104 {
el14ml 0:3403a3415306 105 for ( int i=0 ; i<5; i++) {
el14ml 3:ee005c9f0348 106 int distance = sensor.getDistanceCm(); /// obtaining sensor distance reading
el14ml 3:ee005c9f0348 107 if (distance < 400) { /// better averages random anonymous values not even considered
el14ml 3:ee005c9f0348 108 averageDistance += distance; /// assignment by sum
el14ml 0:3403a3415306 109 } else {
el14ml 3:ee005c9f0348 110 i--; ///if distance > 400 do not take that reading into account go again so range of sensor is 30-400cm
el14ml 0:3403a3415306 111 }
el14ml 0:3403a3415306 112 }
el14ml 2:0dfa60f22f07 113 averageDistance = averageDistance/5; //sum of 5 readings divided by 5 to get average
el14ml 3:ee005c9f0348 114 lcd.clear();
el14ml 0:3403a3415306 115 }
el14ml 0:3403a3415306 116
el14ml 3:ee005c9f0348 117 void redLedIndicator() /// this function evaluates the average distance reading, displays it and controls the auditory + visual alerts
el14ml 0:3403a3415306 118 {
el14ml 0:3403a3415306 119 if (averageDistance<=30) {
el14ml 3:ee005c9f0348 120 myled = 1; /// warning led comes on
el14ml 3:ee005c9f0348 121 lcd.clear(); /// clear previous distance values displayed
el14ml 2:0dfa60f22f07 122 lcd.printString(" ***COLLISION ",2,1); //warning text
el14ml 0:3403a3415306 123 lcd.printString(" WARNING*** ",8,3);
el14ml 0:3403a3415306 124 lcd.refresh();
el14ml 3:ee005c9f0348 125 buzzer = delay; /// duty cycle, volume of buzzer controlled with pot
el14ml 3:ee005c9f0348 126 buzzer.period(0.001); ///highest frequency pitch when warning message comes on
el14ml 0:3403a3415306 127 } else {
el14ml 0:3403a3415306 128 myled = 0;
el14ml 0:3403a3415306 129 }
el14ml 3:ee005c9f0348 130 if (averageDistance >30) { /// above critical distance
el14ml 0:3403a3415306 131 lcd.clear();
el14ml 3:ee005c9f0348 132 char str[10]; ///keep the string as character array in local stack so can be manipulated
el14ml 3:ee005c9f0348 133 if (g_button1_flag == 0) { ///button 1 flag is set 0 by default so distance displayed in units of cm
el14ml 0:3403a3415306 134
el14ml 3:ee005c9f0348 135 sprintf(str,"%.2f",averageDistance); /// printing first 2 digits of average distance as float
el14ml 3:ee005c9f0348 136 lcd.printString("cm",62,4); // printing units
el14ml 0:3403a3415306 137
el14ml 3:ee005c9f0348 138 } else if (g_button1_flag == 1) { ///button 1 pressed, flag set 1
el14ml 2:0dfa60f22f07 139 sprintf(str,"%.2f",averageDistance*0.393701); //converting from cm to inches
el14ml 3:ee005c9f0348 140 lcd.printString("in",60,4); /// printing the units
el14ml 0:3403a3415306 141
el14ml 0:3403a3415306 142 }
el14ml 0:3403a3415306 143 lcd.printString(" ",0,3);
el14ml 0:3403a3415306 144 lcd.printString(str,20,4);
el14ml 0:3403a3415306 145 lcd.refresh();
el14ml 0:3403a3415306 146 }
el14ml 0:3403a3415306 147 }
el14ml 3:ee005c9f0348 148 void drawDistanceBars() /// function draws bars so as to indicate the approximate distance to nearest target object
el14ml 0:3403a3415306 149 {
el14ml 0:3403a3415306 150 if(averageDistance>380) {
el14ml 3:ee005c9f0348 151 lcd.drawRect(67,2,5,16,1); ///Draw 8 bars
el14ml 0:3403a3415306 152
el14ml 0:3403a3415306 153 }
el14ml 0:3403a3415306 154 if(averageDistance>330) {
el14ml 3:ee005c9f0348 155 lcd.drawRect(59,2,5,16,1); ///Draw 7 bars
el14ml 0:3403a3415306 156
el14ml 0:3403a3415306 157 }
el14ml 0:3403a3415306 158 if(averageDistance>280) {
el14ml 3:ee005c9f0348 159 lcd.drawRect(51,2,5,16,1); ///Draw 6 bars
el14ml 0:3403a3415306 160
el14ml 0:3403a3415306 161 }
el14ml 0:3403a3415306 162 if(averageDistance>230) {
el14ml 3:ee005c9f0348 163 lcd.drawRect(43,2,5,16,1); ///Draw 5 bars
el14ml 0:3403a3415306 164
el14ml 0:3403a3415306 165 }
el14ml 0:3403a3415306 166 if(averageDistance>180) {
el14ml 3:ee005c9f0348 167 lcd.drawRect(35,2,5,16,1); ///Draw 4 bars
el14ml 0:3403a3415306 168
el14ml 0:3403a3415306 169 }
el14ml 0:3403a3415306 170 if(averageDistance>130) {
el14ml 2:0dfa60f22f07 171 lcd.drawRect(27,2,5,16,1); //Draw 3 bars
el14ml 0:3403a3415306 172
el14ml 0:3403a3415306 173 }
el14ml 0:3403a3415306 174 if(averageDistance>80) {
el14ml 3:ee005c9f0348 175 lcd.drawRect(19,2,5,16,1); ///Draw 2 Bars
el14ml 0:3403a3415306 176
el14ml 0:3403a3415306 177 }
el14ml 0:3403a3415306 178 if(averageDistance>30) {
el14ml 3:ee005c9f0348 179 lcd.drawRect(12,2,5,16,1); ///Draw one rectangle
el14ml 0:3403a3415306 180 }
el14ml 0:3403a3415306 181 lcd.refresh();
el14ml 0:3403a3415306 182 }
el14ml 0:3403a3415306 183 void moveArrayElements()
el14ml 0:3403a3415306 184 {
el14ml 0:3403a3415306 185
el14ml 0:3403a3415306 186 for (int i=81 ; i>0; i--) {
el14ml 3:ee005c9f0348 187 graphArray[i] = graphArray[i-1]; ///moving each element of array to right
el14ml 3:ee005c9f0348 188 }
el14ml 0:3403a3415306 189 }
el14ml 3:ee005c9f0348 190 void adjacentArrayElements() /// assigning float distance to first element of graph matrix
el14ml 0:3403a3415306 191 {
el14ml 0:3403a3415306 192
el14ml 3:ee005c9f0348 193 graphArray[0]= averageDistance; ///array element is average distance float
el14ml 3:ee005c9f0348 194 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
el14ml 3:ee005c9f0348 195 myled=1; ///red led comes on
el14ml 3:ee005c9f0348 196 buzzer = delay; /// duty cycle
el14ml 3:ee005c9f0348 197 buzzer.period(buzzerPeriod); ///set period of buzzer to my function
el14ml 0:3403a3415306 198
el14ml 3:ee005c9f0348 199 } else { /// if no movement alerts are off
el14ml 0:3403a3415306 200 myled=0;
el14ml 0:3403a3415306 201 buzzer=0;
el14ml 0:3403a3415306 202 }
el14ml 0:3403a3415306 203 }
el14ml 3:ee005c9f0348 204 void plotAxes() ///plotting both the x and y axis which are 2 pixels thick, i represents the pixels
el14ml 0:3403a3415306 205 {
el14ml 3:ee005c9f0348 206 ///i is pixel
el14ml 3:ee005c9f0348 207 for ( int i=0; i<84; i++) { /// plotting x axis
el14ml 0:3403a3415306 208 lcd.setPixel(i,46);
el14ml 0:3403a3415306 209 lcd.setPixel(i,47);
el14ml 0:3403a3415306 210 }
el14ml 3:ee005c9f0348 211 ///plotting y axis
el14ml 3:ee005c9f0348 212 for ( int i=0; i<48; i++) { /// plotting y axis
el14ml 0:3403a3415306 213 lcd.setPixel(0,i);
el14ml 0:3403a3415306 214 lcd.setPixel(1,i);
el14ml 0:3403a3415306 215 }
el14ml 0:3403a3415306 216 }
el14ml 3:ee005c9f0348 217 void plotDistancePoint() ///function converts float distance to integer pixel
el14ml 0:3403a3415306 218 {
el14ml 0:3403a3415306 219
el14ml 3:ee005c9f0348 220 /// i in this loop is element of the array NOT pixel
el14ml 3:ee005c9f0348 221 for (int i=0; i<82; i++) { /// array is 82 elements long as 2 pixels are used for axes
el14ml 3:ee005c9f0348 222 ///x position + y position
el14ml 3:ee005c9f0348 223 int p = (int)(45-(graphArray[i]/(200/46))); ///convert array element float distance to integer pixel
el14ml 3:ee005c9f0348 224 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
el14ml 0:3403a3415306 225 }
el14ml 0:3403a3415306 226 lcd.refresh();
el14ml 0:3403a3415306 227 }
el14ml 0:3403a3415306 228
el14ml 2:0dfa60f22f07 229