Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE HTU21D mbed-rtos mbed wave_player
main.cpp@12:a5be4d71addc, 2022-12-09 (annotated)
- Committer:
- dajbaptis
- Date:
- Fri Dec 09 04:25:45 2022 +0000
- Revision:
- 12:a5be4d71addc
- Parent:
- 11:0309bef74ba8
ece 4180 project
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dajbaptis | 12:a5be4d71addc | 1 | /** Sample program to read temperature and humidity |
| dajbaptis | 12:a5be4d71addc | 2 | * |
| dajbaptis | 12:a5be4d71addc | 3 | * @author Alex Lipford |
| dajbaptis | 12:a5be4d71addc | 4 | * Georgia Institute of Technology |
| dajbaptis | 12:a5be4d71addc | 5 | * ECE 4180 Embeded Systems Design |
| dajbaptis | 12:a5be4d71addc | 6 | * Professor Hamblen |
| dajbaptis | 12:a5be4d71addc | 7 | * 10/19/2014 |
| dajbaptis | 12:a5be4d71addc | 8 | * |
| dajbaptis | 12:a5be4d71addc | 9 | * @section LICENSE |
| dajbaptis | 12:a5be4d71addc | 10 | * ---------------------------------------------------------------------------- |
| dajbaptis | 12:a5be4d71addc | 11 | * "THE BEER-WARE LICENSE" (Revision 42): |
| dajbaptis | 12:a5be4d71addc | 12 | * <alexlipford@gmail.com> wrote this file. As long as you retain this notice you |
| dajbaptis | 12:a5be4d71addc | 13 | * can do whatever you want with this stuff. If we meet some day, and you think |
| dajbaptis | 12:a5be4d71addc | 14 | * this stuff is worth it, you can buy me a beer in return. |
| dajbaptis | 12:a5be4d71addc | 15 | * ---------------------------------------------------------------------------- |
| dajbaptis | 12:a5be4d71addc | 16 | * |
| dajbaptis | 12:a5be4d71addc | 17 | * |
| dajbaptis | 12:a5be4d71addc | 18 | * @section DESCRIPTION |
| dajbaptis | 12:a5be4d71addc | 19 | * |
| dajbaptis | 12:a5be4d71addc | 20 | * Honeywell HTU21D Humidity and Temperature sensor. |
| dajbaptis | 12:a5be4d71addc | 21 | * |
| dajbaptis | 12:a5be4d71addc | 22 | * Datasheet, specs, and information: |
| dajbaptis | 12:a5be4d71addc | 23 | * |
| dajbaptis | 12:a5be4d71addc | 24 | * https://www.sparkfun.com/products/12064 |
| dajbaptis | 12:a5be4d71addc | 25 | */ |
| dajbaptis | 12:a5be4d71addc | 26 | |
| emilmont | 1:491820ee784d | 27 | #include "mbed.h" |
| dajbaptis | 12:a5be4d71addc | 28 | #include <string> |
| dajbaptis | 12:a5be4d71addc | 29 | #include <stdlib.h> |
| dajbaptis | 12:a5be4d71addc | 30 | #include "HTU21D.h" |
| dajbaptis | 12:a5be4d71addc | 31 | #include "uLCD_4DGL.h" |
| dajbaptis | 12:a5be4d71addc | 32 | #include "SongPlayer.h" |
| dajbaptis | 12:a5be4d71addc | 33 | #include <math.h> |
| mbed_official | 11:0309bef74ba8 | 34 | #include "rtos.h" |
| dajbaptis | 12:a5be4d71addc | 35 | |
| dajbaptis | 12:a5be4d71addc | 36 | Serial pc(USBTX, USBRX); |
| dajbaptis | 12:a5be4d71addc | 37 | |
| dajbaptis | 12:a5be4d71addc | 38 | DigitalOut reset(p26); |
| emilmont | 1:491820ee784d | 39 | DigitalOut led1(LED1); |
| dajbaptis | 12:a5be4d71addc | 40 | DigitalOut led4(LED4); |
| dajbaptis | 12:a5be4d71addc | 41 | Timer t; |
| dajbaptis | 12:a5be4d71addc | 42 | |
| dajbaptis | 12:a5be4d71addc | 43 | AnalogIn CO2(p18); |
| dajbaptis | 12:a5be4d71addc | 44 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; |
| dajbaptis | 12:a5be4d71addc | 45 | HTU21D temphumid(p9, p10); //Temp humid sensor || SDA, SCL |
| dajbaptis | 12:a5be4d71addc | 46 | int sample_ftemp; |
| dajbaptis | 12:a5be4d71addc | 47 | int sample_ctemp; |
| dajbaptis | 12:a5be4d71addc | 48 | int sample_humid; |
| dajbaptis | 12:a5be4d71addc | 49 | float sample_co2; |
| dajbaptis | 12:a5be4d71addc | 50 | |
| dajbaptis | 12:a5be4d71addc | 51 | float note[7]= {700.0,0,700.0,0,700.0,0,700.0 |
| dajbaptis | 12:a5be4d71addc | 52 | }; |
| dajbaptis | 12:a5be4d71addc | 53 | float duration[7]= {0.48,0.48,0.48,0.48,0.48,0.48, 0.0 |
| dajbaptis | 12:a5be4d71addc | 54 | }; |
| dajbaptis | 12:a5be4d71addc | 55 | |
| dajbaptis | 12:a5be4d71addc | 56 | |
| emilmont | 1:491820ee784d | 57 | |
| dajbaptis | 12:a5be4d71addc | 58 | void sensorsthread(void const *args) { |
| dajbaptis | 12:a5be4d71addc | 59 | while (1) { |
| dajbaptis | 12:a5be4d71addc | 60 | sample_ftemp = temphumid.sample_ftemp(); |
| dajbaptis | 12:a5be4d71addc | 61 | sample_ctemp = temphumid.sample_ctemp(); |
| dajbaptis | 12:a5be4d71addc | 62 | sample_humid = temphumid.sample_humid(); |
| dajbaptis | 12:a5be4d71addc | 63 | sample_co2 = CO2.read()*1000; |
| dajbaptis | 12:a5be4d71addc | 64 | Thread::wait(1000); |
| dajbaptis | 12:a5be4d71addc | 65 | } |
| dajbaptis | 12:a5be4d71addc | 66 | } |
| dajbaptis | 12:a5be4d71addc | 67 | |
| dajbaptis | 12:a5be4d71addc | 68 | |
| dajbaptis | 12:a5be4d71addc | 69 | void lcdthread(void const *args) { |
| dajbaptis | 12:a5be4d71addc | 70 | uLCD.background_color(BLACK); |
| dajbaptis | 12:a5be4d71addc | 71 | uLCD.cls(); |
| dajbaptis | 12:a5be4d71addc | 72 | uLCD.baudrate(30000); //jack up baud rate to max for fast display |
| dajbaptis | 12:a5be4d71addc | 73 | |
| dajbaptis | 12:a5be4d71addc | 74 | while (1) { |
| dajbaptis | 12:a5be4d71addc | 75 | uLCD.cls(); |
| dajbaptis | 12:a5be4d71addc | 76 | uLCD.locate(0,2); |
| dajbaptis | 12:a5be4d71addc | 77 | uLCD.color(WHITE); |
| dajbaptis | 12:a5be4d71addc | 78 | uLCD.printf("Temperature: %d F\n\r", sample_ftemp); |
| dajbaptis | 12:a5be4d71addc | 79 | uLCD.printf("Temperature: %d C\n\r", sample_ctemp); |
| dajbaptis | 12:a5be4d71addc | 80 | uLCD.printf("Humidity: %d %%\n\r", sample_humid); |
| dajbaptis | 12:a5be4d71addc | 81 | uLCD.printf("Gas: %0.2f ppm \n\r", sample_co2); |
| dajbaptis | 12:a5be4d71addc | 82 | if (sample_co2 > 80){ uLCD.printf("Evacuate! CO is high!");} |
| dajbaptis | 12:a5be4d71addc | 83 | uLCD.printf("\n\r"); |
| dajbaptis | 12:a5be4d71addc | 84 | wait(0.5); |
| dajbaptis | 12:a5be4d71addc | 85 | |
| mbed_official | 11:0309bef74ba8 | 86 | Thread::wait(1000); |
| emilmont | 1:491820ee784d | 87 | } |
| emilmont | 1:491820ee784d | 88 | } |
| dajbaptis | 12:a5be4d71addc | 89 | |
| emilmont | 1:491820ee784d | 90 | int main() { |
| dajbaptis | 12:a5be4d71addc | 91 | |
| dajbaptis | 12:a5be4d71addc | 92 | sample_ftemp = temphumid.sample_ftemp(); |
| dajbaptis | 12:a5be4d71addc | 93 | sample_ctemp = temphumid.sample_ctemp(); |
| dajbaptis | 12:a5be4d71addc | 94 | sample_humid = temphumid.sample_humid(); |
| dajbaptis | 12:a5be4d71addc | 95 | |
| dajbaptis | 12:a5be4d71addc | 96 | pc.printf("Begin Thread\n"); |
| dajbaptis | 12:a5be4d71addc | 97 | Thread t1(sensorsthread); |
| dajbaptis | 12:a5be4d71addc | 98 | Thread t3(lcdthread); |
| dajbaptis | 12:a5be4d71addc | 99 | |
| dajbaptis | 12:a5be4d71addc | 100 | SongPlayer mySpeaker(p26); |
| dajbaptis | 12:a5be4d71addc | 101 | |
| dajbaptis | 12:a5be4d71addc | 102 | |
| dajbaptis | 12:a5be4d71addc | 103 | while(1) { |
| dajbaptis | 12:a5be4d71addc | 104 | if (sample_co2 > 80) { |
| dajbaptis | 12:a5be4d71addc | 105 | mySpeaker.PlaySong(note,duration); |
| dajbaptis | 12:a5be4d71addc | 106 | } |
| dajbaptis | 12:a5be4d71addc | 107 | |
| dajbaptis | 12:a5be4d71addc | 108 | Thread::wait(1000); |
| emilmont | 1:491820ee784d | 109 | } |
| emilmont | 1:491820ee784d | 110 | } |
| dajbaptis | 12:a5be4d71addc | 111 |