(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Final_Project

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * Author - Jack McGarley - 18689007
00003 * Date - August 2022
00004 * Acknowledgements
00005 * Craig A. Evans, University of Leeds, TMP102 Library, Feb 2016
00006 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
00007 */
00008 
00009 #include "mbed.h" // include the library header, ensure the library has been imported into the project
00010 #include "Joystick.h"
00011 #include "TMP102.h"
00012 #include "N5110.h"
00013 
00014 TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object
00015 
00016 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); //Designating N5110 Display
00017 
00018 Joystick joystick(PTB10,PTB11,PTC16); //Designating Joystick
00019 
00020 Serial pc(USBTX,USBRX); // UART connection for PC
00021 
00022 DigitalOut grn_led1(PTA1); // Designating LEDs from left to right
00023 DigitalOut grn_led2(PTA2);
00024 DigitalOut grn_led3(PTC2);
00025 DigitalOut red_led1(PTC3);
00026 DigitalOut red_led2(PTC4);
00027 DigitalOut red_led3(PTD3);
00028 DigitalIn button_A(PTB9); // Designating Buttons
00029 //DigitalIn B(PTD0);
00030 DigitalIn button_X(PTC17);
00031 DigitalIn button_Y(PTC12);
00032 DigitalIn button_L(PTB18);
00033 DigitalIn button_R(PTB3);
00034 InterruptIn button_back(PTB19);
00035 InterruptIn button_start(PTC5);
00036 InterruptIn sw2(SW2); // K64F on-board switches
00037 InterruptIn sw3(SW3);
00038 InterruptIn B(PTD0);
00039 InterruptIn R(PTB3);                           // Right Bumper Button
00040 InterruptIn L(PTB18);                          // Left Bumper Button
00041 InterruptIn A(PTB9);                           // A Button
00042 InterruptIn Y(PTC12);                          // Y Button 
00043 DigitalOut r_led(LED_RED); // K64F on-board LEDs
00044 DigitalOut g_led(LED_GREEN);
00045 DigitalOut b_led(LED_BLUE);
00046 
00047 
00048 
00049 float Morning, Afternoon, Evening, Night ;
00050 
00051 
00052 
00053 /** Declare voids before using in the code*/
00054 
00055 void button_start_isr(); // Button Start interrupt service routine
00056 void button_back_isr();
00057 
00058 void B_isr(); // Interrupt Voidsvoid R_isr();                                 
00059 void L_isr();
00060 void A_isr();
00061 void Y_isr();
00062 void init_buttons(); // Initialise buttons
00063 
00064 
00065 void error(); // error function hangs flashing an LED
00066 
00067 
00068 void init_serial(); // Setting up the serial port
00069 
00070 
00071 void init_K64F(); // Setting up the on-board LEDs and switches
00072 
00073 
00074 void init_leds(); // Setting up the LEDs
00075 
00076 
00077 void welcome(); // Setting up welcome screen
00078 
00079 
00080 void startup(); // Setting up startup
00081 
00082 
00083 void collection(); //temp collection
00084 
00085 
00086 void view_data(); //view saved data
00087 
00088 
00089 
00090 void display_tmp(); // Tempertature display
00091 
00092 
00093 
00094 void morning(); //morning temp reading
00095 
00096 
00097 
00098 void afternoon(); //afternoon temp reading
00099 
00100 
00101 
00102 void evening(); //evening temp reading
00103 
00104 
00105 
00106 void night(); //night temp reading
00107 
00108 
00109 
00110 volatile int button_start_flag;  // Global Variables
00111 volatile int button_back_flag;  // Global Variables
00112 volatile int g_R_flag = 0;                   
00113 volatile int g_L_flag = 0;                   
00114 volatile int g_A_flag = 0;                   
00115 volatile int g_Y_flag = 0;  
00116 volatile int g_B_flag = 0;
00117 
00118 
00119 
00120 
00121 /**Main Function*/
00122 int main()
00123 {
00124 button_start.mode(PullDown);
00125 button_start.rise(button_start_isr);
00126 button_back.mode(PullDown);
00127 button_back.rise(button_back_isr);
00128 B.fall(&B_isr);                         // Flipping the Interrupt Function
00129 B.mode(PullDown);                       // When the PCB Button is Pulled Down
00130 /**R.fall(&R_isr);                            
00131 R.mode(PullDown);                             
00132 L.fall(&L_isr);
00133 L.mode(PullDown);
00134 A.fall(&A_isr);A.mode(PullDown);   
00135 Y.fall(&Y_isr);
00136 Y.mode(PullDown);**/
00137 
00138 
00139 
00140 init_K64F(); // Initialising the board, serial port, LED'S and joystick
00141 init_serial();
00142 init_leds();
00143 joystick.init();
00144 
00145 
00146 
00147 tmp102.init(); // call the sensor init method using dot syntax
00148 
00149 
00150 
00151 lcd.init(); // initialise display
00152 
00153 
00154 
00155 lcd.setContrast(0.5); // Sets contrast to 0.5
00156 welcome(); // Initialising Welcome Screen
00157 startup(); // Initialising Startup
00158 
00159 
00160 
00161 while (1) {
00162 // these are settings that I have adjusted
00163 lcd.normalMode(); // normal colour mode
00164 lcd.setBrightness(0.75); // put LED backlight on 75%
00165 
00166 
00167 
00168 float T = tmp102.get_temperature(); // read temperature and print over serial port
00169 pc.printf("T = %.1f K\n",T);
00170 
00171 
00172 
00173 if (T > 37.2f) {
00174 red_led1.write(0); // LED 1 will flash when you're in High Temp
00175 pc.printf("High Temperature \n"); // Printing to the LCD
00176 }
00177 
00178 
00179 
00180 else if (T < 36.1f) {
00181 red_led2.write(0); // LED 2 will flash when you're in Low Temp
00182 printf("Low Temperature \n"); // Printing to the LCD
00183 }
00184 else if (T > 38) {
00185 red_led3.write(0); // LED 3 will flash when you're beyond high temperature
00186 printf("Call Your GP \n"); // Printing to the LCD
00187 }
00188 }
00189 }
00190 
00191 
00192 
00193 void init_serial() {
00194 pc.baud(9600); // set to highest baud - ensure terminal software matches
00195 }
00196 
00197 
00198 
00199 void init_K64F()
00200 {
00201 r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
00202 g_led = 1;
00203 b_led = 1;
00204 
00205 
00206 
00207 // since the on-board switches have external pull-ups, we should disable the internal pull-down
00208 // resistors that are enabled by default using InterruptIn
00209 sw2.mode(PullNone);
00210 sw3.mode(PullNone);
00211 
00212 
00213 
00214 }
00215 void init_leds()
00216 {
00217 red_led1.write(1); // LEDs are common anode (active-low) so writing a 1 will turn them off
00218 red_led2.write(1);
00219 red_led3.write(1);
00220 grn_led1.write(0);// LED on to show the board is on
00221 grn_led2.write(1);
00222 grn_led3.write(1);
00223 }
00224 void lcd_sett()//LCD Set
00225 {
00226 }
00227 void startup() // Void function setup
00228 {
00229 button_start_flag = 0;
00230 button_back_flag = 0;
00231 int select = 1;
00232 /**Menu starting here, using the joystick for navigating the menu.
00233 */
00234 while (1) {
00235 char d = joystick.get_direction(); // Allowing the joystick to navigate the menu
00236 
00237 
00238 
00239 switch(select) { // Main switch
00240 case 1: //Internal case 1 switch
00241 switch(d) {
00242 case N:
00243 select = 4;
00244 wait(0.3);
00245 // printf("UP");
00246 break; // Break from internal switch
00247 case S:
00248 select = 2;
00249 wait(0.3);
00250 // printf("Down");
00251 break; // Break from internal switch
00252 }
00253 break; // Break from main switch
00254 
00255 
00256 
00257 case 2: //Internal case 2 switch
00258 switch(d) {
00259 case N:
00260 select = 1;
00261 wait(0.3);
00262 // printf("UP");
00263 break; // Break from internal switch
00264 case S:
00265 select = 3;
00266 wait(0.3);
00267 // printf("Down");
00268 break; // Break from internal switch
00269 }
00270 break; // Break from main switch
00271 
00272 
00273 
00274 case 3: //Internal case 3 switch
00275 switch(d) {
00276 case N:
00277 select = 2;
00278 wait(0.3);
00279 // printf("UP");
00280 break; // Break from internal switch
00281 case S:
00282 select = 4;
00283 wait(0.3);
00284 // printf("Down");
00285 break; // Break from internal switch
00286 }
00287 break; // Break from main switch
00288 
00289 
00290 
00291 case 4: //Internal case 4 switch
00292 switch(d) {
00293 case N:
00294 select = 3;
00295 wait(0.3);
00296 // printf("UP");
00297 break; // Break from internal switch
00298 case S:
00299 select = 1;
00300 wait(0.3);
00301 // printf("Down");
00302 break; // Break from internal switch
00303 }
00304 break; // Break from main switch
00305 }
00306 wait(0.3);
00307 /** Menu selection screen printed to LCD*/
00308 
00309 if (select == 1){
00310 lcd.clear(); // Clear display
00311 lcd.printString(" >Collection ", 0, 0); // Menu Selection, printing to LCD
00312 lcd.printString(" View Data ", 0, 1);
00313 lcd.printString(" Live Data ", 0, 2);
00314 lcd.printString(" About ", 0, 3);
00315 lcd.refresh(); // Refresh display
00316 wait(0.3);
00317 
00318 if (button_start_flag==1){
00319     printf("start button");
00320     button_start_flag=0;
00321     collection();
00322 }
00323 }  
00324 
00325 
00326 
00327 else if (select == 2) {
00328 lcd.printString(" Collection ", 0, 0);// Menu Selection, Printing to LCD
00329 lcd.printString(" >View Data ", 0, 1);
00330 lcd.printString(" Live Data ", 0, 2);
00331 lcd.printString(" About ", 0, 3);
00332 lcd.refresh(); // Refresh display
00333 wait(0.3);
00334 if (button_start_flag==1){
00335     button_start_flag=0;
00336     view_data();
00337 }
00338 }    
00339 
00340 
00341 
00342 else if (select == 3) {
00343 lcd.printString(" Collection ", 0, 0);// Menu Selection, Printing to LCD
00344 lcd.printString(" View Data ", 0, 1);// Menu Selection, Printing to LCD
00345 lcd.printString(" >Live Data ", 0, 2);
00346 lcd.printString(" About ", 0, 3);
00347 lcd.refresh(); // Refresh display
00348 wait(0.3);
00349 display_tmp();}
00350 
00351 
00352 
00353 else if (select == 4){
00354 lcd.printString(" Collection ", 0, 0);// Menu Selection, Printing to LCD
00355 lcd.printString(" View Data ", 0, 1);
00356 lcd.printString(" Live Data ", 0, 2);
00357 lcd.printString(" >About ", 0, 3);
00358 lcd.refresh(); // Refresh display
00359 wait(0.3);}
00360 }
00361 }
00362 
00363 
00364 void welcome() // Start Welcome screens
00365 {                          
00366 lcd.clear();
00367 lcd.printString(" Jack McGarley ", 0, 0); // Welcome Screen 1, Printing to LCD   
00368 lcd.printString(" 18689007 ", 0, 1);
00369 lcd.printString(" Schneider ", 0, 2);
00370 lcd.printString(" Lincoln Uni ", 0, 3);
00371 lcd.printString(" February 2022 ", 0, 4);
00372 lcd.refresh(); // Refresh display
00373 wait(3);
00374 
00375 
00376 lcd.clear();
00377 lcd.printString(" Temperature ", 0, 0);// Welcome Screen 2, Printing to LCD
00378 lcd.printString(" Based ", 0, 1);
00379 lcd.printString(" Smart Device ", 0, 2);
00380 lcd.printString(" Health ", 0, 3);
00381 lcd.printString(" Monitoring ", 0, 4);
00382 lcd.refresh(); // Refresh display
00383 wait(3);
00384 }
00385 
00386 void B_isr()                            // Right Bumper Interrupt Service
00387 {g_B_flag = 1;                           // set flag in ISR
00388 }
00389 void R_isr()                            // Right Bumper Interrupt Service    
00390 {    g_R_flag = 1;                       // set flag in ISR
00391 }
00392 void L_isr()                            // Left Bumper Interrupt Service    
00393 {    g_L_flag = 1;                       // set flag in ISR
00394 }
00395 void A_isr()                            // A Button Interrupt Service    
00396 {    g_A_flag = 1;                       // set flag in ISR
00397 }
00398 void Y_isr()                            // Y Button Interrupt Service    
00399 {    g_Y_flag = 1;                       // set flag in ISR
00400 }
00401 
00402 
00403 
00404 void display_tmp() // Displays Temperature on LCD
00405 {   
00406 char buffer[14]; //each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
00407 if (g_B_flag)   { // Press button B to display Live data
00408 g_B_flag = 0;
00409 B.rise(&B_isr);
00410 float T = tmp102.get_temperature(); // Get temperature and display
00411 int length = sprintf(buffer,"%.1f C",T); // Displays to .1 decimal place        
00412 if (length <= 14) {
00413 lcd.clear(); // Clear display
00414 lcd.printString("Temperature",0,1);
00415 lcd.printString(buffer,0,2);
00416 lcd.refresh();} // Refresh display 
00417 wait(3);
00418 }
00419 }
00420 
00421 
00422 
00423 void collection(){
00424     
00425     int state = 0;
00426     
00427     while(1)
00428     {
00429         switch(state){
00430             case 0:
00431             {
00432 lcd.clear(); // Clear display
00433 lcd.printString(" >Morning", 0, 0); // Menu Selection, printing to LCD
00434 lcd.printString(" Afternoon ", 0, 1);
00435 lcd.printString(" Evening ", 0, 2);
00436 lcd.printString(" Night ", 0, 3);
00437 lcd.refresh(); // Refresh display
00438 wait(0.3);}
00439 
00440 
00441 
00442 if (button_start_flag==1){
00443     button_start_flag=0;
00444     morning();
00445 }
00446 else if (button_back_flag==1){
00447     button_back_flag=0;
00448     startup();
00449 }
00450 }
00451 }
00452 }
00453 
00454 void view_data()
00455 {
00456     
00457     lcd.clear();
00458     char buffer[14];
00459     int length = sprintf(buffer,"%.1f C", Morning);
00460     if (length <= 14)
00461     lcd.printString("Morning Temp",0,0);
00462     lcd.printString(buffer,24,3);
00463     lcd.refresh();
00464     wait(1);
00465     
00466     if (button_back_flag==1){
00467     button_back_flag=0;
00468     startup();
00469 }
00470 }   
00471 void morning(){
00472     
00473     Morning = tmp102.get_temperature();
00474     printf("morning temp = %i\n",Morning);
00475     }
00476     
00477 void button_start_isr()
00478 {
00479 button_start_flag = 1;
00480 }
00481 
00482 void button_back_isr()
00483 {
00484 button_back_flag = 1;
00485 }