Embedded Systems Project Mateusz Loboda 200843098

Dependencies:   N5110 SRF02-Mateusz mbed

Committer:
el14ml
Date:
Wed May 04 22:45:02 2016 +0000
Revision:
2:0dfa60f22f07
Parent:
1:32b9ad362749
Child:
3:ee005c9f0348
header file doxygen

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