Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sensor test sketch
- for more information see http://www.ladyada.net/make/logshield/lighttemp.html
- */
- #define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
- //TMP36 Pin Variables
- int tempPin = A0; //the analog pin the TMP36's Vout (sense) pin is connected to
- //the resolution is 10 mV / degree centigrade with a
- //500 mV offset to allow for negative temperatures
- unsigned long tempReading; // the analog reading from the sensor
- bool flag = 0; // For telling if we were on or off before
- void setup(void) {
- // We'll send debugging information via the Serial monitor
- Serial.begin(115200);
- pinMode(2, OUTPUT);
- // If you want to set the aref to something other than 5v
- analogReference(EXTERNAL);
- }
- void loop(void) {
- tempReading = 0;
- for(int i = 0; i < 1000; i++){
- tempReading += analogRead(tempPin);
- }
- tempReading /= 1000; // Average a few reads.
- // converting that reading to voltage, which is based off the reference voltage
- float voltage = tempReading * aref_voltage;
- voltage /= 1024.0;
- // now print out the temperature
- float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
- //to degrees ((volatge - 500mV) times 100)
- // now convert to Fahrenheight
- float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
- Serial.println(temperatureF);
- if (temperatureF > 95) {
- digitalWrite(2, HIGH);
- if (flag == 0){
- flag = 1;
- Serial.println("fan_turned_on");
- }
- } else if (temperatureF < 90) {
- digitalWrite(2, LOW);
- if (flag == 1){
- flag = 0;
- Serial.println("fan_turned_off");
- }
- }
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement