Exercise4
Dependencies: mbed Nucleo_Sensor_Shield
main.cpp@1:be2eea2ae8e7, 2018-02-08 (annotated)
- Committer:
- kennethwfk
- Date:
- Thu Feb 08 08:11:12 2018 +0000
- Revision:
- 1:be2eea2ae8e7
- Parent:
- 0:fc777b6c55d6
First draft for demo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kennethwfk | 0:fc777b6c55d6 | 1 | /*---------------------------------------------------------------------------- |
kennethwfk | 0:fc777b6c55d6 | 2 | LAB EXERCISE - Environmental sensors measurments |
kennethwfk | 0:fc777b6c55d6 | 3 | ---------------------------------------- |
kennethwfk | 0:fc777b6c55d6 | 4 | In this exercise we will read the environmental sensors on the Nucleo sensors shield (X-NUCLEO-IKS01A1) |
kennethwfk | 0:fc777b6c55d6 | 5 | with the help of the ST Nucleo Sensor Shield library which is compatible with the MBED API. |
kennethwfk | 0:fc777b6c55d6 | 6 | Then we will send the measurements via USB to our PC using serial communication. |
kennethwfk | 0:fc777b6c55d6 | 7 | We can then display the results using a terminal emulation program (e.g. Termite). |
kennethwfk | 0:fc777b6c55d6 | 8 | |
kennethwfk | 0:fc777b6c55d6 | 9 | At the same time the program blinks the on board LED to show alivness. |
kennethwfk | 0:fc777b6c55d6 | 10 | |
kennethwfk | 0:fc777b6c55d6 | 11 | |
kennethwfk | 0:fc777b6c55d6 | 12 | GOOD LUCK! |
kennethwfk | 0:fc777b6c55d6 | 13 | *----------------------------------------------------------------------------*/ |
kennethwfk | 0:fc777b6c55d6 | 14 | |
kennethwfk | 0:fc777b6c55d6 | 15 | #include "mbed.h" |
kennethwfk | 0:fc777b6c55d6 | 16 | #include "x_cube_mems.h" |
kennethwfk | 0:fc777b6c55d6 | 17 | |
kennethwfk | 0:fc777b6c55d6 | 18 | // Create a DigitalOut objects for the LED |
kennethwfk | 0:fc777b6c55d6 | 19 | DigitalOut led(LED1); |
kennethwfk | 0:fc777b6c55d6 | 20 | |
kennethwfk | 0:fc777b6c55d6 | 21 | // Create a Serial objects to communicate via USB |
kennethwfk | 0:fc777b6c55d6 | 22 | Serial pc(USBTX, USBRX); |
kennethwfk | 0:fc777b6c55d6 | 23 | |
kennethwfk | 0:fc777b6c55d6 | 24 | // 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 | 25 | Ticker blinky; |
kennethwfk | 0:fc777b6c55d6 | 26 | Ticker update; |
kennethwfk | 0:fc777b6c55d6 | 27 | |
kennethwfk | 0:fc777b6c55d6 | 28 | volatile float TEMPERATURE_C; |
kennethwfk | 0:fc777b6c55d6 | 29 | volatile float TEMPERATURE_F; |
kennethwfk | 0:fc777b6c55d6 | 30 | volatile float TEMPERATURE_K; |
kennethwfk | 0:fc777b6c55d6 | 31 | volatile float HUMIDITY; |
kennethwfk | 0:fc777b6c55d6 | 32 | volatile float PRESSURE; |
kennethwfk | 0:fc777b6c55d6 | 33 | |
kennethwfk | 0:fc777b6c55d6 | 34 | bool measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 35 | |
kennethwfk | 0:fc777b6c55d6 | 36 | void blinky_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 37 | led = !led; |
kennethwfk | 0:fc777b6c55d6 | 38 | } |
kennethwfk | 0:fc777b6c55d6 | 39 | |
kennethwfk | 0:fc777b6c55d6 | 40 | void sensors_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 41 | measurements_update = true; |
kennethwfk | 0:fc777b6c55d6 | 42 | } |
kennethwfk | 0:fc777b6c55d6 | 43 | |
kennethwfk | 0:fc777b6c55d6 | 44 | int main() { |
kennethwfk | 0:fc777b6c55d6 | 45 | |
kennethwfk | 0:fc777b6c55d6 | 46 | /* Create a sensor shield object */ |
kennethwfk | 0:fc777b6c55d6 | 47 | static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
kennethwfk | 0:fc777b6c55d6 | 48 | |
kennethwfk | 0:fc777b6c55d6 | 49 | /* Attach a function to be called by the Ticker objects at a specific interval in seconds*/ |
kennethwfk | 0:fc777b6c55d6 | 50 | blinky.attach(&blinky_handler, 0.5); |
kennethwfk | 0:fc777b6c55d6 | 51 | update.attach(&sensors_handler, 3); |
kennethwfk | 0:fc777b6c55d6 | 52 | |
kennethwfk | 0:fc777b6c55d6 | 53 | while(1) { |
kennethwfk | 0:fc777b6c55d6 | 54 | |
kennethwfk | 0:fc777b6c55d6 | 55 | if(measurements_update == true){ |
kennethwfk | 0:fc777b6c55d6 | 56 | /* Read the environmental sensors */ |
kennethwfk | 0:fc777b6c55d6 | 57 | Sensors->hts221.GetTemperature((float *)&TEMPERATURE_C); |
kennethwfk | 0:fc777b6c55d6 | 58 | Sensors->hts221.GetHumidity((float *)&HUMIDITY); |
kennethwfk | 0:fc777b6c55d6 | 59 | Sensors->lps25h.GetPressure((float *)&PRESSURE); |
kennethwfk | 0:fc777b6c55d6 | 60 | |
kennethwfk | 0:fc777b6c55d6 | 61 | TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit |
kennethwfk | 0:fc777b6c55d6 | 62 | TEMPERATURE_K = (TEMPERATURE_C + 273.15f); //Convert the temperature from Celsius to Kelvin |
kennethwfk | 0:fc777b6c55d6 | 63 | pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K); |
kennethwfk | 0:fc777b6c55d6 | 64 | pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY); |
kennethwfk | 0:fc777b6c55d6 | 65 | pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); |
kennethwfk | 0:fc777b6c55d6 | 66 | |
kennethwfk | 0:fc777b6c55d6 | 67 | pc.printf("\r\n"); |
kennethwfk | 0:fc777b6c55d6 | 68 | |
kennethwfk | 0:fc777b6c55d6 | 69 | measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 70 | } |
kennethwfk | 0:fc777b6c55d6 | 71 | __wfi(); |
kennethwfk | 0:fc777b6c55d6 | 72 | } |
kennethwfk | 0:fc777b6c55d6 | 73 | } |