Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Committer:
el15pjt
Date:
Thu May 05 11:35:51 2016 +0000
Revision:
14:204562d1fac8
Parent:
12:0171e8723d9d
Final project submitting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15pjt 7:14cfb0df30e6 1
el15pjt 7:14cfb0df30e6 2 /**
el15pjt 7:14cfb0df30e6 3 @file main.cpp
el15pjt 7:14cfb0df30e6 4 @brief Program implementation
el15pjt 0:0b180348c7e4 5 */
el15pjt 0:0b180348c7e4 6
el15pjt 7:14cfb0df30e6 7 #include "main.h"
el15pjt 0:0b180348c7e4 8
el15pjt 0:0b180348c7e4 9 int main()
el15pjt 0:0b180348c7e4 10 {
el15pjt 7:14cfb0df30e6 11 setup();
el15pjt 7:14cfb0df30e6 12 lcd.init(); // Initiate LCD
el15pjt 11:b64d123b9f4f 13 int d = 0;
el15pjt 11:b64d123b9f4f 14 int myarray[5] {999,999,999,999,999}; /// initilised to any possitive integer to prevent random values at start up triggering an alert
el15pjt 11:b64d123b9f4f 15
el15pjt 11:b64d123b9f4f 16 // Load settings from SD Card\\\\\\\\\\
el15pjt 10:c9f3c22368f1 17 FILE *fp; // file pointer
el15pjt 9:bc259fc22fa2 18 fp = fopen("/sd/settings.txt", "r");
el15pjt 9:bc259fc22fa2 19 if (fp == NULL) { // if it can't open the file then print error message
el15pjt 9:bc259fc22fa2 20 serial.printf("Error! Unable to open file!\n");
el15pjt 9:bc259fc22fa2 21 } else {
el15pjt 9:bc259fc22fa2 22 serial.printf("Reading file....\n");
el15pjt 7:14cfb0df30e6 23
el15pjt 9:bc259fc22fa2 24 while (fscanf(fp, "%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,",&bright,&units,&offset,&check_flag,&r1,&r2,&r3,&r4,&r5,&r6,&r7) != EOF) {
el15pjt 9:bc259fc22fa2 25 serial.printf("%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i, \n",bright,units,offset,check_flag,r1,r2,r3,r4,r5,r6,r7);
el15pjt 9:bc259fc22fa2 26 }
el15pjt 9:bc259fc22fa2 27 serial.printf("Done.\n");
el15pjt 9:bc259fc22fa2 28 fclose(fp); // ensure you close the file after reading
el15pjt 7:14cfb0df30e6 29 }
el15pjt 11:b64d123b9f4f 30
el15pjt 14:204562d1fac8 31 //Startup Screen
el15pjt 14:204562d1fac8 32
el15pjt 11:b64d123b9f4f 33 lcd.printString("ELEC 2645",0,0);
el15pjt 11:b64d123b9f4f 34 lcd.printString("Project Ranger",0,1);
el15pjt 11:b64d123b9f4f 35 lcd.printString("Phil Thompson",0,2);
el15pjt 11:b64d123b9f4f 36 lcd.printString("SID 200971914 ",0,3);
el15pjt 11:b64d123b9f4f 37 wait (3);
el15pjt 14:204562d1fac8 38
el15pjt 11:b64d123b9f4f 39
el15pjt 11:b64d123b9f4f 40 lcd.setBrightness(bright); //Set brightness of screen on startup from loaded value from SD Card
el15pjt 11:b64d123b9f4f 41
el15pjt 14:204562d1fac8 42 // Main while loop of the Ranger
el15pjt 0:0b180348c7e4 43 while(1) {
el15pjt 8:fe6ebe807b9d 44 if (g_sw1_flag) { /// Event triggered inturupt to call the menu and reset page numbers
el15pjt 5:083d484e95a1 45 g_sw1_flag = 0;
el15pjt 5:083d484e95a1 46 page = 0;
el15pjt 5:083d484e95a1 47 subpage = 0;
el15pjt 6:b464a8a94d91 48 lcd.clear();
el15pjt 5:083d484e95a1 49 menu();
el15pjt 2:329597081c06 50 }
el15pjt 3:8782b8b8658b 51 if (g_timer_flag_srf02) {
el15pjt 3:8782b8b8658b 52 g_timer_flag_srf02 = 0; // if it has, clear the flag
el15pjt 11:b64d123b9f4f 53 if (units == 1) { // if units = 1 metric measurment are retrived from SFR02
el15pjt 5:083d484e95a1 54 distance = srf02.getDistanceCm();
el15pjt 7:14cfb0df30e6 55 distance = distance-(offset*c);
el15pjt 11:b64d123b9f4f 56 } else { //If units = 0 imperial measurments retrived from SFR02
el15pjt 5:083d484e95a1 57 distance = srf02.getDistannceInch();
el15pjt 11:b64d123b9f4f 58 distance = distance -(offset*c);
el15pjt 11:b64d123b9f4f 59 } // close else breaket
el15pjt 9:bc259fc22fa2 60 } // close if inteurrupt bracket
el15pjt 7:14cfb0df30e6 61
el15pjt 11:b64d123b9f4f 62
el15pjt 14:204562d1fac8 63 //Function to average last 5 readings
el15pjt 9:bc259fc22fa2 64 if (d == 4) {
el15pjt 9:bc259fc22fa2 65 d = 0;
el15pjt 9:bc259fc22fa2 66 } else {
el15pjt 9:bc259fc22fa2 67 d++;
el15pjt 0:0b180348c7e4 68 }
el15pjt 9:bc259fc22fa2 69 myarray[d] = distance;
el15pjt 9:bc259fc22fa2 70 totaldistance =0;
el15pjt 10:c9f3c22368f1 71 for (int i = 0; i < 5; i++) {
el15pjt 9:bc259fc22fa2 72 totaldistance = totaldistance + myarray[i];
el15pjt 9:bc259fc22fa2 73 }
el15pjt 9:bc259fc22fa2 74 avgdistance = (totaldistance/5);
el15pjt 9:bc259fc22fa2 75
el15pjt 14:204562d1fac8 76
el15pjt 14:204562d1fac8 77 /*Debugging\\
el15pjt 11:b64d123b9f4f 78 Uncomment to use button 2 to toggle thru alert levels for debugging and comment out setalert();
el15pjt 14:204562d1fac8 79 serial.printf("TOATAL DISTANCE %i\n", totaldistance);
el15pjt 14:204562d1fac8 80 serial.printf("DISTANCE %i\n", distance);
el15pjt 14:204562d1fac8 81 serial.printf("AVERAGE DISTANCE %f\n", avgdistance);
el15pjt 14:204562d1fac8 82 serial.printf("ARRAY ELEMENT = %i\n", d);
el15pjt 14:204562d1fac8 83 serial.printf("myarray %i\n", myarray[d]);
el15pjt 11:b64d123b9f4f 84 if (g_sw2_flag) { /// Event triggered inturupt to call the menu and reset page numbers
el15pjt 11:b64d123b9f4f 85 g_sw2_flag = 0;
el15pjt 11:b64d123b9f4f 86 if (alert ==8) {
el15pjt 11:b64d123b9f4f 87 alert =0;
el15pjt 11:b64d123b9f4f 88 } else {
el15pjt 11:b64d123b9f4f 89 alert++;
el15pjt 11:b64d123b9f4f 90 }
el15pjt 14:204562d1fac8 91 serial.printf("ALERT LEVEL %i\n", alert);
el15pjt 14:204562d1fac8 92 }*/
el15pjt 14:204562d1fac8 93
el15pjt 14:204562d1fac8 94
el15pjt 10:c9f3c22368f1 95
el15pjt 5:083d484e95a1 96 setalert();
el15pjt 14:204562d1fac8 97 //serial.printf("ALERT LEVEL %i\n", alert);
el15pjt 3:8782b8b8658b 98 lcdoutput();
el15pjt 11:b64d123b9f4f 99
el15pjt 11:b64d123b9f4f 100 if (alert !=0) { // no buzzer needed at alert == 0 path clear
el15pjt 11:b64d123b9f4f 101 if (buzz_flag == 0) {// flag keeps buzz from being called durig it's off period
el15pjt 10:c9f3c22368f1 102 setbuzzer();
el15pjt 10:c9f3c22368f1 103 }
el15pjt 10:c9f3c22368f1 104 }
el15pjt 11:b64d123b9f4f 105 setleds(); // Function called to set LED outputs
el15pjt 11:b64d123b9f4f 106 sleep(); // Sleep till nest interupt
el15pjt 0:0b180348c7e4 107 }
el15pjt 0:0b180348c7e4 108 }
el15pjt 0:0b180348c7e4 109
el15pjt 11:b64d123b9f4f 110 void setalert()
el15pjt 3:8782b8b8658b 111 {
el15pjt 11:b64d123b9f4f 112 if (avgdistance >= (r6*c) && avgdistance < (r7*c)) { // r6 150 and r7 200
el15pjt 11:b64d123b9f4f 113 alert = 1; /// alert 1 avgdistance between preset 150Cm to 200Cm
el15pjt 11:b64d123b9f4f 114 } else if (avgdistance >= (r5*c) && avgdistance < (r6*c)) {
el15pjt 11:b64d123b9f4f 115 alert = 2; /// alert 2 when between preset 90Cm to 150Cm
el15pjt 11:b64d123b9f4f 116 } else if (avgdistance >= (r4*c) && avgdistance < (r5*c)) {
el15pjt 11:b64d123b9f4f 117 alert = 3; /// alert 3 when avgdistance between 60Cm to 90Cm
el15pjt 11:b64d123b9f4f 118 } else if (avgdistance >= (r3*c) && avgdistance < (r4*c)) {
el15pjt 11:b64d123b9f4f 119 alert = 4; /// alert 4 when avgdistance between 40Cm and 60Cm
el15pjt 11:b64d123b9f4f 120 } else if (avgdistance >= (r2*c) && avgdistance < (r3*c)) {
el15pjt 11:b64d123b9f4f 121 alert = 5; ///alert 5 when avgdistance between 20Cm and 40m
el15pjt 11:b64d123b9f4f 122 } else if (avgdistance >= (r1*c) && avgdistance <= (r2*c)) { //r1 3 and r2 20
el15pjt 11:b64d123b9f4f 123 alert = 6; ///alert 6 when avgdistance between 1 and 20
el15pjt 11:b64d123b9f4f 124 } else if (avgdistance <=(r1*c)) {
el15pjt 11:b64d123b9f4f 125 alert = 7; ///alert 7 when avgdistance below 1Cm
el15pjt 11:b64d123b9f4f 126 if (check_flag ==0) {
el15pjt 11:b64d123b9f4f 127 save();
el15pjt 11:b64d123b9f4f 128 }
el15pjt 11:b64d123b9f4f 129 check_flag = 1;
el15pjt 11:b64d123b9f4f 130 } else {
el15pjt 11:b64d123b9f4f 131 alert = 0; /// alert 0 all else
el15pjt 11:b64d123b9f4f 132 }
el15pjt 11:b64d123b9f4f 133 }
el15pjt 11:b64d123b9f4f 134 void lcdoutput()
el15pjt 14:204562d1fac8 135 {
el15pjt 7:14cfb0df30e6 136 // if alert == 0 no need to display range path is clear
el15pjt 6:b464a8a94d91 137 if (alert == 0) {
el15pjt 9:bc259fc22fa2 138 lcd.clear();
el15pjt 7:14cfb0df30e6 139 if (g_timer_flag_standby) {
el15pjt 7:14cfb0df30e6 140 g_timer_flag_standby = 0;
el15pjt 9:bc259fc22fa2 141 Traw = tmp102.get_temperature();
el15pjt 7:14cfb0df30e6 142 standby++;
el15pjt 9:bc259fc22fa2 143 if (standby >2) {
el15pjt 9:bc259fc22fa2 144 standby = 2;
el15pjt 7:14cfb0df30e6 145 }
el15pjt 7:14cfb0df30e6 146 }
el15pjt 7:14cfb0df30e6 147 switch (standby) {
el15pjt 9:bc259fc22fa2 148 case 0:
el15pjt 9:bc259fc22fa2 149 if (check_flag == 1) {
el15pjt 10:c9f3c22368f1 150 lcd.printString("COLLISIONCHECK",0,5);
el15pjt 9:bc259fc22fa2 151 }
el15pjt 10:c9f3c22368f1 152 lcd.printString("**PATH CLEAR**",0,0);
el15pjt 9:bc259fc22fa2 153 lcd.refresh();
el15pjt 9:bc259fc22fa2 154 break;
el15pjt 7:14cfb0df30e6 155 case 1:
el15pjt 8:fe6ebe807b9d 156 if (check_flag == 1) {
el15pjt 10:c9f3c22368f1 157 lcd.printString("COLLISIONCHECK",0,5);
el15pjt 8:fe6ebe807b9d 158 }
el15pjt 9:bc259fc22fa2 159 if (units == 0) {
el15pjt 9:bc259fc22fa2 160 T = (Traw*1.8000)+32.00;
el15pjt 9:bc259fc22fa2 161 sprintf(buffer3,"TEMP = %.2fF",T);
el15pjt 9:bc259fc22fa2 162 sprintf(buffer2,"TEMPERATER");
el15pjt 9:bc259fc22fa2 163 } else {
el15pjt 9:bc259fc22fa2 164 T=Traw;
el15pjt 9:bc259fc22fa2 165 sprintf(buffer3,"TEMP = %.2fC",T);
el15pjt 9:bc259fc22fa2 166 sprintf(buffer2,"TEMPERATER");
el15pjt 9:bc259fc22fa2 167 }
el15pjt 9:bc259fc22fa2 168 lcd.printString(buffer3,4,3);
el15pjt 9:bc259fc22fa2 169 lcd.printString(buffer2,12,2);
el15pjt 7:14cfb0df30e6 170 lcd.refresh();
el15pjt 7:14cfb0df30e6 171 break;
el15pjt 7:14cfb0df30e6 172 case 2:
el15pjt 8:fe6ebe807b9d 173 if (check_flag == 1) {
el15pjt 10:c9f3c22368f1 174 lcd.printString("COLLISIONCHECK",0,5);
el15pjt 8:fe6ebe807b9d 175 }
el15pjt 9:bc259fc22fa2 176 if (units == 0) {
el15pjt 9:bc259fc22fa2 177 T = (Traw*1.8000)+32.00;
el15pjt 9:bc259fc22fa2 178 sprintf(buffer3,"TEMP = %.2fF",T);
el15pjt 9:bc259fc22fa2 179 sprintf(buffer2,"TEMPERATER");
el15pjt 9:bc259fc22fa2 180 } else {
el15pjt 9:bc259fc22fa2 181 T=Traw;
el15pjt 9:bc259fc22fa2 182 sprintf(buffer3,"TEMP = %.2fC",T);
el15pjt 9:bc259fc22fa2 183 sprintf(buffer2,"TEMPERATER");
el15pjt 8:fe6ebe807b9d 184 }
el15pjt 9:bc259fc22fa2 185 lcd.printString(buffer3,4,3);
el15pjt 9:bc259fc22fa2 186 lcd.printString(buffer2,12,2);
el15pjt 7:14cfb0df30e6 187 lcd.refresh();
el15pjt 7:14cfb0df30e6 188 lcd.setBrightness(0);
el15pjt 7:14cfb0df30e6 189 break;
el15pjt 9:bc259fc22fa2 190 } //close switch
el15pjt 10:c9f3c22368f1 191 } else {
el15pjt 10:c9f3c22368f1 192 //If alert isn't 0 then the distance is to be dispayed alonng with the the distance bar
el15pjt 9:bc259fc22fa2 193 lcd.setBrightness(bright);
el15pjt 9:bc259fc22fa2 194 standby = 0;
el15pjt 9:bc259fc22fa2 195 lcd.clear();
el15pjt 9:bc259fc22fa2 196 if (units == 1) {
el15pjt 9:bc259fc22fa2 197 sprintf(buffer2,"%0.2f Cm",avgdistance);
el15pjt 9:bc259fc22fa2 198 } else {
el15pjt 9:bc259fc22fa2 199 sprintf(buffer2,"%0.2f In",avgdistance);
el15pjt 9:bc259fc22fa2 200 }
el15pjt 12:0171e8723d9d 201 lcd.printString(buffer2,22,2);
el15pjt 10:c9f3c22368f1 202 lcd.printString("****RANGE!****",0,0);
el15pjt 12:0171e8723d9d 203 lcd.printString("DISTANCE",18,1);
el15pjt 10:c9f3c22368f1 204 lcd.printString("Menu",0,5);
el15pjt 12:0171e8723d9d 205 float h;
el15pjt 10:c9f3c22368f1 206 h = (r7*c)/84; ///Maps length of bar to largest set range r7
el15pjt 9:bc259fc22fa2 207 distbar = (avgdistance*h)-2;
el15pjt 6:b464a8a94d91 208 //drawRect(int x0,int y0,int width,int height,int fill);
el15pjt 8:fe6ebe807b9d 209 lcd.drawRect(0,29,distbar,7,1); //
el15pjt 12:0171e8723d9d 210 // serial.printf("H = %f, r7 = %i",h,r7);
el15pjt 12:0171e8723d9d 211 // serial.printf("distbar %f",distbar);
el15pjt 6:b464a8a94d91 212 lcd.refresh();
el15pjt 5:083d484e95a1 213 }
el15pjt 10:c9f3c22368f1 214 }//close function
el15pjt 3:8782b8b8658b 215
el15pjt 2:329597081c06 216
el15pjt 3:8782b8b8658b 217 void setleds()
el15pjt 3:8782b8b8658b 218 {
el15pjt 11:b64d123b9f4f 219 int flash = 0; ///Variable to toggle LEDs high low
el15pjt 10:c9f3c22368f1 220 if (g_timer_flag_led) {
el15pjt 10:c9f3c22368f1 221 g_timer_flag_led = 0;
el15pjt 10:c9f3c22368f1 222 flash = !flash; // if it has, clear the flag
el15pjt 10:c9f3c22368f1 223 }
el15pjt 10:c9f3c22368f1 224 if(Alertlevel[alert].fa_led == HIGH) {
el15pjt 10:c9f3c22368f1 225 a_led = flash;
el15pjt 10:c9f3c22368f1 226 } else {
el15pjt 10:c9f3c22368f1 227 a_led = Alertlevel[alert].sa_led;
el15pjt 10:c9f3c22368f1 228 }
el15pjt 10:c9f3c22368f1 229 if (Alertlevel[alert].frr_led == HIGH) {
el15pjt 10:c9f3c22368f1 230 rr_led = flash;
el15pjt 3:8782b8b8658b 231 } else {
el15pjt 3:8782b8b8658b 232 rr_led = Alertlevel[alert].srr_led;
el15pjt 10:c9f3c22368f1 233 }
el15pjt 10:c9f3c22368f1 234 if(Alertlevel[alert].fgg_led == HIGH) {
el15pjt 10:c9f3c22368f1 235 gg_led = flash;
el15pjt 10:c9f3c22368f1 236 } else {
el15pjt 5:083d484e95a1 237 gg_led = Alertlevel[alert].sgg_led;
el15pjt 3:8782b8b8658b 238 }
el15pjt 5:083d484e95a1 239 }
el15pjt 7:14cfb0df30e6 240
el15pjt 5:083d484e95a1 241 void setbuzzer()
el15pjt 5:083d484e95a1 242 {
el15pjt 7:14cfb0df30e6 243 /**
el15pjt 7:14cfb0df30e6 244 control the PWM to drive the buzzer
el15pjt 7:14cfb0df30e6 245 @param buzzer.period frequncy 1KHz
el15pjt 7:14cfb0df30e6 246 @param buzzer duty cycle equal on/off max volume
el15pjt 7:14cfb0df30e6 247 @param Alertlevel[alert].toneon controls how long the tone will last depending on alert
el15pjt 7:14cfb0df30e6 248 */
el15pjt 8:fe6ebe807b9d 249 buzzer.period (1.0/1000.0);
el15pjt 10:c9f3c22368f1 250 buzzer = 0.5;
el15pjt 8:fe6ebe807b9d 251 buzzoff.attach(&flip, Alertlevel[alert].toneon);
el15pjt 8:fe6ebe807b9d 252 }
el15pjt 7:14cfb0df30e6 253
el15pjt 8:fe6ebe807b9d 254 void flip()
el15pjt 8:fe6ebe807b9d 255 {
el15pjt 8:fe6ebe807b9d 256 buzz_flag = 1;
el15pjt 8:fe6ebe807b9d 257 buzzer = 0.0;
el15pjt 9:bc259fc22fa2 258 buzzon.attach(&buzzflag, Alertlevel[alert].toneoff);
el15pjt 8:fe6ebe807b9d 259 }
el15pjt 7:14cfb0df30e6 260
el15pjt 8:fe6ebe807b9d 261 void buzzflag()
el15pjt 8:fe6ebe807b9d 262 {
el15pjt 8:fe6ebe807b9d 263 buzz_flag = 0;
el15pjt 8:fe6ebe807b9d 264 return;
el15pjt 3:8782b8b8658b 265 }
el15pjt 3:8782b8b8658b 266
el15pjt 3:8782b8b8658b 267 void backlight ()
el15pjt 3:8782b8b8658b 268 {
el15pjt 3:8782b8b8658b 269 if (bright == 1.0) {
el15pjt 3:8782b8b8658b 270 bright = 0;
el15pjt 3:8782b8b8658b 271 } else {
el15pjt 3:8782b8b8658b 272 bright += 0.2;
el15pjt 3:8782b8b8658b 273 }
el15pjt 3:8782b8b8658b 274 lcd.setBrightness(bright);
el15pjt 3:8782b8b8658b 275 }
el15pjt 3:8782b8b8658b 276
el15pjt 5:083d484e95a1 277 void menu()
el15pjt 4:673930f04866 278 {
el15pjt 5:083d484e95a1 279 while(1) {
el15pjt 5:083d484e95a1 280 if (g_sw1_flag) {
el15pjt 5:083d484e95a1 281 g_sw1_flag = 0;
el15pjt 11:b64d123b9f4f 282 page++; // Moves page
el15pjt 6:b464a8a94d91 283 lcd.clear();
el15pjt 5:083d484e95a1 284 }
el15pjt 5:083d484e95a1 285 switch (page) {
el15pjt 5:083d484e95a1 286 case 0:
el15pjt 5:083d484e95a1 287 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 288 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 289 backlight();
el15pjt 6:b464a8a94d91 290 lcd.clear();
el15pjt 5:083d484e95a1 291 }
el15pjt 9:bc259fc22fa2 292 int lightbar = bright*84;
el15pjt 9:bc259fc22fa2 293 sprintf(buffer2,"%.0f%%",bright*100);
el15pjt 9:bc259fc22fa2 294 lcd.drawRect(0,26,lightbar,7,1); // move bar up!!!!!!!!!!!!!!!!
el15pjt 10:c9f3c22368f1 295 lcd.printString("BACKLIGHT",0,1);
el15pjt 9:bc259fc22fa2 296 lcd.printString(buffer2,0,2);
el15pjt 10:c9f3c22368f1 297 lcd.printString("NEXT ADJ",0,5);
el15pjt 9:bc259fc22fa2 298 lcd.refresh();
el15pjt 5:083d484e95a1 299 break;
el15pjt 5:083d484e95a1 300 case 1:
el15pjt 5:083d484e95a1 301 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 302 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 303 if (offset == 20) {
el15pjt 5:083d484e95a1 304 offset = 0;
el15pjt 9:bc259fc22fa2 305 lcd.clear();
el15pjt 5:083d484e95a1 306 } else {
el15pjt 5:083d484e95a1 307 offset += 1;
el15pjt 5:083d484e95a1 308 }
el15pjt 5:083d484e95a1 309 }
el15pjt 9:bc259fc22fa2 310 sprintf(buffer2,"%i",offset);
el15pjt 10:c9f3c22368f1 311 lcd.printString("OFFSET",0,1);
el15pjt 9:bc259fc22fa2 312 sprintf(buffer4,"NEXT ADJ");
el15pjt 9:bc259fc22fa2 313 lcd.printString(buffer4,0,5);
el15pjt 5:083d484e95a1 314 break;
el15pjt 5:083d484e95a1 315 case 2:
el15pjt 5:083d484e95a1 316 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 317 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 9:bc259fc22fa2 318 if (units == 1) {
el15pjt 9:bc259fc22fa2 319 units = 0;
el15pjt 9:bc259fc22fa2 320 c = 0.3937;
el15pjt 5:083d484e95a1 321 } else {
el15pjt 9:bc259fc22fa2 322 units = 1;
el15pjt 7:14cfb0df30e6 323 c = 1;
el15pjt 9:bc259fc22fa2 324 lcd.clear();
el15pjt 5:083d484e95a1 325 }
el15pjt 5:083d484e95a1 326 }
el15pjt 10:c9f3c22368f1 327 if (units == 0) {
el15pjt 10:c9f3c22368f1 328 sprintf(buffer2,"IMPERIAL");
el15pjt 10:c9f3c22368f1 329 } else {
el15pjt 10:c9f3c22368f1 330 sprintf(buffer2,"METRIC");
el15pjt 10:c9f3c22368f1 331 }
el15pjt 10:c9f3c22368f1 332 lcd.printString("NEXT ADJ",0,5);
el15pjt 10:c9f3c22368f1 333 lcd.printString("UNITS",0,1);
el15pjt 5:083d484e95a1 334 break;
el15pjt 8:fe6ebe807b9d 335 case 3:
el15pjt 8:fe6ebe807b9d 336 if (g_sw2_flag) {
el15pjt 8:fe6ebe807b9d 337 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 8:fe6ebe807b9d 338 check_flag =0;
el15pjt 9:bc259fc22fa2 339 lcd.clear();
el15pjt 8:fe6ebe807b9d 340 }
el15pjt 9:bc259fc22fa2 341 if (check_flag == 0) {
el15pjt 9:bc259fc22fa2 342 sprintf(buffer2,"COLLISION");
el15pjt 10:c9f3c22368f1 343 lcd.printString("NO",0,1);
el15pjt 9:bc259fc22fa2 344 sprintf(buffer4,"NEXT ");
el15pjt 9:bc259fc22fa2 345 } else {
el15pjt 9:bc259fc22fa2 346 sprintf(buffer2,"COLLISION");
el15pjt 10:c9f3c22368f1 347 lcd.printString("CLEAR",0,1);
el15pjt 9:bc259fc22fa2 348 sprintf(buffer4,"NEXT CLEAR");
el15pjt 9:bc259fc22fa2 349 }
el15pjt 9:bc259fc22fa2 350
el15pjt 9:bc259fc22fa2 351 lcd.printString(buffer4,0,5);
el15pjt 9:bc259fc22fa2 352 break;
el15pjt 9:bc259fc22fa2 353 case 4:
el15pjt 9:bc259fc22fa2 354 if (g_sw2_flag) {
el15pjt 9:bc259fc22fa2 355 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 9:bc259fc22fa2 356 bright = 1.0;
el15pjt 9:bc259fc22fa2 357 offset = 0;
el15pjt 9:bc259fc22fa2 358 units = 1;
el15pjt 11:b64d123b9f4f 359 r1 = 03;// Upper limit of alert 1
el15pjt 11:b64d123b9f4f 360 r2 = 10;// Upper limit of alert 2
el15pjt 11:b64d123b9f4f 361 r3 = 20;// Upper limit of alert 3
el15pjt 11:b64d123b9f4f 362 r4 = 30;// Upper limit of alert 4
el15pjt 11:b64d123b9f4f 363 r5 = 50;// Upper limit of alert 5
el15pjt 11:b64d123b9f4f 364 r6 = 60;// Upper limit of alert 6
el15pjt 11:b64d123b9f4f 365 r7 = 80;// Upper limit of alert 7
el15pjt 9:bc259fc22fa2 366 lcd.clear();
el15pjt 10:c9f3c22368f1 367 lcd.printString("SETTINGS",0,1);
el15pjt 10:c9f3c22368f1 368 lcd.printString("RESET",0,2);
el15pjt 9:bc259fc22fa2 369 wait(1);
el15pjt 9:bc259fc22fa2 370 return;
el15pjt 9:bc259fc22fa2 371 }
el15pjt 9:bc259fc22fa2 372 sprintf(buffer2,"SETTINGS");
el15pjt 10:c9f3c22368f1 373 lcd.printString("RESET",0,1);
el15pjt 10:c9f3c22368f1 374 lcd.printString("NEXT RESET",0,5);
el15pjt 8:fe6ebe807b9d 375 break;
el15pjt 9:bc259fc22fa2 376 case 5:
el15pjt 5:083d484e95a1 377 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 378 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 379 submenu();
el15pjt 5:083d484e95a1 380 }
el15pjt 9:bc259fc22fa2 381 sprintf(buffer2,"PARAMETERS");
el15pjt 10:c9f3c22368f1 382 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 383 lcd.printString("EXIT ADJ",0,5);
el15pjt 5:083d484e95a1 384 break;
el15pjt 5:083d484e95a1 385 default:
el15pjt 5:083d484e95a1 386 lcd.clear();
el15pjt 9:bc259fc22fa2 387 save ();
el15pjt 10:c9f3c22368f1 388 lcd.printString(" SAVING ",0,2);
el15pjt 10:c9f3c22368f1 389 lcd.printString(" SETTINGS ",0,3);
el15pjt 9:bc259fc22fa2 390 wait (1);
el15pjt 9:bc259fc22fa2 391 return;
el15pjt 9:bc259fc22fa2 392 }// switch bracket
el15pjt 9:bc259fc22fa2 393 lcd.printString(buffer2,0,2);
el15pjt 10:c9f3c22368f1 394 lcd.printString("*****MENU*****",0,0);
el15pjt 9:bc259fc22fa2 395 lcd.refresh();
el15pjt 9:bc259fc22fa2 396 }//while braket
el15pjt 9:bc259fc22fa2 397 }//functon bracket
el15pjt 7:14cfb0df30e6 398
el15pjt 5:083d484e95a1 399 void submenu()
el15pjt 5:083d484e95a1 400 {
el15pjt 5:083d484e95a1 401 while(1) {
el15pjt 9:bc259fc22fa2 402 /// interupt used to shift page
el15pjt 5:083d484e95a1 403 if (g_sw1_flag) {
el15pjt 5:083d484e95a1 404 g_sw1_flag = 0;
el15pjt 5:083d484e95a1 405 subpage++;
el15pjt 5:083d484e95a1 406 }
el15pjt 9:bc259fc22fa2 407 switch (subpage) { ///interupt used to adjust range
el15pjt 5:083d484e95a1 408 case 0:
el15pjt 5:083d484e95a1 409 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 410 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 411 if (r2 == r3) {
el15pjt 11:b64d123b9f4f 412 r2 = 3;
el15pjt 5:083d484e95a1 413 } else {
el15pjt 5:083d484e95a1 414 r2 = r2+1;
el15pjt 5:083d484e95a1 415 }
el15pjt 5:083d484e95a1 416 }
el15pjt 9:bc259fc22fa2 417 sprintf(buffer4,"1Cm to %iCm",r2);
el15pjt 10:c9f3c22368f1 418 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 419 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 420 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 421 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 422 lcd.printString("NEXT ADJ",0,5);
el15pjt 5:083d484e95a1 423 break;
el15pjt 5:083d484e95a1 424 case 1:
el15pjt 5:083d484e95a1 425 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 426 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 427 if (r3 == r4) {
el15pjt 5:083d484e95a1 428 r3 = r2;
el15pjt 5:083d484e95a1 429 } else {
el15pjt 5:083d484e95a1 430 r3 += 1;
el15pjt 5:083d484e95a1 431 }
el15pjt 5:083d484e95a1 432 }
el15pjt 9:bc259fc22fa2 433 sprintf(buffer4,"%iCm to %iCm",r2,r3);
el15pjt 10:c9f3c22368f1 434 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 435 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 436 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 437 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 438 lcd.printString("NEXT ADJ",0,5);
el15pjt 5:083d484e95a1 439 break;
el15pjt 5:083d484e95a1 440 case 2:
el15pjt 5:083d484e95a1 441 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 442 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 443 if (r4 == r5) {
el15pjt 5:083d484e95a1 444 r4 = r3;
el15pjt 5:083d484e95a1 445 } else {
el15pjt 5:083d484e95a1 446 r4 += 1;
el15pjt 5:083d484e95a1 447 }
el15pjt 5:083d484e95a1 448 }
el15pjt 9:bc259fc22fa2 449 sprintf(buffer4,"%iCm to %iCm",r3,r4);
el15pjt 10:c9f3c22368f1 450 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 451 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 452 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 453 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 454 lcd.printString("NEXT ADJ",0,5);
el15pjt 5:083d484e95a1 455 break;
el15pjt 5:083d484e95a1 456 case 3:
el15pjt 5:083d484e95a1 457 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 458 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 459 if (r5 == r6) {
el15pjt 5:083d484e95a1 460 r5 = r4;
el15pjt 5:083d484e95a1 461 } else {
el15pjt 5:083d484e95a1 462 r5 += 1;
el15pjt 5:083d484e95a1 463 }
el15pjt 5:083d484e95a1 464 }
el15pjt 11:b64d123b9f4f 465 sprintf(buffer4,"%iCm to %iCm",r4,r5);
el15pjt 10:c9f3c22368f1 466 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 467 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 468 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 469 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 470 lcd.printString("NEXT ADJ",0,5);
el15pjt 5:083d484e95a1 471 break;
el15pjt 5:083d484e95a1 472 case 4:
el15pjt 5:083d484e95a1 473 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 474 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 475 if (r6 == r7) {
el15pjt 5:083d484e95a1 476 r6 = r5;
el15pjt 5:083d484e95a1 477 } else {
el15pjt 5:083d484e95a1 478 r6 += 1;
el15pjt 5:083d484e95a1 479 }
el15pjt 5:083d484e95a1 480 }
el15pjt 9:bc259fc22fa2 481 sprintf(buffer4,"%iCm to %iCm",r5,r6);
el15pjt 10:c9f3c22368f1 482 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 483 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 484 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 485 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 486 lcd.printString("NEXT ADJ",0,5);
el15pjt 5:083d484e95a1 487 break;
el15pjt 5:083d484e95a1 488 case 5:
el15pjt 5:083d484e95a1 489 if (g_sw2_flag) {
el15pjt 5:083d484e95a1 490 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 5:083d484e95a1 491 if (r7 == 300) {
el15pjt 5:083d484e95a1 492 r7 = r6;
el15pjt 5:083d484e95a1 493 } else {
el15pjt 5:083d484e95a1 494 r7 += 1;
el15pjt 5:083d484e95a1 495 }
el15pjt 5:083d484e95a1 496 }
el15pjt 9:bc259fc22fa2 497 sprintf(buffer4,"%iCm to %iCm",r6,r7);
el15pjt 10:c9f3c22368f1 498 lcd.printString("*****MENU*****",0,0);
el15pjt 10:c9f3c22368f1 499 lcd.printString("RANGE",0,1);
el15pjt 10:c9f3c22368f1 500 lcd.printString("PARAMETERS",0,2);
el15pjt 5:083d484e95a1 501 lcd.printString(buffer4,0,3);
el15pjt 10:c9f3c22368f1 502 lcd.printString("EXIT ADJ",0,5);
el15pjt 5:083d484e95a1 503 break;
el15pjt 5:083d484e95a1 504 default:
el15pjt 5:083d484e95a1 505 lcd.clear();
el15pjt 5:083d484e95a1 506 return;
el15pjt 9:bc259fc22fa2 507 }//switch breaket
el15pjt 9:bc259fc22fa2 508 }//while bracket
el15pjt 9:bc259fc22fa2 509 }//function bracket
el15pjt 9:bc259fc22fa2 510
el15pjt 11:b64d123b9f4f 511 void save()
el15pjt 11:b64d123b9f4f 512 {
el15pjt 11:b64d123b9f4f 513 fp = fopen("/sd/settings.txt", "w");
el15pjt 11:b64d123b9f4f 514 if (fp == NULL) { // if it can't open the file then print error message
el15pjt 11:b64d123b9f4f 515 serial.printf("Error! Unable to open file!\n");
el15pjt 11:b64d123b9f4f 516 } else { // opened file so can write
el15pjt 11:b64d123b9f4f 517 serial.printf("Writing to file....");
el15pjt 11:b64d123b9f4f 518 fprintf(fp, "%f,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",bright,units,offset,check_flag,r1,r2,r3,r4,r5,r6,r7); // ensure data type matches
el15pjt 11:b64d123b9f4f 519 serial.printf("Done.\n");
el15pjt 11:b64d123b9f4f 520 fclose(fp); // ensure you close the file after writing
el15pjt 11:b64d123b9f4f 521 }
el15pjt 11:b64d123b9f4f 522 }
el15pjt 11:b64d123b9f4f 523
el15pjt 7:14cfb0df30e6 524 void setup()
el15pjt 7:14cfb0df30e6 525 {
el15pjt 10:c9f3c22368f1 526 serial.baud(115200); // full-speed!
el15pjt 7:14cfb0df30e6 527 ticker.attach(&timer_isr_led,0.35); /// Attach the ticker for the flashig LEDs
el15pjt 11:b64d123b9f4f 528 ticker_srf02.attach(&timer_isr_srf02,0.2);/// Attach the ticker for collecting a range reading
el15pjt 9:bc259fc22fa2 529 ticker_standby.attach(&timer_isr_standby,5.0);
el15pjt 7:14cfb0df30e6 530 sw1.rise(&sw1_isr); /// sw1_isr called when button presed on the rising edge
el15pjt 7:14cfb0df30e6 531 sw2.rise(&sw2_isr); /// sw2_isr called when button presed on the rising edge
el15pjt 9:bc259fc22fa2 532 r_led = 1; //Onboard leds
el15pjt 9:bc259fc22fa2 533 b_led = 1; //Onboard leds
el15pjt 9:bc259fc22fa2 534 g_led = 1; //Onboard leds
el15pjt 9:bc259fc22fa2 535 rr_led = 0; //PCB LEDS
el15pjt 9:bc259fc22fa2 536 a_led = 0; //PCB LEDS
el15pjt 9:bc259fc22fa2 537 gg_led = 0; //PCB LEDS
el15pjt 9:bc259fc22fa2 538 sw2.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
el15pjt 9:bc259fc22fa2 539 sw1.mode(PullDown); //Turns on use of the pulldown resistors for use with the PCB buttons
el15pjt 11:b64d123b9f4f 540 }
el15pjt 11:b64d123b9f4f 541
el15pjt 11:b64d123b9f4f 542 void sw2_isr()
el15pjt 11:b64d123b9f4f 543 {
el15pjt 11:b64d123b9f4f 544 g_sw2_flag = 1; /** set flag in ISR by button 2 @param g_sw2_flag 0 or 1*/
el15pjt 11:b64d123b9f4f 545 }
el15pjt 11:b64d123b9f4f 546
el15pjt 11:b64d123b9f4f 547 void sw1_isr()
el15pjt 11:b64d123b9f4f 548 {
el15pjt 11:b64d123b9f4f 549 g_sw1_flag = 1; /** set flag in ISR by button 2 @param g_sw1_flag 0 or 1*/
el15pjt 11:b64d123b9f4f 550 }
el15pjt 11:b64d123b9f4f 551
el15pjt 11:b64d123b9f4f 552 void timer_isr_led()
el15pjt 11:b64d123b9f4f 553 {
el15pjt 11:b64d123b9f4f 554 g_timer_flag_led = 1; /** set flag in ISR by timer_isr_led @param g_timer_flag_led 0 or 1 */
el15pjt 11:b64d123b9f4f 555 }
el15pjt 11:b64d123b9f4f 556
el15pjt 11:b64d123b9f4f 557 void timer_isr_srf02()
el15pjt 11:b64d123b9f4f 558 {
el15pjt 11:b64d123b9f4f 559 g_timer_flag_srf02 = 1; /** set flag in ISR by ticker_srf02 @param g_timer_flag_srf02 0 or 1 */
el15pjt 11:b64d123b9f4f 560 }
el15pjt 11:b64d123b9f4f 561
el15pjt 11:b64d123b9f4f 562 void timer_isr_standby()
el15pjt 11:b64d123b9f4f 563 {
el15pjt 11:b64d123b9f4f 564 g_timer_flag_standby = 1; /** set flag in ISR by ticker_tone @param g_timer_flag_tone 0 or 1 */
el15pjt 11:b64d123b9f4f 565 }