2012-10-12 19:23:48 +02:00
|
|
|
/*
|
|
|
|
Arduino Starter Kit example
|
2017-07-12 19:58:31 +02:00
|
|
|
Project 3 - Love-O-Meter
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
This sketch is written to accompany Project 3 in the
|
|
|
|
Arduino Starter Kit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
Parts required:
|
2013-10-21 09:58:40 +02:00
|
|
|
1 TMP36 temperature sensor
|
2012-10-12 19:23:48 +02:00
|
|
|
3 red LEDs
|
|
|
|
3 220 ohm resistors
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
Created 13 September 2012
|
|
|
|
by Scott Fitzgerald
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/starterKit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example code is part of the public domain
|
2012-10-12 19:23:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// named constant for the pin the sensor is connected to
|
|
|
|
const int sensorPin = A0;
|
2017-07-12 19:58:31 +02:00
|
|
|
// room temperature in Celsius
|
2012-10-12 19:23:48 +02:00
|
|
|
const float baselineTemp = 20.0;
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void setup() {
|
2012-10-12 19:23:48 +02:00
|
|
|
// open a serial connection to display values
|
|
|
|
Serial.begin(9600);
|
|
|
|
// set the LED pins as outputs
|
|
|
|
// the for() loop saves some extra coding
|
2013-10-21 09:58:40 +02:00
|
|
|
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
|
|
|
|
pinMode(pinNumber, OUTPUT);
|
2012-10-12 19:23:48 +02:00
|
|
|
digitalWrite(pinNumber, LOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void loop() {
|
|
|
|
// read the value on AnalogIn pin 0
|
2012-10-12 19:23:48 +02:00
|
|
|
// and store it in a variable
|
|
|
|
int sensorVal = analogRead(sensorPin);
|
|
|
|
|
|
|
|
// send the 10-bit sensor value out the serial port
|
|
|
|
Serial.print("sensor Value: ");
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.print(sensorVal);
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// convert the ADC reading to voltage
|
2013-10-21 09:58:40 +02:00
|
|
|
float voltage = (sensorVal / 1024.0) * 5.0;
|
2012-10-12 19:23:48 +02:00
|
|
|
|
|
|
|
// Send the voltage level out the Serial port
|
|
|
|
Serial.print(", Volts: ");
|
|
|
|
Serial.print(voltage);
|
|
|
|
|
|
|
|
// convert the voltage to temperature in degrees C
|
|
|
|
// the sensor changes 10 mV per degree
|
|
|
|
// the datasheet says there's a 500 mV offset
|
2017-07-12 19:58:31 +02:00
|
|
|
// ((voltage - 500 mV) times 100)
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.print(", degrees C: ");
|
2012-10-12 19:23:48 +02:00
|
|
|
float temperature = (voltage - .5) * 100;
|
|
|
|
Serial.println(temperature);
|
|
|
|
|
|
|
|
// if the current temperature is lower than the baseline
|
|
|
|
// turn off all LEDs
|
2015-06-12 13:10:49 +02:00
|
|
|
if (temperature < baselineTemp + 2) {
|
2012-10-12 19:23:48 +02:00
|
|
|
digitalWrite(2, LOW);
|
|
|
|
digitalWrite(3, LOW);
|
|
|
|
digitalWrite(4, LOW);
|
2013-10-21 09:58:40 +02:00
|
|
|
} // if the temperature rises 2-4 degrees, turn an LED on
|
|
|
|
else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) {
|
2012-10-12 19:23:48 +02:00
|
|
|
digitalWrite(2, HIGH);
|
|
|
|
digitalWrite(3, LOW);
|
|
|
|
digitalWrite(4, LOW);
|
2013-10-21 09:58:40 +02:00
|
|
|
} // if the temperature rises 4-6 degrees, turn a second LED on
|
|
|
|
else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) {
|
2012-10-12 19:23:48 +02:00
|
|
|
digitalWrite(2, HIGH);
|
|
|
|
digitalWrite(3, HIGH);
|
|
|
|
digitalWrite(4, LOW);
|
|
|
|
} // if the temperature rises more than 6 degrees, turn all LEDs on
|
2013-10-21 09:58:40 +02:00
|
|
|
else if (temperature >= baselineTemp + 6) {
|
2012-10-12 19:23:48 +02:00
|
|
|
digitalWrite(2, HIGH);
|
|
|
|
digitalWrite(3, HIGH);
|
|
|
|
digitalWrite(4, HIGH);
|
|
|
|
}
|
|
|
|
delay(1);
|
|
|
|
}
|