![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Use a touchscreen display to select which sensor measurement to display
Dependencies: mbed DisplayModule24_demo_day10
tmp.cpp@8:a7b7edb66de5, 2017-01-17 (annotated)
- Committer:
- ldelaney17
- Date:
- Tue Jan 17 21:28:13 2017 +0000
- Revision:
- 8:a7b7edb66de5
- Parent:
- 7:b275c76de4ba
final commit; cleaned up debug code; added comments; set 10s ticker period
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ldelaney17 | 3:4cd5469a146f | 1 | #include "mbed.h" |
ldelaney17 | 3:4cd5469a146f | 2 | #include "tmp.h" |
ldelaney17 | 3:4cd5469a146f | 3 | //reused code from miniproject day 7. Not reinventing the wheel. |
ldelaney17 | 3:4cd5469a146f | 4 | |
ldelaney17 | 3:4cd5469a146f | 5 | |
ldelaney17 | 3:4cd5469a146f | 6 | //Code for Temperature Sensor |
ldelaney17 | 3:4cd5469a146f | 7 | I2C * tempsensor; |
ldelaney17 | 3:4cd5469a146f | 8 | |
ldelaney17 | 3:4cd5469a146f | 9 | void temp_init(){ |
ldelaney17 | 3:4cd5469a146f | 10 | tempsensor = &connection; |
ldelaney17 | 3:4cd5469a146f | 11 | char config_t[3]; |
ldelaney17 | 3:4cd5469a146f | 12 | config_t[0]=0x01; |
ldelaney17 | 3:4cd5469a146f | 13 | config_t[1]=0x60; //configures data byte 1 |
ldelaney17 | 3:4cd5469a146f | 14 | config_t[2] = 0xA0; //configures data byte 2 |
ldelaney17 | 3:4cd5469a146f | 15 | tempsensor->write(temp_addr, config_t,3); |
ldelaney17 | 3:4cd5469a146f | 16 | config_t[0]=0x00; |
ldelaney17 | 3:4cd5469a146f | 17 | tempsensor->write(temp_addr, config_t,1); |
ldelaney17 | 3:4cd5469a146f | 18 | } |
ldelaney17 | 3:4cd5469a146f | 19 | float get_temp(){ |
ldelaney17 | 7:b275c76de4ba | 20 | pc.printf("in call to temp\r\n"); |
ldelaney17 | 3:4cd5469a146f | 21 | float temp; |
ldelaney17 | 3:4cd5469a146f | 22 | char temp_read[2]; |
ldelaney17 | 7:b275c76de4ba | 23 | wait(0.05); //shorter wait time |
ldelaney17 | 3:4cd5469a146f | 24 | tempsensor->read(temp_addr, temp_read, 2); |
ldelaney17 | 7:b275c76de4ba | 25 | |
ldelaney17 | 3:4cd5469a146f | 26 | temp = 0.0625*(((temp_read[0]<<8))+temp_read[1]>>4);//calculate and convert data |
ldelaney17 | 3:4cd5469a146f | 27 | #ifdef DEBUG_MODE |
ldelaney17 | 3:4cd5469a146f | 28 | pc.printf("Temp = %.2f degC\n\r", temp); |
ldelaney17 | 3:4cd5469a146f | 29 | #endif |
ldelaney17 | 3:4cd5469a146f | 30 | return temp; |
ldelaney17 | 4:3d5abdf9ad0e | 31 | } |
ldelaney17 | 4:3d5abdf9ad0e | 32 | |
ldelaney17 | 8:a7b7edb66de5 | 33 | // gets the average of n (default 5) temperature readings |
ldelaney17 | 4:3d5abdf9ad0e | 34 | float get_avg_temp(int n){ |
ldelaney17 | 4:3d5abdf9ad0e | 35 | if (n <= 0) |
ldelaney17 | 4:3d5abdf9ad0e | 36 | return 0; |
ldelaney17 | 4:3d5abdf9ad0e | 37 | float count = 0; |
ldelaney17 | 4:3d5abdf9ad0e | 38 | for (int i = 0; i < n; i++){ |
ldelaney17 | 4:3d5abdf9ad0e | 39 | count += get_temp(); |
ldelaney17 | 4:3d5abdf9ad0e | 40 | } |
ldelaney17 | 4:3d5abdf9ad0e | 41 | return count / n; |
ldelaney17 | 4:3d5abdf9ad0e | 42 | } |
ldelaney17 | 4:3d5abdf9ad0e | 43 |