ultrasonic distance sensor - time headerfile

Dependencies:   N5110 PowerControl SRF02 beep mbed

Fork of Ultrasonicdistancesensor by I Adam

Committer:
probrania7
Date:
Mon May 11 18:47:19 2015 +0000
Revision:
3:bbfabbbc4291
Parent:
2:184524f95eb1
Ultrasonic ver1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
probrania7 3:bbfabbbc4291 1 /**
probrania7 3:bbfabbbc4291 2 @file main.h
probrania7 3:bbfabbbc4291 3 @brief @library for interfacing with SRF02 ultrasonic sensor in I2C
probrania7 3:bbfabbbc4291 4 @see http://www.robot-electronics.co.uk/htm/srf02tech.htm
probrania7 3:bbfabbbc4291 5 @Brief Nokia library provide by Dr. Craig Evans
probrania7 3:bbfabbbc4291 6 @Brief Beep library provided by
probrania7 3:bbfabbbc4291 7 @programmer Ibrahim H Adam
probrania7 3:bbfabbbc4291 8 */
probrania7 0:7a82ad48df33 9
probrania7 3:bbfabbbc4291 10 /**
probrania7 3:bbfabbbc4291 11 @brief Header files included in the project
probrania7 3:bbfabbbc4291 12 */
probrania7 3:bbfabbbc4291 13 #include "mbed.h" //mbed standared header fle
probrania7 3:bbfabbbc4291 14 #include "PowerControl/PowerControl.h" //power saving functions
probrania7 3:bbfabbbc4291 15 #include "PowerControl/EthernetPowerControl.h" //power saving functions
probrania7 3:bbfabbbc4291 16 #include "N5110.h" //Nokia screen library
probrania7 3:bbfabbbc4291 17 #include "beep.h" //beep
probrania7 3:bbfabbbc4291 18 #include "SRF02.h" //ultrasonic range finder
probrania7 3:bbfabbbc4291 19 #include "timeMgr.h" //time functions
probrania7 3:bbfabbbc4291 20 /**
probrania7 3:bbfabbbc4291 21 @brief pin connections to Nokia Screen
probrania7 0:7a82ad48df33 22 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
probrania7 3:bbfabbbc4291 23 */
probrania7 0:7a82ad48df33 24 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
probrania7 3:bbfabbbc4291 25 /**
probrania7 0:7a82ad48df33 26 // Can also power (VCC) directly from VOUT (3.3 V) -
probrania7 0:7a82ad48df33 27 // Can give better performance due to current limitation from GPIO pin
probrania7 0:7a82ad48df33 28
probrania7 3:bbfabbbc4291 29 @brief pin connection to range finder
probrania7 2:184524f95eb1 30 // SDA, SCL
probrania7 3:bbfabbbc4291 31 */
probrania7 3:bbfabbbc4291 32 SRF02 sensor(p28,p27); //
probrania7 3:bbfabbbc4291 33 Beep beep1(p21); //PWM pin
probrania7 3:bbfabbbc4291 34 AnalogIn potIn (p20);
probrania7 0:7a82ad48df33 35
probrania7 0:7a82ad48df33 36
probrania7 3:bbfabbbc4291 37 //LEDs to indicate program execution stage
probrania7 2:184524f95eb1 38 DigitalOut Led1 (p24);
probrania7 2:184524f95eb1 39 DigitalOut Led2 (p14);
probrania7 2:184524f95eb1 40 DigitalOut Led3 (p25);
probrania7 2:184524f95eb1 41 DigitalOut Led4 (p23);
probrania7 3:bbfabbbc4291 42 /**
probrania7 3:bbfabbbc4291 43 @brief buffer arraey of character data type for bufferD
probrania7 3:bbfabbbc4291 44 @brief buffer arraey of character data type for bufferE
probrania7 3:bbfabbbc4291 45 @brief buffer arraey of character data type for bufferF
probrania7 3:bbfabbbc4291 46
probrania7 3:bbfabbbc4291 47 */
probrania7 3:bbfabbbc4291 48 char bufferD[14];
probrania7 3:bbfabbbc4291 49 char bufferE[14];
probrania7 3:bbfabbbc4291 50 char bufferF[14];
probrania7 3:bbfabbbc4291 51
probrania7 3:bbfabbbc4291 52 /*
probrania7 3:bbfabbbc4291 53 @brief Type local file system created for file
probrania7 3:bbfabbbc4291 54 @brief function created to take two arguments
probrania7 3:bbfabbbc4291 55 @param data 2
probrania7 3:bbfabbbc4291 56 @param data1
probrania7 3:bbfabbbc4291 57
probrania7 3:bbfabbbc4291 58 */
probrania7 3:bbfabbbc4291 59 LocalFileSystem local("local"); //create local file system
probrania7 3:bbfabbbc4291 60 void writeDataToFile(char* (data2), float data);
probrania7 3:bbfabbbc4291 61
probrania7 3:bbfabbbc4291 62 /**
probrania7 3:bbfabbbc4291 63 @briefInterript buttons created to set flags
probrania7 3:bbfabbbc4291 64
probrania7 3:bbfabbbc4291 65 */
probrania7 0:7a82ad48df33 66 InterruptIn button1 (p17);
probrania7 0:7a82ad48df33 67 InterruptIn button2 (p29);
probrania7 0:7a82ad48df33 68 InterruptIn button3 (p30);
probrania7 3:bbfabbbc4291 69
probrania7 3:bbfabbbc4291 70 int position = 0; //position for select statement
probrania7 3:bbfabbbc4291 71 int positionB; //first position sampled in distance measurement
probrania7 3:bbfabbbc4291 72 int positionA; //second position sampled in distance measurement
probrania7 3:bbfabbbc4291 73 int movedDist; //gap moved
probrania7 3:bbfabbbc4291 74 void nextPosition(); //
probrania7 3:bbfabbbc4291 75 void calculateDist(); //distance calculculation
probrania7 3:bbfabbbc4291 76 int nextPosFlag = 0; //flag to capture while loop attention
probrania7 3:bbfabbbc4291 77 void nextPos(); //
probrania7 3:bbfabbbc4291 78 void getPointA();
probrania7 3:bbfabbbc4291 79 void getPointB();
probrania7 3:bbfabbbc4291 80
probrania7 3:bbfabbbc4291 81 void introMessage(); //into message
probrania7 3:bbfabbbc4291 82 void errorHandler(); //not included
probrania7 3:bbfabbbc4291 83 void menu(); //menu selection function
probrania7 3:bbfabbbc4291 84 void demo(); //demo code supplied with library
probrania7 3:bbfabbbc4291 85 void cursorSelect(int, int, int, int); //cursor funnction accepting 4 integer agruments
probrania7 3:bbfabbbc4291 86 void beeper(float, float); //beeper function accepting two float arguments
probrania7 3:bbfabbbc4291 87 void menuLevel(float); //menu level function accepting a float variable
probrania7 3:bbfabbbc4291 88 void startMessage(); //start message function
probrania7 3:bbfabbbc4291 89 void displayClock(); //clock function
probrania7 0:7a82ad48df33 90
probrania7 3:bbfabbbc4291 91 void distMeasure(); //distance measurement function
probrania7 3:bbfabbbc4291 92 void saveData(); //not used
probrania7 3:bbfabbbc4291 93 void adjustBrightness(float); //LCD brightness
probrania7 3:bbfabbbc4291 94 void sysClock(); //system clock - require setting with refference to EPOCH time
probrania7 3:bbfabbbc4291 95 void startClockMsg(); //clock message warning clock need setting
probrania7 3:bbfabbbc4291 96 void clearRow(); //clear pixels in selected rw
probrania7 3:bbfabbbc4291 97 void proxCheck(); //proximity check function
probrania7 3:bbfabbbc4291 98 void dataCapture(); //distance point stamping
probrania7 3:bbfabbbc4291 99 void clearRow2(); //clear pixels in selected row
probrania7 3:bbfabbbc4291 100
probrania7 3:bbfabbbc4291 101 /*
probrania7 3:bbfabbbc4291 102 @brief functions of interrupt service routine
probrania7 3:bbfabbbc4291 103 @brief user to set flags to 1
probrania7 3:bbfabbbc4291 104 */
probrania7 3:bbfabbbc4291 105
probrania7 0:7a82ad48df33 106 void button1Pressed();
probrania7 0:7a82ad48df33 107 void button2Pressed();
probrania7 0:7a82ad48df33 108 void button3Pressed();
probrania7 2:184524f95eb1 109
probrania7 3:bbfabbbc4291 110 /**
probrania7 3:bbfabbbc4291 111 @brief initially setting flags to 0
probrania7 3:bbfabbbc4291 112 @brief to be reverted to 1 from within ISR functions
probrania7 3:bbfabbbc4291 113 */
probrania7 3:bbfabbbc4291 114
probrania7 3:bbfabbbc4291 115 int button1Flag = 0;
probrania7 3:bbfabbbc4291 116 int button2Flag = 0;
probrania7 3:bbfabbbc4291 117 int button3Flag = 0;
probrania7 3:bbfabbbc4291 118 int msgFlag = 1; //introduction message flag
probrania7 3:bbfabbbc4291 119
probrania7 3:bbfabbbc4291 120 /**
probrania7 3:bbfabbbc4291 121 @brief menu level set to 0
probrania7 3:bbfabbbc4291 122 @brief brighness flag set to 0
probrania7 3:bbfabbbc4291 123 @brief distance flag set to 0
probrania7 3:bbfabbbc4291 124 @brief clock flag set to 0
probrania7 3:bbfabbbc4291 125 setting flags to 0 will cause while loop to ignore underlying programs
probrania7 3:bbfabbbc4291 126 */
probrania7 0:7a82ad48df33 127
probrania7 0:7a82ad48df33 128 int menuLevelFlag = 0;
probrania7 0:7a82ad48df33 129 int brightFlag = 0;
probrania7 0:7a82ad48df33 130 int distFlag = 0;
probrania7 0:7a82ad48df33 131 int clockFlag = 0;
probrania7 3:bbfabbbc4291 132 int dataFlag = 0;
probrania7 0:7a82ad48df33 133
probrania7 3:bbfabbbc4291 134 //more flags
probrania7 3:bbfabbbc4291 135 int getPointAFlag = 0;
probrania7 3:bbfabbbc4291 136 int getPointBFlag = 0;
probrania7 3:bbfabbbc4291 137
probrania7 3:bbfabbbc4291 138 Ticker distTimer; //ticker time object created
probrania7 3:bbfabbbc4291 139 int dstFlg = 0; //distance flag initialised to 0
probrania7 3:bbfabbbc4291 140
probrania7 3:bbfabbbc4291 141 //distance check function to sef diatance flag
probrania7 1:920749af1300 142 void distChk()
probrania7 1:920749af1300 143 {
probrania7 2:184524f95eb1 144 dstFlg = 1;
probrania7 2:184524f95eb1 145 }
probrania7 1:920749af1300 146
probrania7 3:bbfabbbc4291 147 /**
probrania7 3:bbfabbbc4291 148 @briefclkTimer object of ticker timer
probrania7 3:bbfabbbc4291 149 @briefclosk flag set to 0
probrania7 3:bbfabbbc4291 150 @briefcalls fiunction clock check
probrania7 3:bbfabbbc4291 151 @briefsets clock flag to 0
probrania7 3:bbfabbbc4291 152 */
probrania7 1:920749af1300 153 Ticker clkTimer;
probrania7 1:920749af1300 154 int clkFlg = 0;
probrania7 1:920749af1300 155 void clkChk()
probrania7 1:920749af1300 156 {
probrania7 2:184524f95eb1 157 clkFlg = 1;
probrania7 2:184524f95eb1 158 }
probrania7 2:184524f95eb1 159
probrania7 3:bbfabbbc4291 160 /**
probrania7 3:bbfabbbc4291 161 @briefvarying buzzer sound with reference to distance
probrania7 3:bbfabbbc4291 162
probrania7 3:bbfabbbc4291 163 */
probrania7 2:184524f95eb1 164 void buzzerRange();
probrania7 1:920749af1300 165
probrania7 3:bbfabbbc4291 166 int distance; //distance variable, global declaration
probrania7 0:7a82ad48df33 167
probrania7 0:7a82ad48df33 168 int main()
probrania7 3:bbfabbbc4291 169 {
probrania7 3:bbfabbbc4291 170 PHY_PowerDown(); //ethernet powerdown
probrania7 0:7a82ad48df33 171 // first need to initialise display
probrania7 0:7a82ad48df33 172 lcd.init();
probrania7 3:bbfabbbc4291 173
probrania7 0:7a82ad48df33 174 serial.attach(&serialISR); // attach serial ISR
probrania7 3:bbfabbbc4291 175 timer2.attach(&timer2Expired,1.0); //timer 2 triggered every 2 2seconds
probrania7 2:184524f95eb1 176
probrania7 0:7a82ad48df33 177 set_time(1424535031); // initialise time to 1st January 1970
probrania7 2:184524f95eb1 178 //1424535031
probrania7 2:184524f95eb1 179
probrania7 0:7a82ad48df33 180 introMessage();
probrania7 3:bbfabbbc4291 181 startClockMsg();
probrania7 3:bbfabbbc4291 182 /**
probrania7 3:bbfabbbc4291 183 @brief buttons 1 triggers on rising edge
probrania7 3:bbfabbbc4291 184 @brief button 3 triggers on thre rising edge
probrania7 3:bbfabbbc4291 185 */
probrania7 2:184524f95eb1 186 button1.rise(&button1Pressed);
probrania7 0:7a82ad48df33 187 button3.rise(&button3Pressed);
probrania7 3:bbfabbbc4291 188
probrania7 2:184524f95eb1 189
probrania7 0:7a82ad48df33 190 while(1)
probrania7 3:bbfabbbc4291 191 {
probrania7 3:bbfabbbc4291 192
probrania7 3:bbfabbbc4291 193 if (msgFlag) //if message flag is set to 1
probrania7 0:7a82ad48df33 194 {
probrania7 3:bbfabbbc4291 195 startMessage(); //while loop will allow start message to tum
probrania7 2:184524f95eb1 196 }
probrania7 3:bbfabbbc4291 197
probrania7 3:bbfabbbc4291 198 if (button1Flag) // check if button flag is set
probrania7 3:bbfabbbc4291 199 //the renus the following codes
probrania7 2:184524f95eb1 200 {
probrania7 3:bbfabbbc4291 201 msgFlag=0; //setmsgFlag to 0
probrania7 3:bbfabbbc4291 202 menu(); //call menu function
probrania7 3:bbfabbbc4291 203 menuLevelFlag = 1; //set menuLevelFunction to 1
probrania7 2:184524f95eb1 204 }
probrania7 3:bbfabbbc4291 205 /**
probrania7 3:bbfabbbc4291 206 @brief if menu level flag is set
probrania7 3:bbfabbbc4291 207 @briefset button 1 flag to 0
probrania7 3:bbfabbbc4291 208 @brief call menu level function,
probrania7 3:bbfabbbc4291 209 @brief menu level function to be controlled by potentiometer
probrania7 3:bbfabbbc4291 210 */
probrania7 2:184524f95eb1 211 if (menuLevelFlag)
probrania7 0:7a82ad48df33 212 {
probrania7 0:7a82ad48df33 213 button1Flag =0;
probrania7 2:184524f95eb1 214 menuLevel(potIn);
probrania7 0:7a82ad48df33 215 }
probrania7 3:bbfabbbc4291 216 /**
probrania7 3:bbfabbbc4291 217 @brief if distance flag is set
probrania7 3:bbfabbbc4291 218 @brief set menu level flag to 0
probrania7 3:bbfabbbc4291 219 @brief call diatance measure function,
probrania7 3:bbfabbbc4291 220 @brief button 1 rising edge sets distance flag to 0
probrania7 3:bbfabbbc4291 221 */
probrania7 2:184524f95eb1 222 if (distFlag)
probrania7 0:7a82ad48df33 223 {
probrania7 0:7a82ad48df33 224 menuLevelFlag = 0;
probrania7 3:bbfabbbc4291 225 distMeasure();
probrania7 3:bbfabbbc4291 226
probrania7 3:bbfabbbc4291 227 if(button1)
probrania7 3:bbfabbbc4291 228 {
probrania7 3:bbfabbbc4291 229 distFlag = 0;
probrania7 3:bbfabbbc4291 230 }
probrania7 3:bbfabbbc4291 231 }
probrania7 3:bbfabbbc4291 232
probrania7 3:bbfabbbc4291 233 /**
probrania7 3:bbfabbbc4291 234 @brief if data flag is set
probrania7 3:bbfabbbc4291 235 @brief set menu level flag to 0
probrania7 3:bbfabbbc4291 236 @brief call data capture function,
probrania7 3:bbfabbbc4291 237 @brief button 1 rising edge sets distance flag to 0
probrania7 3:bbfabbbc4291 238 */
probrania7 3:bbfabbbc4291 239 if (dataFlag)
probrania7 3:bbfabbbc4291 240 {
probrania7 3:bbfabbbc4291 241 menuLevelFlag = 0;
probrania7 3:bbfabbbc4291 242 dataCapture();
probrania7 3:bbfabbbc4291 243
probrania7 3:bbfabbbc4291 244 if(button1)
probrania7 0:7a82ad48df33 245 {
probrania7 3:bbfabbbc4291 246 dataFlag = 0;
probrania7 2:184524f95eb1 247 }
probrania7 3:bbfabbbc4291 248 }
probrania7 3:bbfabbbc4291 249 /**
probrania7 3:bbfabbbc4291 250 @brief if next position flag is set
probrania7 3:bbfabbbc4291 251 @brief if rising edge of button 3 is encountered
probrania7 3:bbfabbbc4291 252 @brief call next position function,
probrania7 3:bbfabbbc4291 253
probrania7 3:bbfabbbc4291 254 */
probrania7 3:bbfabbbc4291 255 if (nextPosFlag)
probrania7 3:bbfabbbc4291 256 {
probrania7 3:bbfabbbc4291 257 if (button3)
probrania7 3:bbfabbbc4291 258 nextPos();
probrania7 3:bbfabbbc4291 259 }
probrania7 3:bbfabbbc4291 260
probrania7 3:bbfabbbc4291 261 /**
probrania7 3:bbfabbbc4291 262 @brief if brigtness flag is set
probrania7 3:bbfabbbc4291 263 @brief set menu level flag to 0
probrania7 3:bbfabbbc4291 264 @brief call adjust brightness function to be controlled by potentiometer,
probrania7 3:bbfabbbc4291 265 @brief button 1 rising edge sets brightness flag to flag to 0
probrania7 3:bbfabbbc4291 266 */
probrania7 2:184524f95eb1 267 if (brightFlag)
probrania7 0:7a82ad48df33 268 {
probrania7 0:7a82ad48df33 269 menuLevelFlag = 0;
probrania7 2:184524f95eb1 270 adjustBrightness(potIn);
probrania7 2:184524f95eb1 271 if (button1)
probrania7 2:184524f95eb1 272 {
probrania7 2:184524f95eb1 273 brightFlag = 0;
probrania7 2:184524f95eb1 274 }
probrania7 2:184524f95eb1 275 }
probrania7 3:bbfabbbc4291 276 /**
probrania7 3:bbfabbbc4291 277 @brief if clock flag is set
probrania7 3:bbfabbbc4291 278 @brief set menu level flag to 0
probrania7 3:bbfabbbc4291 279 @brief call displayclock function,
probrania7 3:bbfabbbc4291 280 @brief button 1 rising edge sets distance flag to 0
probrania7 3:bbfabbbc4291 281 @brief and set timer2Flag to 0
probrania7 3:bbfabbbc4291 282 */
probrania7 2:184524f95eb1 283 if (clockFlag)
probrania7 2:184524f95eb1 284 {
probrania7 2:184524f95eb1 285 menuLevelFlag = 0;
probrania7 3:bbfabbbc4291 286 displayClock();
probrania7 3:bbfabbbc4291 287 if (button1)
probrania7 0:7a82ad48df33 288 {
probrania7 3:bbfabbbc4291 289 clockFlag = 0;
probrania7 3:bbfabbbc4291 290 timer2Flag = 0;
probrania7 2:184524f95eb1 291 }
probrania7 2:184524f95eb1 292 }
probrania7 3:bbfabbbc4291 293 /**
probrania7 3:bbfabbbc4291 294 @brief if timer2 flag is set
probrania7 3:bbfabbbc4291 295 @brief set timer2 flag to 0
probrania7 3:bbfabbbc4291 296 @brief call getCurrentTime function,
probrania7 3:bbfabbbc4291 297 @brief button 1 rising edge sets distance flag to 0
probrania7 3:bbfabbbc4291 298 */
probrania7 3:bbfabbbc4291 299 if (timer2Flag)
probrania7 3:bbfabbbc4291 300 {
probrania7 3:bbfabbbc4291 301 timer2Flag = 0;
probrania7 3:bbfabbbc4291 302 getCurrentTime();
probrania7 3:bbfabbbc4291 303 }
probrania7 3:bbfabbbc4291 304 /**
probrania7 3:bbfabbbc4291 305 @brief if setTime flag is set
probrania7 3:bbfabbbc4291 306 @brief call chkUpdateTime function,
probrania7 3:bbfabbbc4291 307 @brief button 1 rising edge sets distance flag to 0
probrania7 3:bbfabbbc4291 308 */
probrania7 3:bbfabbbc4291 309 if (setTimeFlag)
probrania7 3:bbfabbbc4291 310 {
probrania7 3:bbfabbbc4291 311 chkUpdateTime();
probrania7 3:bbfabbbc4291 312 }
probrania7 3:bbfabbbc4291 313 // function goes to sleep when no activity
probrania7 3:bbfabbbc4291 314 sleep();
probrania7 2:184524f95eb1 315 }
probrania7 0:7a82ad48df33 316 }
probrania7 0:7a82ad48df33 317
probrania7 3:bbfabbbc4291 318 /**
probrania7 3:bbfabbbc4291 319 @brief introductory message
probrania7 3:bbfabbbc4291 320
probrania7 3:bbfabbbc4291 321 */
probrania7 3:bbfabbbc4291 322
probrania7 0:7a82ad48df33 323 void introMessage()
probrania7 2:184524f95eb1 324 {
probrania7 3:bbfabbbc4291 325 /**
probrania7 3:bbfabbbc4291 326 @brief LED flash sequence
probrania7 3:bbfabbbc4291 327 @brief all LEDs ON
probrania7 3:bbfabbbc4291 328 */
probrania7 3:bbfabbbc4291 329 Led1 = 1;
probrania7 3:bbfabbbc4291 330 Led2 = 1;
probrania7 3:bbfabbbc4291 331 Led3 = 1;
probrania7 3:bbfabbbc4291 332 Led4 = 1;
probrania7 3:bbfabbbc4291 333 /**
probrania7 3:bbfabbbc4291 334 @brief introductory text a
probrania7 3:bbfabbbc4291 335 @brief name of project
probrania7 3:bbfabbbc4291 336 @brief wait for 1.3 secs
probrania7 3:bbfabbbc4291 337 @brief clear screen
probrania7 3:bbfabbbc4291 338 */
probrania7 3:bbfabbbc4291 339
probrania7 2:184524f95eb1 340 lcd.drawLine(4, 0, 79, 0, 1);
probrania7 2:184524f95eb1 341 lcd.printString("Ultrasonic",13,1);
probrania7 2:184524f95eb1 342 lcd.printString("Distance",18,2);
probrania7 2:184524f95eb1 343 lcd.printString("Sensor",23,3);
probrania7 2:184524f95eb1 344 lcd.drawLine(4, 45, 79, 45, 1);
probrania7 2:184524f95eb1 345
probrania7 2:184524f95eb1 346 wait(1.3);
probrania7 2:184524f95eb1 347 lcd.clear();
probrania7 2:184524f95eb1 348
probrania7 3:bbfabbbc4291 349 /**
probrania7 3:bbfabbbc4291 350 @brief introductory text b
probrania7 3:bbfabbbc4291 351 @brief university name
probrania7 3:bbfabbbc4291 352 @brief wait for 1.3 secs
probrania7 3:bbfabbbc4291 353 @brief clear screen
probrania7 3:bbfabbbc4291 354 */
probrania7 3:bbfabbbc4291 355
probrania7 2:184524f95eb1 356 lcd.drawLine(4, 0, 79, 0, 1);
probrania7 2:184524f95eb1 357 lcd.printString("University",13,1);
probrania7 2:184524f95eb1 358 lcd.printString("Of",35,2);
probrania7 2:184524f95eb1 359 lcd.printString("Leeds",26,3);
probrania7 2:184524f95eb1 360 lcd.drawLine(4, 45, 79, 45, 1);
probrania7 2:184524f95eb1 361
probrania7 2:184524f95eb1 362 wait(1.3);
probrania7 2:184524f95eb1 363 lcd.clear();
probrania7 2:184524f95eb1 364
probrania7 3:bbfabbbc4291 365 /**
probrania7 3:bbfabbbc4291 366 @brief introductory text c
probrania7 3:bbfabbbc4291 367 @brief programmert's name
probrania7 3:bbfabbbc4291 368 @brief wait for 1.3 secs
probrania7 3:bbfabbbc4291 369 @brief clear screen
probrania7 3:bbfabbbc4291 370 */
probrania7 3:bbfabbbc4291 371
probrania7 2:184524f95eb1 372 lcd.drawLine(4, 0, 79, 0, 1);
probrania7 2:184524f95eb1 373 lcd.printString("Ibrahim Adam",5,1);
probrania7 2:184524f95eb1 374 lcd.printString("200789110",10,2);
probrania7 2:184524f95eb1 375 lcd.printString("Yr2 - 2015",8,3);
probrania7 2:184524f95eb1 376 lcd.drawLine(4, 45, 79, 45, 1);
probrania7 2:184524f95eb1 377
probrania7 2:184524f95eb1 378 wait(1.3);
probrania7 2:184524f95eb1 379 lcd.clear();
probrania7 2:184524f95eb1 380
probrania7 3:bbfabbbc4291 381 /**
probrania7 3:bbfabbbc4291 382 @brief introductory text d
probrania7 3:bbfabbbc4291 383 @brief module title
probrania7 3:bbfabbbc4291 384 @brief wait for 1.3 secs
probrania7 3:bbfabbbc4291 385 @brief clear screen
probrania7 3:bbfabbbc4291 386 */
probrania7 3:bbfabbbc4291 387
probrania7 2:184524f95eb1 388 lcd.drawLine(4, 0, 79, 0, 1);
probrania7 2:184524f95eb1 389 lcd.printString("Elec 2645",15,1);
probrania7 2:184524f95eb1 390 lcd.printString("Embedded SYS",8,2);
probrania7 2:184524f95eb1 391 lcd.printString("Project",21,3);
probrania7 2:184524f95eb1 392 lcd.drawLine(4, 45, 79, 45, 1);
probrania7 2:184524f95eb1 393
probrania7 2:184524f95eb1 394 msgFlag = 1;
probrania7 2:184524f95eb1 395
probrania7 2:184524f95eb1 396 wait(1.3);
probrania7 2:184524f95eb1 397 lcd.clear();
probrania7 2:184524f95eb1 398 }
probrania7 0:7a82ad48df33 399
probrania7 3:bbfabbbc4291 400 /**
probrania7 3:bbfabbbc4291 401 @brief not included at present moment
probrania7 3:bbfabbbc4291 402 @brief a future posibilityt
probrania7 3:bbfabbbc4291 403
probrania7 3:bbfabbbc4291 404 */
probrania7 3:bbfabbbc4291 405
probrania7 2:184524f95eb1 406 void errorHandler()
probrania7 2:184524f95eb1 407 {
probrania7 2:184524f95eb1 408 //end text
probrania7 2:184524f95eb1 409 lcd.drawLine(4, 0, 79, 0, 1);
probrania7 2:184524f95eb1 410 lcd.printString("Error Help",12,1);
probrania7 2:184524f95eb1 411
probrania7 2:184524f95eb1 412
probrania7 2:184524f95eb1 413 wait(0.2);
probrania7 2:184524f95eb1 414 lcd.clear();
probrania7 2:184524f95eb1 415 }
probrania7 2:184524f95eb1 416
probrania7 3:bbfabbbc4291 417 /**
probrania7 3:bbfabbbc4291 418 @brief adjust brightness function
probrania7 3:bbfabbbc4291 419 @brief takes float argument
probrania7 3:bbfabbbc4291 420 @brief checks the range
probrania7 3:bbfabbbc4291 421 @brief clear screen
probrania7 3:bbfabbbc4291 422 @brief set corresponding brightness
probrania7 3:bbfabbbc4291 423 @brief display brightness level on screen
probrania7 3:bbfabbbc4291 424 */
probrania7 3:bbfabbbc4291 425
probrania7 2:184524f95eb1 426 void adjustBrightness(float x)
probrania7 3:bbfabbbc4291 427 {
probrania7 3:bbfabbbc4291 428 if ((x >= 0) && (x < 0.2))
probrania7 3:bbfabbbc4291 429 {
probrania7 3:bbfabbbc4291 430 lcd.clear();
probrania7 2:184524f95eb1 431 lcd.setBrightness(0);
probrania7 2:184524f95eb1 432 lcd.printString("Light 0%",13,1);
probrania7 2:184524f95eb1 433 }
probrania7 2:184524f95eb1 434
probrania7 3:bbfabbbc4291 435 if ((x >= 0.2) && (x < 0.4))
probrania7 3:bbfabbbc4291 436 {
probrania7 3:bbfabbbc4291 437 lcd.clear();
probrania7 2:184524f95eb1 438 lcd.setBrightness(0.25);
probrania7 2:184524f95eb1 439 lcd.printString("Light 25%",13,1);
probrania7 2:184524f95eb1 440 }
probrania7 2:184524f95eb1 441
probrania7 3:bbfabbbc4291 442 if ((x >= 0.4) && (x < 0.6))
probrania7 3:bbfabbbc4291 443 {
probrania7 3:bbfabbbc4291 444 lcd.clear();
probrania7 2:184524f95eb1 445 lcd.setBrightness(0.5);
probrania7 2:184524f95eb1 446 lcd.printString("Light 50%",13,1);
probrania7 2:184524f95eb1 447 }
probrania7 2:184524f95eb1 448
probrania7 3:bbfabbbc4291 449 if ((x >= 0.6) && (x < 0.8))
probrania7 3:bbfabbbc4291 450 {
probrania7 3:bbfabbbc4291 451 lcd.clear();
probrania7 2:184524f95eb1 452 lcd.setBrightness(0.75);
probrania7 2:184524f95eb1 453 lcd.printString("Light 75%",13,1);
probrania7 2:184524f95eb1 454 }
probrania7 3:bbfabbbc4291 455 if (x >= 0.8)
probrania7 3:bbfabbbc4291 456 {
probrania7 3:bbfabbbc4291 457 lcd.clear();
probrania7 2:184524f95eb1 458 lcd.setBrightness(1);
probrania7 2:184524f95eb1 459 lcd.printString("Light 100%",13,1);
probrania7 2:184524f95eb1 460 }
probrania7 3:bbfabbbc4291 461
probrania7 2:184524f95eb1 462 }
probrania7 2:184524f95eb1 463
probrania7 3:bbfabbbc4291 464 /**
probrania7 3:bbfabbbc4291 465 @brief data capture function
probrania7 3:bbfabbbc4291 466 @brief display title
probrania7 3:bbfabbbc4291 467 @brief call distance measure function
probrania7 3:bbfabbbc4291 468 @brief set next positioFlag to 1
probrania7 3:bbfabbbc4291 469
probrania7 3:bbfabbbc4291 470
probrania7 3:bbfabbbc4291 471 */
probrania7 3:bbfabbbc4291 472 void dataCapture()
probrania7 3:bbfabbbc4291 473 {
probrania7 3:bbfabbbc4291 474
probrania7 3:bbfabbbc4291 475 lcd.printString("Store Dist",9,0);
probrania7 3:bbfabbbc4291 476
probrania7 3:bbfabbbc4291 477 distMeasure();
probrania7 3:bbfabbbc4291 478 nextPosFlag = 1;
probrania7 3:bbfabbbc4291 479 }
probrania7 3:bbfabbbc4291 480
probrania7 3:bbfabbbc4291 481 /**
probrania7 3:bbfabbbc4291 482 @brief set LED sequence
probrania7 3:bbfabbbc4291 483 @brief clear screen
probrania7 3:bbfabbbc4291 484 @brief display main menu title
probrania7 3:bbfabbbc4291 485 @brief draw line at specitied location
probrania7 3:bbfabbbc4291 486 @brief print further information on subsquent lines
probrania7 3:bbfabbbc4291 487 @brief display brightness level on screen
probrania7 3:bbfabbbc4291 488 */
probrania7 0:7a82ad48df33 489 void menu()
probrania7 2:184524f95eb1 490 {
probrania7 3:bbfabbbc4291 491 Led1 = 1;
probrania7 3:bbfabbbc4291 492 Led2 = 0;
probrania7 3:bbfabbbc4291 493 Led3 = 0;
probrania7 3:bbfabbbc4291 494 Led4 = 0;
probrania7 2:184524f95eb1 495 lcd.clear();
probrania7 0:7a82ad48df33 496 lcd.printString("-MAIN MENU-",9,0);
probrania7 0:7a82ad48df33 497 lcd.drawLine(4, 7, 79, 7, 1);
probrania7 0:7a82ad48df33 498 lcd.printString("Dist read",15,1);
probrania7 2:184524f95eb1 499 lcd.printString("Data",15,2);
probrania7 0:7a82ad48df33 500 lcd.printString("brightness",15,3);
probrania7 2:184524f95eb1 501 lcd.printString("SYS Clock",15,4);
probrania7 2:184524f95eb1 502 }
probrania7 3:bbfabbbc4291 503 /**
probrania7 3:bbfabbbc4291 504 @brief cursor select definition function
probrania7 3:bbfabbbc4291 505 @brief vertical start point, dommension
probrania7 3:bbfabbbc4291 506 @brief horizontal start point, dimmension
probrania7 3:bbfabbbc4291 507 @brief in all specified point coordinates turn off pixel
probrania7 3:bbfabbbc4291 508
probrania7 3:bbfabbbc4291 509 */
probrania7 0:7a82ad48df33 510 void cursorSelect(int xDimm, int xLimm, int yDimm, int yLimm)
probrania7 0:7a82ad48df33 511 {
probrania7 0:7a82ad48df33 512 int i,j;
probrania7 2:184524f95eb1 513
probrania7 0:7a82ad48df33 514 //clear selecton column
probrania7 2:184524f95eb1 515 for (i = 1; i < 6; i++)
probrania7 0:7a82ad48df33 516 {
probrania7 2:184524f95eb1 517 for(j = 1; j < 46; j++)
probrania7 0:7a82ad48df33 518 {
probrania7 0:7a82ad48df33 519 lcd.clearPixel(i,j);
probrania7 2:184524f95eb1 520 }
probrania7 2:184524f95eb1 521 }
probrania7 2:184524f95eb1 522
probrania7 0:7a82ad48df33 523 //particular selection
probrania7 2:184524f95eb1 524 for (i = xDimm; i < xLimm; i++)
probrania7 0:7a82ad48df33 525 {
probrania7 2:184524f95eb1 526 for(j = yDimm; j < yLimm; j++)
probrania7 0:7a82ad48df33 527 {
probrania7 2:184524f95eb1 528 lcd.setPixel(i,j);
probrania7 2:184524f95eb1 529 }
probrania7 2:184524f95eb1 530 }
probrania7 2:184524f95eb1 531 lcd.refresh();
probrania7 2:184524f95eb1 532 }
probrania7 0:7a82ad48df33 533
probrania7 3:bbfabbbc4291 534
probrania7 3:bbfabbbc4291 535 /**
probrania7 0:7a82ad48df33 536 void demo()
probrania7 0:7a82ad48df33 537 {
probrania7 0:7a82ad48df33 538 // these are default settings so not strictly needed
probrania7 0:7a82ad48df33 539 lcd.normalMode(); // normal colour mode
probrania7 2:184524f95eb1 540 lcd.setBrightness(0.5); // put LED backlight on 50%
probrania7 0:7a82ad48df33 541
probrania7 2:184524f95eb1 542 // can directly print strings at specified co-ordinates
probrania7 2:184524f95eb1 543 lcd.printString("Hello, World!",0,0);
probrania7 0:7a82ad48df33 544
probrania7 2:184524f95eb1 545 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
probrania7 2:184524f95eb1 546 // so can display a string of a maximum 14 characters in length
probrania7 2:184524f95eb1 547 // or create formatted strings - ensure they aren't more than 14 characters long
probrania7 2:184524f95eb1 548 int temperature = 27;
probrania7 2:184524f95eb1 549 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
probrania7 2:184524f95eb1 550 // it is important the format specifier ensures the length will fit in the buffer
probrania7 2:184524f95eb1 551 if (length <= 14) // if string will fit on display
probrania7 2:184524f95eb1 552 lcd.printString(buffer,0,1); // display on screen
probrania7 0:7a82ad48df33 553
probrania7 2:184524f95eb1 554 float pressure = 1012.3; // same idea with floats
probrania7 2:184524f95eb1 555 length = sprintf(buffer,"P = %.2f mb",pressure);
probrania7 2:184524f95eb1 556 if (length <= 14)
probrania7 2:184524f95eb1 557 lcd.printString(buffer,0,2);
probrania7 0:7a82ad48df33 558
probrania7 2:184524f95eb1 559 // can also print individual characters at specified place
probrania7 2:184524f95eb1 560 lcd.printChar('X',5,3);
probrania7 0:7a82ad48df33 561
probrania7 2:184524f95eb1 562 // draw a line across the display at y = 40 pixels (origin top-left)
probrania7 2:184524f95eb1 563 for (int i = 0; i < WIDTH; i++)
probrania7 2:184524f95eb1 564 {
probrania7 2:184524f95eb1 565 lcd.setPixel(i,40);
probrania7 2:184524f95eb1 566 }
probrania7 2:184524f95eb1 567 // need to refresh display after setting pixels
probrania7 2:184524f95eb1 568 lcd.refresh();
probrania7 0:7a82ad48df33 569
probrania7 2:184524f95eb1 570 // can also check status of pixels using getPixel(x,y)
probrania7 0:7a82ad48df33 571
probrania7 2:184524f95eb1 572 wait(5.0);
probrania7 2:184524f95eb1 573 lcd.clear(); // clear display
probrania7 2:184524f95eb1 574 lcd.inverseMode(); // invert colours
probrania7 2:184524f95eb1 575 lcd.setBrightness(1.0); // put LED backlight on full
probrania7 0:7a82ad48df33 576
probrania7 2:184524f95eb1 577 float array[84];
probrania7 0:7a82ad48df33 578
probrania7 2:184524f95eb1 579 for (int i = 0; i < 84; i++)
probrania7 2:184524f95eb1 580 {
probrania7 2:184524f95eb1 581 array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
probrania7 2:184524f95eb1 582 }
probrania7 0:7a82ad48df33 583
probrania7 2:184524f95eb1 584 // can also plot graphs - 84 elements only
probrania7 2:184524f95eb1 585 // values must be in range 0.0 - 1.0
probrania7 2:184524f95eb1 586 lcd.plotArray(array);
probrania7 2:184524f95eb1 587 wait(5.0);
probrania7 2:184524f95eb1 588 lcd.clear();
probrania7 2:184524f95eb1 589 lcd.normalMode(); // normal colour mode back
probrania7 2:184524f95eb1 590 lcd.setBrightness(0.5); // put LED backlight on 50%
probrania7 0:7a82ad48df33 591
probrania7 2:184524f95eb1 592 // example of drawing lines
probrania7 2:184524f95eb1 593 for (int x = 0; x < WIDTH ; x+=10)
probrania7 2:184524f95eb1 594 {
probrania7 2:184524f95eb1 595 // x0,y0,x1,y1,type 0-white,1-black,2-dotted
probrania7 2:184524f95eb1 596 lcd.drawLine(0,0,x,HEIGHT,2);
probrania7 2:184524f95eb1 597 }
probrania7 0:7a82ad48df33 598
probrania7 2:184524f95eb1 599 wait(5.0);
probrania7 2:184524f95eb1 600 lcd.clear();
probrania7 0:7a82ad48df33 601
probrania7 2:184524f95eb1 602 // example of how to draw circles
probrania7 2:184524f95eb1 603 lcd.drawCircle(WIDTH/2,HEIGHT/2,20,1); // x,y,radius,black fill
probrania7 2:184524f95eb1 604 lcd.drawCircle(WIDTH/2,HEIGHT/2,10,2); // x,y,radius,white fill
probrania7 2:184524f95eb1 605 lcd.drawCircle(WIDTH/2,HEIGHT/2,30,0); // x,y,radius,transparent with outline
probrania7 0:7a82ad48df33 606
probrania7 2:184524f95eb1 607 wait(5.0);
probrania7 2:184524f95eb1 608 lcd.clear();
probrania7 0:7a82ad48df33 609
probrania7 2:184524f95eb1 610 // example of how to draw rectangles
probrania7 2:184524f95eb1 611 // origin x,y,width,height,type
probrania7 2:184524f95eb1 612 lcd.drawRect(10,10,50,30,1); // filled black rectangle
probrania7 2:184524f95eb1 613 lcd.drawRect(15,15,20,10,2); // filled white rectange (no outline)
probrania7 2:184524f95eb1 614 lcd.drawRect(2,2,70,40,0); // transparent, just outline
probrania7 0:7a82ad48df33 615
probrania7 2:184524f95eb1 616 wait(5.0);
probrania7 2:184524f95eb1 617 lcd.clear();
probrania7 2:184524f95eb1 618 }
probrania7 0:7a82ad48df33 619
probrania7 3:bbfabbbc4291 620 */
probrania7 3:bbfabbbc4291 621
probrania7 3:bbfabbbc4291 622 /**
probrania7 3:bbfabbbc4291 623 @brief Interrupt service routines
probrania7 3:bbfabbbc4291 624 @brief button 1
probrania7 3:bbfabbbc4291 625 @brief button 1 flag st to 1 to get while loop attention
probrania7 3:bbfabbbc4291 626 @brief button 2
probrania7 3:bbfabbbc4291 627 @brief button 2 flag st to 2 to get while loop attention
probrania7 3:bbfabbbc4291 628 @brief button 3
probrania7 3:bbfabbbc4291 629 @brief button 3 flag st to 3 to get while loop attention
probrania7 3:bbfabbbc4291 630 */
probrania7 0:7a82ad48df33 631 void button1Pressed()
probrania7 0:7a82ad48df33 632 {
probrania7 0:7a82ad48df33 633 button1Flag = 1;
probrania7 2:184524f95eb1 634 }
probrania7 0:7a82ad48df33 635 void button2Pressed()
probrania7 0:7a82ad48df33 636 {
probrania7 2:184524f95eb1 637 button2Flag = 1;
probrania7 2:184524f95eb1 638 }
probrania7 0:7a82ad48df33 639 void button3Pressed()
probrania7 0:7a82ad48df33 640 {
probrania7 2:184524f95eb1 641 button3Flag = 1;
probrania7 2:184524f95eb1 642 }
probrania7 3:bbfabbbc4291 643 /**
probrania7 3:bbfabbbc4291 644 @brief beep sound based on beeper frequency and beeper time
probrania7 3:bbfabbbc4291 645 @brief entered respectively
probrania7 3:bbfabbbc4291 646 */
probrania7 0:7a82ad48df33 647 void beeper(float beeperFreq, float beeperTime)
probrania7 0:7a82ad48df33 648 {
probrania7 2:184524f95eb1 649 beep1.beep(beeperFreq,beeperTime);
probrania7 2:184524f95eb1 650 }
probrania7 2:184524f95eb1 651
probrania7 3:bbfabbbc4291 652 /**
probrania7 3:bbfabbbc4291 653 @brief menu level function, takes float argument
probrania7 3:bbfabbbc4291 654 @brief argument checked against range 0 -1
probrania7 3:bbfabbbc4291 655 @brief corresponding cursor position established
probrania7 3:bbfabbbc4291 656 @brief if rising edge of button 2 encountered
probrania7 3:bbfabbbc4291 657 @brief clear screen
probrania7 3:bbfabbbc4291 658 @brief set distance flag to 1
probrania7 3:bbfabbbc4291 659 @brief set bright flag to 0
probrania7 3:bbfabbbc4291 660 @brief set clock flag to 0
probrania7 3:bbfabbbc4291 661 @brief set data flag to 0
probrania7 3:bbfabbbc4291 662 */
probrania7 0:7a82ad48df33 663 void menuLevel(float lvl)
probrania7 0:7a82ad48df33 664 {
probrania7 2:184524f95eb1 665 if ((lvl >= 0) && (lvl < 0.25))
probrania7 2:184524f95eb1 666 {
probrania7 0:7a82ad48df33 667 cursorSelect(1, 6, 9, 14);
probrania7 2:184524f95eb1 668 if (button2)
probrania7 3:bbfabbbc4291 669 {
probrania7 3:bbfabbbc4291 670 lcd.clear();
probrania7 0:7a82ad48df33 671 distFlag = 1;
probrania7 2:184524f95eb1 672 brightFlag = 0;
probrania7 0:7a82ad48df33 673 clockFlag = 0;
probrania7 3:bbfabbbc4291 674 dataFlag = 0;
probrania7 3:bbfabbbc4291 675
probrania7 3:bbfabbbc4291 676 }
probrania7 0:7a82ad48df33 677 }
probrania7 0:7a82ad48df33 678 if ((lvl >= 0.25) && (lvl < 0.5))
probrania7 0:7a82ad48df33 679 {
probrania7 0:7a82ad48df33 680 cursorSelect(1, 6, 17, 22);
probrania7 3:bbfabbbc4291 681 if (button2)
probrania7 3:bbfabbbc4291 682 {
probrania7 3:bbfabbbc4291 683 lcd.clear();
probrania7 3:bbfabbbc4291 684 dataFlag = 1;
probrania7 3:bbfabbbc4291 685 brightFlag = 0;
probrania7 3:bbfabbbc4291 686 distFlag =0;
probrania7 3:bbfabbbc4291 687 clockFlag = 0;
probrania7 3:bbfabbbc4291 688 }
probrania7 0:7a82ad48df33 689 }
probrania7 2:184524f95eb1 690
probrania7 2:184524f95eb1 691 if ((lvl >= 0.5) && (lvl < 0.75))
probrania7 0:7a82ad48df33 692 {
probrania7 2:184524f95eb1 693 cursorSelect(1, 6, 25, 30);
probrania7 2:184524f95eb1 694 if (button2)
probrania7 0:7a82ad48df33 695 {
probrania7 3:bbfabbbc4291 696 lcd.clear();
probrania7 0:7a82ad48df33 697 brightFlag = 1;
probrania7 2:184524f95eb1 698 distFlag =0;
probrania7 0:7a82ad48df33 699 clockFlag = 0;
probrania7 3:bbfabbbc4291 700 dataFlag = 0;
probrania7 2:184524f95eb1 701 }
probrania7 0:7a82ad48df33 702 }
probrania7 2:184524f95eb1 703 if ((lvl >= 0.75) && (lvl <= 1))
probrania7 0:7a82ad48df33 704 {
probrania7 0:7a82ad48df33 705 cursorSelect(1, 6, 33, 38);
probrania7 2:184524f95eb1 706 if (button2)
probrania7 0:7a82ad48df33 707 {
probrania7 0:7a82ad48df33 708 clockFlag = 1;
probrania7 0:7a82ad48df33 709 distFlag = 0;
probrania7 2:184524f95eb1 710 brightFlag = 0;
probrania7 3:bbfabbbc4291 711 dataFlag = 0;
probrania7 2:184524f95eb1 712 }
probrania7 0:7a82ad48df33 713 }
probrania7 2:184524f95eb1 714 }
probrania7 2:184524f95eb1 715
probrania7 3:bbfabbbc4291 716 /**
probrania7 3:bbfabbbc4291 717 @brief function wo return distance
probrania7 3:bbfabbbc4291 718 @brief distance based on speeed of sound and time travelled to and from
probrania7 3:bbfabbbc4291 719
probrania7 3:bbfabbbc4291 720 */
probrania7 0:7a82ad48df33 721 void distMeasure()
probrania7 0:7a82ad48df33 722 {
probrania7 3:bbfabbbc4291 723 /**
probrania7 3:bbfabbbc4291 724 @brief print title Measure
probrania7 3:bbfabbbc4291 725 @brief return sensor distance in cms
probrania7 3:bbfabbbc4291 726 @brief do proximity check
probrania7 3:bbfabbbc4291 727
probrania7 3:bbfabbbc4291 728 */
probrania7 0:7a82ad48df33 729 lcd.printString("Measure",15,0);
probrania7 2:184524f95eb1 730 distance = sensor.getDistanceCm();
probrania7 3:bbfabbbc4291 731 proxCheck();
probrania7 2:184524f95eb1 732
probrania7 3:bbfabbbc4291 733 /**
probrania7 3:bbfabbbc4291 734 @brief read sensor distance in cm and print over serial port
probrania7 3:bbfabbbc4291 735 @brief each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
probrania7 3:bbfabbbc4291 736 @brief so can display a string of a maximum 14 characters in length
probrania7 3:bbfabbbc4291 737 @brief or create formatted strings - ensure they aren't more than 14 characters long
probrania7 3:bbfabbbc4291 738 */
probrania7 3:bbfabbbc4291 739 char buffer[14];
probrania7 3:bbfabbbc4291 740 /**
probrania7 3:bbfabbbc4291 741 @brief print formatted data to buffer
probrania7 3:bbfabbbc4291 742 @brief it is important the format specifier ensures the length will fit in the buffer
probrania7 3:bbfabbbc4291 743 */
probrania7 3:bbfabbbc4291 744 int length = sprintf(buffer,"Dist = %2d cm",distance);
probrania7 0:7a82ad48df33 745
probrania7 3:bbfabbbc4291 746 if (length <= 14)
probrania7 3:bbfabbbc4291 747 { // if string will fit on display
probrania7 3:bbfabbbc4291 748 lcd.printString(buffer,0,1); // display on screen
probrania7 3:bbfabbbc4291 749 }
probrania7 3:bbfabbbc4291 750
probrania7 2:184524f95eb1 751 }
probrania7 2:184524f95eb1 752
probrania7 0:7a82ad48df33 753 void saveData()
probrania7 0:7a82ad48df33 754 {
probrania7 0:7a82ad48df33 755 lcd.printString("Save data",36,0);
probrania7 2:184524f95eb1 756 }
probrania7 2:184524f95eb1 757 void sysClock()
probrania7 0:7a82ad48df33 758 {
probrania7 0:7a82ad48df33 759 lcd.printString("System test",30,0);
probrania7 2:184524f95eb1 760 }
probrania7 3:bbfabbbc4291 761 /**
probrania7 3:bbfabbbc4291 762 @brief start message function
probrania7 3:bbfabbbc4291 763 @brief LED session sequence
probrania7 3:bbfabbbc4291 764 @brief display subsequent messages
probrania7 3:bbfabbbc4291 765
probrania7 3:bbfabbbc4291 766 */
probrania7 0:7a82ad48df33 767 void startMessage()
probrania7 0:7a82ad48df33 768 {
probrania7 3:bbfabbbc4291 769 Led1 = 1;
probrania7 3:bbfabbbc4291 770 Led2 = 1;
probrania7 3:bbfabbbc4291 771 Led3 = 1;
probrania7 3:bbfabbbc4291 772 Led4 = 0;
probrania7 0:7a82ad48df33 773 lcd.printString("Press",25,2);
probrania7 2:184524f95eb1 774 lcd.printString("Start Button",8,3);
probrania7 0:7a82ad48df33 775 //displayClock();
probrania7 2:184524f95eb1 776
probrania7 2:184524f95eb1 777 }
probrania7 3:bbfabbbc4291 778 /**
probrania7 3:bbfabbbc4291 779 @brief displayClock function
probrania7 3:bbfabbbc4291 780 @brief clear screen
probrania7 3:bbfabbbc4291 781 @brief print title
probrania7 3:bbfabbbc4291 782
probrania7 3:bbfabbbc4291 783 @brief check if timer2Flag is on
probrania7 3:bbfabbbc4291 784 @brief if yes, get current time
probrania7 3:bbfabbbc4291 785 @brief if setTimeflag
probrania7 3:bbfabbbc4291 786 @brief check upate time
probrania7 3:bbfabbbc4291 787 @brief print contents of buffer to screen
probrania7 3:bbfabbbc4291 788 */
probrania7 0:7a82ad48df33 789 void displayClock()
probrania7 0:7a82ad48df33 790 {
probrania7 3:bbfabbbc4291 791
probrania7 0:7a82ad48df33 792 lcd.clear();
probrania7 2:184524f95eb1 793
probrania7 0:7a82ad48df33 794 lcd.printString("-SYS CLOCK-",9,0);
probrania7 2:184524f95eb1 795
probrania7 3:bbfabbbc4291 796 if (timer2Flag)
probrania7 3:bbfabbbc4291 797 {
probrania7 2:184524f95eb1 798 getCurrentTime();
probrania7 2:184524f95eb1 799 }
probrania7 2:184524f95eb1 800
probrania7 3:bbfabbbc4291 801 if (setTimeFlag)
probrania7 3:bbfabbbc4291 802 {
probrania7 2:184524f95eb1 803 chkUpdateTime();
probrania7 2:184524f95eb1 804 }
probrania7 2:184524f95eb1 805
probrania7 2:184524f95eb1 806 lcd.printString(buffer2,4,2); // display on screen
probrania7 2:184524f95eb1 807 }
probrania7 2:184524f95eb1 808
probrania7 3:bbfabbbc4291 809 /*buzzer function
probrania7 3:bbfabbbc4291 810 @brief
probrania7 3:bbfabbbc4291 811
probrania7 3:bbfabbbc4291 812 */
probrania7 2:184524f95eb1 813 void buzzerRange()
probrania7 2:184524f95eb1 814 {
probrania7 0:7a82ad48df33 815 {
probrania7 2:184524f95eb1 816 beeper(400,distance);
probrania7 2:184524f95eb1 817 }
probrania7 2:184524f95eb1 818 }
probrania7 2:184524f95eb1 819
probrania7 3:bbfabbbc4291 820 /**
probrania7 3:bbfabbbc4291 821 @brief start clock message function
probrania7 3:bbfabbbc4291 822 @brief LED session sequence
probrania7 3:bbfabbbc4291 823 @brief if no time displayed
probrania7 3:bbfabbbc4291 824 @brief display clock
probrania7 3:bbfabbbc4291 825 @brief print title on screen
probrania7 3:bbfabbbc4291 826 @brief clear and print in succession
probrania7 3:bbfabbbc4291 827 */
probrania7 2:184524f95eb1 828 void startClockMsg()
probrania7 2:184524f95eb1 829 {
probrania7 3:bbfabbbc4291 830 Led1 = 1;
probrania7 3:bbfabbbc4291 831 Led2 = 1;
probrania7 3:bbfabbbc4291 832 Led3 = 0;
probrania7 3:bbfabbbc4291 833 Led4 = 0;
probrania7 2:184524f95eb1 834 if(time(NULL)) {
probrania7 2:184524f95eb1 835 displayClock();
probrania7 2:184524f95eb1 836 lcd.printString("Set Clock",15,0);
probrania7 3:bbfabbbc4291 837 wait(0.3);
probrania7 2:184524f95eb1 838 clearRow();
probrania7 3:bbfabbbc4291 839 wait(0.3);
probrania7 2:184524f95eb1 840 lcd.printString("Set Clock",15,0);
probrania7 3:bbfabbbc4291 841 wait(0.3);
probrania7 2:184524f95eb1 842 clearRow();
probrania7 3:bbfabbbc4291 843 wait(0.3);
probrania7 2:184524f95eb1 844 lcd.printString("Set Clock",15,0);
probrania7 3:bbfabbbc4291 845 wait(0.3);
probrania7 2:184524f95eb1 846 clearRow();
probrania7 2:184524f95eb1 847 wait(0.3);
probrania7 2:184524f95eb1 848 lcd.printString("Set Clock",15,0);
probrania7 3:bbfabbbc4291 849 wait(1.3);
probrania7 3:bbfabbbc4291 850
probrania7 3:bbfabbbc4291 851 }
probrania7 3:bbfabbbc4291 852 else
probrania7 3:bbfabbbc4291 853 {
probrania7 3:bbfabbbc4291 854 //diplay clock
probrania7 2:184524f95eb1 855 displayClock();
probrania7 2:184524f95eb1 856 lcd.printString("Clock Set",15,0);
probrania7 3:bbfabbbc4291 857 wait(1.3);
probrania7 2:184524f95eb1 858 }
probrania7 3:bbfabbbc4291 859 lcd.clear();
probrania7 2:184524f95eb1 860 }
probrania7 2:184524f95eb1 861
probrania7 3:bbfabbbc4291 862 //clear selected row
probrania7 2:184524f95eb1 863 void clearRow()
probrania7 2:184524f95eb1 864 {
probrania7 3:bbfabbbc4291 865 for (int j = 0; j <8; j++)
probrania7 0:7a82ad48df33 866 {
probrania7 3:bbfabbbc4291 867 for(int i = 0; i < 84; i++)
probrania7 3:bbfabbbc4291 868 {
probrania7 3:bbfabbbc4291 869 lcd.clearPixel(i,j);
probrania7 3:bbfabbbc4291 870 }
probrania7 3:bbfabbbc4291 871 }
probrania7 3:bbfabbbc4291 872 }
probrania7 2:184524f95eb1 873
probrania7 3:bbfabbbc4291 874 /**
probrania7 3:bbfabbbc4291 875 @brief check range of distance
probrania7 3:bbfabbbc4291 876 @brief assign a particular buzzing frequency
probrania7 2:184524f95eb1 877
probrania7 3:bbfabbbc4291 878 */
probrania7 3:bbfabbbc4291 879 void proxCheck()
probrania7 2:184524f95eb1 880 {
probrania7 3:bbfabbbc4291 881 if ((distance>=20) && (distance <50))
probrania7 2:184524f95eb1 882 {
probrania7 3:bbfabbbc4291 883 beeper(1000, 0.2);
probrania7 3:bbfabbbc4291 884
probrania7 3:bbfabbbc4291 885 }
probrania7 3:bbfabbbc4291 886
probrania7 3:bbfabbbc4291 887 if ((distance >= 10) && (distance < 20))
probrania7 3:bbfabbbc4291 888 {
probrania7 3:bbfabbbc4291 889 beeper(3000, 0.2);
probrania7 3:bbfabbbc4291 890
probrania7 2:184524f95eb1 891 }
probrania7 3:bbfabbbc4291 892
probrania7 3:bbfabbbc4291 893 if (distance < 10)
probrania7 3:bbfabbbc4291 894 {
probrania7 3:bbfabbbc4291 895 beeper(4000, 0.2);
probrania7 3:bbfabbbc4291 896 }
probrania7 2:184524f95eb1 897
probrania7 2:184524f95eb1 898 }
probrania7 2:184524f95eb1 899
probrania7 3:bbfabbbc4291 900 /**
probrania7 3:bbfabbbc4291 901 @brief next position function
probrania7 3:bbfabbbc4291 902 @brief if position is greater than3 default to 0
probrania7 3:bbfabbbc4291 903 @brief switch statement uses position value
probrania7 3:bbfabbbc4291 904 @brief getPointA - first sampled distance
probrania7 3:bbfabbbc4291 905 @brief getPointB - second sampled distance
probrania7 3:bbfabbbc4291 906 @brief calculate dist subtract to get difference
probrania7 3:bbfabbbc4291 907 */
probrania7 3:bbfabbbc4291 908 void nextPos()
probrania7 3:bbfabbbc4291 909 {
probrania7 3:bbfabbbc4291 910
probrania7 3:bbfabbbc4291 911 if (position > 3)
probrania7 3:bbfabbbc4291 912 {
probrania7 3:bbfabbbc4291 913 position = 0;
probrania7 3:bbfabbbc4291 914 }
probrania7 3:bbfabbbc4291 915 switch (position)
probrania7 3:bbfabbbc4291 916 {
probrania7 3:bbfabbbc4291 917 case 0:
probrania7 3:bbfabbbc4291 918 //lcd.clear();
probrania7 3:bbfabbbc4291 919 //lcd.printString("Point A",9,1);
probrania7 3:bbfabbbc4291 920 getPointA();
probrania7 3:bbfabbbc4291 921 //wait(1);
probrania7 3:bbfabbbc4291 922 break;
probrania7 3:bbfabbbc4291 923
probrania7 3:bbfabbbc4291 924 case 1:
probrania7 3:bbfabbbc4291 925 //lcd.clear();
probrania7 3:bbfabbbc4291 926 //lcd.printString("Point B",9,1);
probrania7 3:bbfabbbc4291 927 getPointB();
probrania7 3:bbfabbbc4291 928 //wait(1);
probrania7 3:bbfabbbc4291 929 break;
probrania7 3:bbfabbbc4291 930
probrania7 3:bbfabbbc4291 931 case 2:
probrania7 3:bbfabbbc4291 932 //lcd.clear();
probrania7 3:bbfabbbc4291 933 lcd.printString("Dist ",9,1);
probrania7 3:bbfabbbc4291 934 calculateDist();
probrania7 3:bbfabbbc4291 935 //wait(1);
probrania7 3:bbfabbbc4291 936 break;
probrania7 3:bbfabbbc4291 937 case 3:
probrania7 3:bbfabbbc4291 938
probrania7 3:bbfabbbc4291 939 //
probrania7 3:bbfabbbc4291 940 // writeDataToFile(bufferE, positionA);
probrania7 3:bbfabbbc4291 941 //writeDataToFile(bufferF, positionB);
probrania7 3:bbfabbbc4291 942
probrania7 3:bbfabbbc4291 943 //lcd.printString(bufferC,0,2);
probrania7 3:bbfabbbc4291 944
probrania7 3:bbfabbbc4291 945 positionA = 0;
probrania7 3:bbfabbbc4291 946 positionB = 0;
probrania7 3:bbfabbbc4291 947 lcd.clear();
probrania7 3:bbfabbbc4291 948 break;
probrania7 3:bbfabbbc4291 949 }
probrania7 3:bbfabbbc4291 950 //clearRow2();
probrania7 3:bbfabbbc4291 951 position = position + 1;
probrania7 3:bbfabbbc4291 952
probrania7 3:bbfabbbc4291 953 }
probrania7 3:bbfabbbc4291 954 /**
probrania7 3:bbfabbbc4291 955 @brief point A is assigned value from distance returned value
probrania7 3:bbfabbbc4291 956 */
probrania7 3:bbfabbbc4291 957 void getPointA()
probrania7 3:bbfabbbc4291 958 {
probrania7 3:bbfabbbc4291 959 //positionA = 0;
probrania7 3:bbfabbbc4291 960 positionA = distance;
probrania7 3:bbfabbbc4291 961
probrania7 3:bbfabbbc4291 962
probrania7 3:bbfabbbc4291 963 int lengthE = sprintf(bufferE,"Org = %2d cm",positionA);
probrania7 3:bbfabbbc4291 964 if (lengthE <= 14)
probrania7 3:bbfabbbc4291 965 {
probrania7 3:bbfabbbc4291 966 lcd.printString(bufferE,0,2);
probrania7 3:bbfabbbc4291 967 }
probrania7 3:bbfabbbc4291 968 }
probrania7 3:bbfabbbc4291 969 /**
probrania7 3:bbfabbbc4291 970 @brief point B is assigned value from distance returned value
probrania7 3:bbfabbbc4291 971 */
probrania7 3:bbfabbbc4291 972 void getPointB()
probrania7 3:bbfabbbc4291 973 {
probrania7 3:bbfabbbc4291 974 // positionB = 0;
probrania7 3:bbfabbbc4291 975 positionB = distance;
probrania7 3:bbfabbbc4291 976
probrania7 3:bbfabbbc4291 977
probrania7 3:bbfabbbc4291 978 int lengthF = sprintf(bufferF,"Dest = %2d cm",positionB);
probrania7 3:bbfabbbc4291 979 if (lengthF <= 14)
probrania7 3:bbfabbbc4291 980 {
probrania7 3:bbfabbbc4291 981 lcd.printString(bufferF,0,3);
probrania7 3:bbfabbbc4291 982 }
probrania7 3:bbfabbbc4291 983 }
probrania7 3:bbfabbbc4291 984 /**
probrania7 3:bbfabbbc4291 985 @brief calculate seperation distance
probrania7 3:bbfabbbc4291 986 */
probrania7 3:bbfabbbc4291 987 void calculateDist()
probrania7 3:bbfabbbc4291 988 {
probrania7 3:bbfabbbc4291 989 movedDist = positionB - positionA;
probrania7 3:bbfabbbc4291 990
probrania7 3:bbfabbbc4291 991 int lengthD = sprintf(bufferD,"Dist = %2d cm",movedDist);
probrania7 3:bbfabbbc4291 992 if (lengthD <= 14)
probrania7 3:bbfabbbc4291 993 {
probrania7 3:bbfabbbc4291 994 lcd.printString(bufferD,0,4);
probrania7 3:bbfabbbc4291 995 //writeDataToFile(bufferD, movedDist);
probrania7 3:bbfabbbc4291 996 }
probrania7 3:bbfabbbc4291 997 }
probrania7 3:bbfabbbc4291 998 //clears specific row
probrania7 3:bbfabbbc4291 999 void clearRow2()
probrania7 3:bbfabbbc4291 1000 {
probrania7 3:bbfabbbc4291 1001 for(int ir = 0; ir < 84; ir ++)
probrania7 3:bbfabbbc4291 1002 {
probrania7 3:bbfabbbc4291 1003 for(int jr = 8; jr < 16; jr ++)
probrania7 3:bbfabbbc4291 1004 {
probrania7 3:bbfabbbc4291 1005 lcd.clear();
probrania7 3:bbfabbbc4291 1006 }
probrania7 3:bbfabbbc4291 1007 }
probrania7 3:bbfabbbc4291 1008 }
probrania7 3:bbfabbbc4291 1009 /**
probrania7 3:bbfabbbc4291 1010 @brief writing to flash memory
probrania7 3:bbfabbbc4291 1011 @brief opening file, type FILE ,
probrania7 3:bbfabbbc4291 1012 @brief print string to file
probrania7 3:bbfabbbc4291 1013 @brief close file
probrania7 3:bbfabbbc4291 1014 */
probrania7 3:bbfabbbc4291 1015 void writeDataToFile(char* (data2), float data)
probrania7 3:bbfabbbc4291 1016 {
probrania7 3:bbfabbbc4291 1017 //leds = 15; //turn on leds for feedback
probrania7 3:bbfabbbc4291 1018 FILE *fp = fopen("/local/log.csv", "a"); //open 'log.csv' for apending
probrania7 3:bbfabbbc4291 1019
probrania7 3:bbfabbbc4291 1020 //if the file doesnt exist it is created, if it exists, data is apended to the end
probrania7 3:bbfabbbc4291 1021 fprintf(fp, "%s, %0.2f C\n", data2, data); //print string to file
probrania7 3:bbfabbbc4291 1022
probrania7 3:bbfabbbc4291 1023 fclose(fp); //close file
probrania7 3:bbfabbbc4291 1024
probrania7 3:bbfabbbc4291 1025 // leds = 0; //turn off leds to signify file access has finished
probrania7 3:bbfabbbc4291 1026
probrania7 3:bbfabbbc4291 1027 }
probrania7 3:bbfabbbc4291 1028
probrania7 3:bbfabbbc4291 1029