Thermometer (using thermistor) that creates beeps of increasing pitch and pulse rate as temperature rises above nominal. Demonstrates use of #include for your own header files, and #define for constants.
Dependencies: mbed
main.cpp@0:59f09202ccbb, 2017-10-05 (annotated)
- Committer:
- rossatmsoe
- Date:
- Thu Oct 05 20:02:27 2017 +0000
- Revision:
- 0:59f09202ccbb
Thermometer (using thermistor) that creates beeps of increasing pitch and pulse rate as temperature rises above nominal. Demonstrates use of #include for your own header files, and #define for constants.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rossatmsoe | 0:59f09202ccbb | 1 | /* Audible Thermometer |
rossatmsoe | 0:59f09202ccbb | 2 | |
rossatmsoe | 0:59f09202ccbb | 3 | Creates beeps of increasing pitch and pulse rate as the temperature |
rossatmsoe | 0:59f09202ccbb | 4 | rises above the nominal value. |
rossatmsoe | 0:59f09202ccbb | 5 | |
rossatmsoe | 0:59f09202ccbb | 6 | Hardware: 10k resistor from A0 to ground |
rossatmsoe | 0:59f09202ccbb | 7 | 10k nominal thermistor from A0 to 3V3 |
rossatmsoe | 0:59f09202ccbb | 8 | Speaker from D9 to ground |
rossatmsoe | 0:59f09202ccbb | 9 | |
rossatmsoe | 0:59f09202ccbb | 10 | */ |
rossatmsoe | 0:59f09202ccbb | 11 | |
rossatmsoe | 0:59f09202ccbb | 12 | #include "mbed.h" |
rossatmsoe | 0:59f09202ccbb | 13 | #include "thermistor.h" // This file contains the temperature calculation |
rossatmsoe | 0:59f09202ccbb | 14 | |
rossatmsoe | 0:59f09202ccbb | 15 | #define NOMINAL_TEMP 75 // Change the nominal temperature here |
rossatmsoe | 0:59f09202ccbb | 16 | |
rossatmsoe | 0:59f09202ccbb | 17 | AnalogIn therm(A0); |
rossatmsoe | 0:59f09202ccbb | 18 | PwmOut speaker(D9); |
rossatmsoe | 0:59f09202ccbb | 19 | Serial pc(USBTX,USBRX); |
rossatmsoe | 0:59f09202ccbb | 20 | |
rossatmsoe | 0:59f09202ccbb | 21 | // This function will be defined in this file, main.cpp |
rossatmsoe | 0:59f09202ccbb | 22 | void beep(float freq, float duration); |
rossatmsoe | 0:59f09202ccbb | 23 | |
rossatmsoe | 0:59f09202ccbb | 24 | int main() |
rossatmsoe | 0:59f09202ccbb | 25 | { |
rossatmsoe | 0:59f09202ccbb | 26 | while(1) { |
rossatmsoe | 0:59f09202ccbb | 27 | |
rossatmsoe | 0:59f09202ccbb | 28 | // Calculate Temperature |
rossatmsoe | 0:59f09202ccbb | 29 | int tempF = temp_calc(therm); // This function is in thermistor.h |
rossatmsoe | 0:59f09202ccbb | 30 | |
rossatmsoe | 0:59f09202ccbb | 31 | // Calculate difference from nominal |
rossatmsoe | 0:59f09202ccbb | 32 | int difference = tempF-NOMINAL_TEMP; |
rossatmsoe | 0:59f09202ccbb | 33 | |
rossatmsoe | 0:59f09202ccbb | 34 | // If I have a fever... |
rossatmsoe | 0:59f09202ccbb | 35 | if (difference >= 1) { |
rossatmsoe | 0:59f09202ccbb | 36 | |
rossatmsoe | 0:59f09202ccbb | 37 | // Create a warning beep that gets higher in pitch and |
rossatmsoe | 0:59f09202ccbb | 38 | // more rapid as temperature increases |
rossatmsoe | 0:59f09202ccbb | 39 | float frequency = difference*100; |
rossatmsoe | 0:59f09202ccbb | 40 | float duration = .5/difference; |
rossatmsoe | 0:59f09202ccbb | 41 | |
rossatmsoe | 0:59f09202ccbb | 42 | // Call beeping function defined below |
rossatmsoe | 0:59f09202ccbb | 43 | beep(frequency,duration); |
rossatmsoe | 0:59f09202ccbb | 44 | |
rossatmsoe | 0:59f09202ccbb | 45 | wait(duration); |
rossatmsoe | 0:59f09202ccbb | 46 | } |
rossatmsoe | 0:59f09202ccbb | 47 | } |
rossatmsoe | 0:59f09202ccbb | 48 | } |
rossatmsoe | 0:59f09202ccbb | 49 | |
rossatmsoe | 0:59f09202ccbb | 50 | void beep(float freq, float duration) // Note that one of the variables |
rossatmsoe | 0:59f09202ccbb | 51 | { // in the note() function has the same |
rossatmsoe | 0:59f09202ccbb | 52 | speaker.period(1/freq); // as a variable in main(). |
rossatmsoe | 0:59f09202ccbb | 53 | speaker=.5; // That's ok, since they are local |
rossatmsoe | 0:59f09202ccbb | 54 | wait(duration); // to their respective functions. |
rossatmsoe | 0:59f09202ccbb | 55 | speaker=0; // This function also uses a global |
rossatmsoe | 0:59f09202ccbb | 56 | } // variable, speaker. |
rossatmsoe | 0:59f09202ccbb | 57 |