Source Code
Dependencies: mbed Nucleo_Sensor_Shield
main.cpp@4:966ae598f73e, 2019-02-15 (annotated)
- Committer:
- khairunnisha
- Date:
- Fri Feb 15 03:23:22 2019 +0000
- Revision:
- 4:966ae598f73e
- Parent:
- 3:53d2d440a695
Test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kennethwfk | 0:fc777b6c55d6 | 1 | #include "mbed.h" |
kennethwfk | 0:fc777b6c55d6 | 2 | #include "x_cube_mems.h" |
kennethwfk | 0:fc777b6c55d6 | 3 | |
khairunnisha | 4:966ae598f73e | 4 | // Create a DigitalOut objects for the LED |
kennethwfk | 0:fc777b6c55d6 | 5 | DigitalOut led(LED1); |
kennethwfk | 0:fc777b6c55d6 | 6 | |
khairunnisha | 4:966ae598f73e | 7 | // Create a Serial objects to communicate via USB |
kennethwfk | 0:fc777b6c55d6 | 8 | Serial pc(USBTX, USBRX); |
kennethwfk | 0:fc777b6c55d6 | 9 | |
khairunnisha | 4:966ae598f73e | 10 | // Create 2 Ticker objects for a recurring interrupts. One for blink a LED and the other one to update the sensor lectures periodicly. |
kennethwfk | 0:fc777b6c55d6 | 11 | Ticker blinky; |
kennethwfk | 0:fc777b6c55d6 | 12 | Ticker update; |
kennethwfk | 0:fc777b6c55d6 | 13 | volatile float TEMPERATURE_C; |
khairunnisha | 4:966ae598f73e | 14 | volatile float TEMPERATURE_F; |
khairunnisha | 4:966ae598f73e | 15 | volatile float TEMPERATURE_K; |
khairunnisha | 4:966ae598f73e | 16 | volatile float HUMIDITY; |
khairunnisha | 4:966ae598f73e | 17 | volatile float PRESSURE; |
kennethwfk | 0:fc777b6c55d6 | 18 | bool measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 19 | void blinky_handler(){ |
khairunnisha | 4:966ae598f73e | 20 | led = !led;} |
kennethwfk | 0:fc777b6c55d6 | 21 | void sensors_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 22 | measurements_update = true; |
kennethwfk | 0:fc777b6c55d6 | 23 | } |
kennethwfk | 0:fc777b6c55d6 | 24 | |
kennethwfk | 0:fc777b6c55d6 | 25 | int main() { |
khairunnisha | 4:966ae598f73e | 26 | /* Create a sensor shield object */ |
kennethwfk | 0:fc777b6c55d6 | 27 | static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
kennethwfk | 0:fc777b6c55d6 | 28 | |
khairunnisha | 4:966ae598f73e | 29 | /* Attach a function to be called by the Ticker objects at a specific interval in seconds*/ |
kennethwfk | 0:fc777b6c55d6 | 30 | blinky.attach(&blinky_handler, 0.5); |
kennethwfk | 0:fc777b6c55d6 | 31 | update.attach(&sensors_handler, 3); |
kennethwfk | 0:fc777b6c55d6 | 32 | |
kennethwfk | 0:fc777b6c55d6 | 33 | while(1) { |
kennethwfk | 0:fc777b6c55d6 | 34 | if(measurements_update == true){ |
kennethwfk | 0:fc777b6c55d6 | 35 | /* Read the environmental sensors */ |
khairunnisha | 4:966ae598f73e | 36 | Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C); |
khairunnisha | 4:966ae598f73e | 37 | Sensors->hts221.GetHumidity((float *)&HUMIDITY); |
khairunnisha | 4:966ae598f73e | 38 | Sensors->lps25h.GetPressure((float *)&PRESSURE); |
selvakumararm | 2:28f8cd15fca4 | 39 | |
khairunnisha | 4:966ae598f73e | 40 | TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit |
khairunnisha | 4:966ae598f73e | 41 | TEMPERATURE_K = (TEMPERATURE_C + 273.15f); //Convert the temperature from Celsius to Kelvin |
khairunnisha | 4:966ae598f73e | 42 | pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K); |
khairunnisha | 4:966ae598f73e | 43 | pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY); |
khairunnisha | 4:966ae598f73e | 44 | pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); |
khairunnisha | 4:966ae598f73e | 45 | pc.printf("\r\n"); |
kennethwfk | 0:fc777b6c55d6 | 46 | measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 47 | } |
kennethwfk | 0:fc777b6c55d6 | 48 | __wfi(); |
kennethwfk | 0:fc777b6c55d6 | 49 | } |
khairunnisha | 4:966ae598f73e | 50 | } |