Distance srnsor final code

Dependencies:   DocTest N5110 PowerControl SRF02 beep mbed

Committer:
fy12dsak
Date:
Mon May 11 22:50:09 2015 +0000
Revision:
0:abe1c17c40e1
final code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fy12dsak 0:abe1c17c40e1 1 /**
fy12dsak 0:abe1c17c40e1 2 @file main.cpp
fy12dsak 0:abe1c17c40e1 3 @brief car parking senser
fy12dsak 0:abe1c17c40e1 4 @brief Shows examples of creating Doxygen documentation.
fy12dsak 0:abe1c17c40e1 5 @brief Revision 1.0.
fy12dsak 0:abe1c17c40e1 6 @author Dawood Al kalbani
fy12dsak 0:abe1c17c40e1 7 @date 5 May 2015
fy12dsak 0:abe1c17c40e1 8 */
fy12dsak 0:abe1c17c40e1 9
fy12dsak 0:abe1c17c40e1 10 #include "mbed.h"
fy12dsak 0:abe1c17c40e1 11 #include "N5110.h"
fy12dsak 0:abe1c17c40e1 12 #include "SRF02.h"
fy12dsak 0:abe1c17c40e1 13 #include "PowerControl/PowerControl.h"
fy12dsak 0:abe1c17c40e1 14 #include "PowerControl/EthernetPowerControl.h"
fy12dsak 0:abe1c17c40e1 15 #include "beep.h"
fy12dsak 0:abe1c17c40e1 16 #define USR_POWERDOWN (0x104)
fy12dsak 0:abe1c17c40e1 17
fy12dsak 0:abe1c17c40e1 18 int semihost_powerdown() ///Function
fy12dsak 0:abe1c17c40e1 19 {
fy12dsak 0:abe1c17c40e1 20
fy12dsak 0:abe1c17c40e1 21 uint32_t arg;
fy12dsak 0:abe1c17c40e1 22 return __semihost (USR_POWERDOWN, &arg);
fy12dsak 0:abe1c17c40e1 23 }
fy12dsak 0:abe1c17c40e1 24
fy12dsak 0:abe1c17c40e1 25
fy12dsak 0:abe1c17c40e1 26
fy12dsak 0:abe1c17c40e1 27 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
fy12dsak 0:abe1c17c40e1 28 /// N5110 LCD pins
fy12dsak 0:abe1c17c40e1 29 N5110 screen(p7,p8,p9,p10,p11,p13,p26);
fy12dsak 0:abe1c17c40e1 30 // Can also power (VCC) directly from VOUT (3.3 V) -
fy12dsak 0:abe1c17c40e1 31 // Can give better performance due to current limitation from GPIO pin
fy12dsak 0:abe1c17c40e1 32 /// SRF02 output pins
fy12dsak 0:abe1c17c40e1 33 SRF02 sensor(p28,p27);
fy12dsak 0:abe1c17c40e1 34 //PwmOut buzz(p21);
fy12dsak 0:abe1c17c40e1 35 BusOut LED(p24);
fy12dsak 0:abe1c17c40e1 36 /// AnalogIn poten output pin
fy12dsak 0:abe1c17c40e1 37 AnalogIn Poten(p20);
fy12dsak 0:abe1c17c40e1 38 Beep buzzer(p21);
fy12dsak 0:abe1c17c40e1 39
fy12dsak 0:abe1c17c40e1 40 Ticker buzzerTimer; ///Ticker
fy12dsak 0:abe1c17c40e1 41 int buzzerCounterMax; ///buzzer
fy12dsak 0:abe1c17c40e1 42
fy12dsak 0:abe1c17c40e1 43 /// this is the buzzer toggle
fy12dsak 0:abe1c17c40e1 44 /// buzzerCounter - static int used to ...
fy12dsak 0:abe1c17c40e1 45 void buzzerToggle(){
fy12dsak 0:abe1c17c40e1 46 static int buzzerCounter=0;
fy12dsak 0:abe1c17c40e1 47 buzzerCounter = (buzzerCounter>=buzzerCounterMax)?0:buzzerCounter+1;
fy12dsak 0:abe1c17c40e1 48 if ((buzzerCounter==0)&&(buzzerCounterMax!=0)){
fy12dsak 0:abe1c17c40e1 49 buzzer.beep(1000,0.1);
fy12dsak 0:abe1c17c40e1 50
fy12dsak 0:abe1c17c40e1 51 }
fy12dsak 0:abe1c17c40e1 52 }
fy12dsak 0:abe1c17c40e1 53
fy12dsak 0:abe1c17c40e1 54 InterruptIn startReading (p15); ///InterruptIn
fy12dsak 0:abe1c17c40e1 55
fy12dsak 0:abe1c17c40e1 56 int startFlag = 0; ///flag
fy12dsak 0:abe1c17c40e1 57 void startRead();
fy12dsak 0:abe1c17c40e1 58 /// this is the startRead
fy12dsak 0:abe1c17c40e1 59 void startRead()
fy12dsak 0:abe1c17c40e1 60 {
fy12dsak 0:abe1c17c40e1 61 startFlag = 1;
fy12dsak 0:abe1c17c40e1 62 }
fy12dsak 0:abe1c17c40e1 63
fy12dsak 0:abe1c17c40e1 64 /// this is the main
fy12dsak 0:abe1c17c40e1 65 int main()
fy12dsak 0:abe1c17c40e1 66 {
fy12dsak 0:abe1c17c40e1 67
fy12dsak 0:abe1c17c40e1 68 int result = semihost_powerdown(); ///
fy12dsak 0:abe1c17c40e1 69 buzzerTimer.attach(&buzzerToggle,0.01);
fy12dsak 0:abe1c17c40e1 70 // first need to initialise display
fy12dsak 0:abe1c17c40e1 71 screen.init();
fy12dsak 0:abe1c17c40e1 72 // buzzer = 0.0;
fy12dsak 0:abe1c17c40e1 73 screen.printString("Leeds Uni",15,0);
fy12dsak 0:abe1c17c40e1 74 wait(1);
fy12dsak 0:abe1c17c40e1 75 screen.printString("Dawood",20,1);
fy12dsak 0:abe1c17c40e1 76 wait(1);
fy12dsak 0:abe1c17c40e1 77 screen.printString("Al Kalbani",15,2);
fy12dsak 0:abe1c17c40e1 78 wait(1);
fy12dsak 0:abe1c17c40e1 79 screen.printString("200744793",20,3);
fy12dsak 0:abe1c17c40e1 80 wait(1);
fy12dsak 0:abe1c17c40e1 81 screen.refresh();
fy12dsak 0:abe1c17c40e1 82 screen.clear();
fy12dsak 0:abe1c17c40e1 83 //button function
fy12dsak 0:abe1c17c40e1 84
fy12dsak 0:abe1c17c40e1 85
fy12dsak 0:abe1c17c40e1 86 startReading.rise(&startRead);
fy12dsak 0:abe1c17c40e1 87
fy12dsak 0:abe1c17c40e1 88 while(1)
fy12dsak 0:abe1c17c40e1 89 {
fy12dsak 0:abe1c17c40e1 90 if (startFlag)
fy12dsak 0:abe1c17c40e1 91
fy12dsak 0:abe1c17c40e1 92 {
fy12dsak 0:abe1c17c40e1 93 //startFlag = 0;
fy12dsak 0:abe1c17c40e1 94
fy12dsak 0:abe1c17c40e1 95 //read sensor distance in cm and print over serial port
fy12dsak 0:abe1c17c40e1 96 int distance = sensor.getDistanceCm();
fy12dsak 0:abe1c17c40e1 97 //serial.printf("Distance = %d cm\n",distance);
fy12dsak 0:abe1c17c40e1 98 // short delay before next measurement
fy12dsak 0:abe1c17c40e1 99
fy12dsak 0:abe1c17c40e1 100 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
fy12dsak 0:abe1c17c40e1 101 // so can display a string of a maximum 14 characters in length
fy12dsak 0:abe1c17c40e1 102 // or create formatted strings - ensure they aren't more than 14 characters long
fy12dsak 0:abe1c17c40e1 103
fy12dsak 0:abe1c17c40e1 104 int length = sprintf(buffer,"Dist = %2d cm",distance); // print formatted data to buffer
fy12dsak 0:abe1c17c40e1 105 // it is important the format specifier ensures the length will fit in the buffer
fy12dsak 0:abe1c17c40e1 106
fy12dsak 0:abe1c17c40e1 107 if (length <= 14) // if string will fit on display
fy12dsak 0:abe1c17c40e1 108 screen.printString(buffer,0,1); // display on screen
fy12dsak 0:abe1c17c40e1 109
fy12dsak 0:abe1c17c40e1 110 screen.printString("Car reversing",6,2);
fy12dsak 0:abe1c17c40e1 111
fy12dsak 0:abe1c17c40e1 112 screen.refresh();
fy12dsak 0:abe1c17c40e1 113 if(distance < 50)
fy12dsak 0:abe1c17c40e1 114 {
fy12dsak 0:abe1c17c40e1 115 // buzz = 1.0;
fy12dsak 0:abe1c17c40e1 116 // buzz.period_ms(10);
fy12dsak 0:abe1c17c40e1 117 }
fy12dsak 0:abe1c17c40e1 118 else {
fy12dsak 0:abe1c17c40e1 119 //buzz = 0.0;
fy12dsak 0:abe1c17c40e1 120
fy12dsak 0:abe1c17c40e1 121 }
fy12dsak 0:abe1c17c40e1 122 if(distance < 50) {
fy12dsak 0:abe1c17c40e1 123 LED = 1.0;
fy12dsak 0:abe1c17c40e1 124 }
fy12dsak 0:abe1c17c40e1 125 else
fy12dsak 0:abe1c17c40e1 126 {
fy12dsak 0:abe1c17c40e1 127 LED = 0.0;
fy12dsak 0:abe1c17c40e1 128 }
fy12dsak 0:abe1c17c40e1 129
fy12dsak 0:abe1c17c40e1 130 float sound = 250 - distance; /// measure the distance withe the max limite
fy12dsak 0:abe1c17c40e1 131 float frequency = sound *2; /// the frequency of sound
fy12dsak 0:abe1c17c40e1 132 float period = distance; /// measured distance
fy12dsak 0:abe1c17c40e1 133
fy12dsak 0:abe1c17c40e1 134 if ( distance < 80 )
fy12dsak 0:abe1c17c40e1 135 {
fy12dsak 0:abe1c17c40e1 136 if(frequency < 80)
fy12dsak 0:abe1c17c40e1 137 frequency = 0;
fy12dsak 0:abe1c17c40e1 138 //buzzer.beep(1000,0.1);
fy12dsak 0:abe1c17c40e1 139 buzzerCounterMax = distance;
fy12dsak 0:abe1c17c40e1 140 //wait(period);
fy12dsak 0:abe1c17c40e1 141 }
fy12dsak 0:abe1c17c40e1 142
fy12dsak 0:abe1c17c40e1 143 screen.setBrightness(Poten.read());
fy12dsak 0:abe1c17c40e1 144
fy12dsak 0:abe1c17c40e1 145 wait(0.5);
fy12dsak 0:abe1c17c40e1 146 /*
fy12dsak 0:abe1c17c40e1 147
fy12dsak 0:abe1c17c40e1 148 // these are default settings so not strictly needed
fy12dsak 0:abe1c17c40e1 149 lcd.normalMode(); // normal colour mode
fy12dsak 0:abe1c17c40e1 150 lcd.setBrightness(0.5); // put LED backlight on 50%
fy12dsak 0:abe1c17c40e1 151
fy12dsak 0:abe1c17c40e1 152 // can directly print strings at specified co-ordinates
fy12dsak 0:abe1c17c40e1 153 lcd.printString("Hello, World!",0,0);
fy12dsak 0:abe1c17c40e1 154
fy12dsak 0:abe1c17c40e1 155 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
fy12dsak 0:abe1c17c40e1 156 // so can display a string of a maximum 14 characters in length
fy12dsak 0:abe1c17c40e1 157 // or create formatted strings - ensure they aren't more than 14 characters long
fy12dsak 0:abe1c17c40e1 158 int temperature = 27;
fy12dsak 0:abe1c17c40e1 159 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
fy12dsak 0:abe1c17c40e1 160 // it is important the format specifier ensures the length will fit in the buffer
fy12dsak 0:abe1c17c40e1 161 if (length <= 14) // if string will fit on display
fy12dsak 0:abe1c17c40e1 162 lcd.printString(buffer,0,1); // display on screen
fy12dsak 0:abe1c17c40e1 163
fy12dsak 0:abe1c17c40e1 164 float pressure = 1012.3; // same idea with floats
fy12dsak 0:abe1c17c40e1 165 length = sprintf(buffer,"P = %.2f mb",pressure);
fy12dsak 0:abe1c17c40e1 166 if (length <= 14)
fy12dsak 0:abe1c17c40e1 167 lcd.printString(buffer,0,2);
fy12dsak 0:abe1c17c40e1 168
fy12dsak 0:abe1c17c40e1 169 // can also print individual characters at specified place
fy12dsak 0:abe1c17c40e1 170 lcd.printChar('X',5,3);
fy12dsak 0:abe1c17c40e1 171
fy12dsak 0:abe1c17c40e1 172 // draw a line across the display at y = 40 pixels (origin top-left)
fy12dsak 0:abe1c17c40e1 173 for (int i = 0; i < WIDTH; i++) {
fy12dsak 0:abe1c17c40e1 174 lcd.setPixel(i,40);
fy12dsak 0:abe1c17c40e1 175 }
fy12dsak 0:abe1c17c40e1 176 // need to refresh display after setting pixels
fy12dsak 0:abe1c17c40e1 177 lcd.refresh();
fy12dsak 0:abe1c17c40e1 178
fy12dsak 0:abe1c17c40e1 179 // can also check status of pixels using getPixel(x,y)
fy12dsak 0:abe1c17c40e1 180
fy12dsak 0:abe1c17c40e1 181 wait(5.0);
fy12dsak 0:abe1c17c40e1 182 lcd.clear(); // clear display
fy12dsak 0:abe1c17c40e1 183 lcd.inverseMode(); // invert colours
fy12dsak 0:abe1c17c40e1 184 lcd.setBrightness(1.0); // put LED backlight on full
fy12dsak 0:abe1c17c40e1 185
fy12dsak 0:abe1c17c40e1 186 float array[84];
fy12dsak 0:abe1c17c40e1 187
fy12dsak 0:abe1c17c40e1 188 for (int i = 0; i < 84; i++) {
fy12dsak 0:abe1c17c40e1 189 array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
fy12dsak 0:abe1c17c40e1 190 }
fy12dsak 0:abe1c17c40e1 191
fy12dsak 0:abe1c17c40e1 192 // can also plot graphs - 84 elements only
fy12dsak 0:abe1c17c40e1 193 // values must be in range 0.0 - 1.0
fy12dsak 0:abe1c17c40e1 194 lcd.plotArray(array);
fy12dsak 0:abe1c17c40e1 195 wait(5.0);
fy12dsak 0:abe1c17c40e1 196 lcd.clear();
fy12dsak 0:abe1c17c40e1 197 lcd.normalMode(); // normal colour mode back
fy12dsak 0:abe1c17c40e1 198 lcd.setBrightness(0.5); // put LED backlight on 50%
fy12dsak 0:abe1c17c40e1 199
fy12dsak 0:abe1c17c40e1 200 // example of drawing lines
fy12dsak 0:abe1c17c40e1 201 for (int x = 0; x < WIDTH ; x+=10) {
fy12dsak 0:abe1c17c40e1 202 // x0,y0,x1,y1,type 0-white,1-black,2-dotted
fy12dsak 0:abe1c17c40e1 203 lcd.drawLine(0,0,x,HEIGHT,2);
fy12dsak 0:abe1c17c40e1 204 }
fy12dsak 0:abe1c17c40e1 205 lcd.refresh(); // need to refresh screen after drawing lines
fy12dsak 0:abe1c17c40e1 206
fy12dsak 0:abe1c17c40e1 207 wait(5.0);
fy12dsak 0:abe1c17c40e1 208 lcd.clear();
fy12dsak 0:abe1c17c40e1 209
fy12dsak 0:abe1c17c40e1 210 // example of how to draw circles
fy12dsak 0:abe1c17c40e1 211 lcd.drawCircle(WIDTH/2,HEIGHT/2,20,1); // x,y,radius,black fill
fy12dsak 0:abe1c17c40e1 212 lcd.drawCircle(WIDTH/2,HEIGHT/2,10,2); // x,y,radius,white fill
fy12dsak 0:abe1c17c40e1 213 lcd.drawCircle(WIDTH/2,HEIGHT/2,30,0); // x,y,radius,transparent with outline
fy12dsak 0:abe1c17c40e1 214 lcd.refresh(); // need to refresh screen after drawing circles
fy12dsak 0:abe1c17c40e1 215
fy12dsak 0:abe1c17c40e1 216 wait(5.0);
fy12dsak 0:abe1c17c40e1 217 lcd.clear();
fy12dsak 0:abe1c17c40e1 218
fy12dsak 0:abe1c17c40e1 219 // example of how to draw rectangles
fy12dsak 0:abe1c17c40e1 220 // origin x,y,width,height,type
fy12dsak 0:abe1c17c40e1 221 lcd.drawRect(10,10,50,30,1); // filled black rectangle
fy12dsak 0:abe1c17c40e1 222 lcd.drawRect(15,15,20,10,2); // filled white rectange (no outline)
fy12dsak 0:abe1c17c40e1 223 lcd.drawRect(2,2,70,40,0); // transparent, just outline
fy12dsak 0:abe1c17c40e1 224 lcd.refresh(); // need to refresh screen after drawing rects
fy12dsak 0:abe1c17c40e1 225
fy12dsak 0:abe1c17c40e1 226
fy12dsak 0:abe1c17c40e1 227 wait(5.0);
fy12dsak 0:abe1c17c40e1 228 lcd.clear();
fy12dsak 0:abe1c17c40e1 229
fy12dsak 0:abe1c17c40e1 230 */
fy12dsak 0:abe1c17c40e1 231 // Sleep();
fy12dsak 0:abe1c17c40e1 232 //wait(0.2);
fy12dsak 0:abe1c17c40e1 233 screen.clear();
fy12dsak 0:abe1c17c40e1 234
fy12dsak 0:abe1c17c40e1 235 if (startReading)
fy12dsak 0:abe1c17c40e1 236 {
fy12dsak 0:abe1c17c40e1 237 startFlag = 0;
fy12dsak 0:abe1c17c40e1 238 }
fy12dsak 0:abe1c17c40e1 239 }
fy12dsak 0:abe1c17c40e1 240
fy12dsak 0:abe1c17c40e1 241
fy12dsak 0:abe1c17c40e1 242 }
fy12dsak 0:abe1c17c40e1 243
fy12dsak 0:abe1c17c40e1 244
fy12dsak 0:abe1c17c40e1 245 }
fy12dsak 0:abe1c17c40e1 246