source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/10.StarterKit_BasicKit/p03_LoveOMeter/p03_LoveOMeter.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 2.3 KB
Line 
1/*
2  Arduino Starter Kit example
3 Project 3  - Love-O-Meter
4
5 This sketch is written to accompany Project 3 in the
6 Arduino Starter Kit
7
8 Parts required:
9 1 TMP36 temperature sensor
10 3 red LEDs
11 3 220 ohm resistors
12
13 Created 13 September 2012
14 by Scott Fitzgerald
15
16 http://www.arduino.cc/starterKit
17
18 This example code is part of the public domain
19 */
20
21// named constant for the pin the sensor is connected to
22const int sensorPin = A0;
23// room temperature in Celcius
24const float baselineTemp = 20.0;
25
26void setup() {
27  // open a serial connection to display values
28  Serial.begin(9600);
29  // set the LED pins as outputs
30  // the for() loop saves some extra coding
31  for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
32    pinMode(pinNumber, OUTPUT);
33    digitalWrite(pinNumber, LOW);
34  }
35}
36
37void loop() {
38  // read the value on AnalogIn pin 0
39  // and store it in a variable
40  int sensorVal = analogRead(sensorPin);
41
42  // send the 10-bit sensor value out the serial port
43  Serial.print("sensor Value: ");
44  Serial.print(sensorVal);
45
46  // convert the ADC reading to voltage
47  float voltage = (sensorVal / 1024.0) * 5.0;
48
49  // Send the voltage level out the Serial port
50  Serial.print(", Volts: ");
51  Serial.print(voltage);
52
53  // convert the voltage to temperature in degrees C
54  // the sensor changes 10 mV per degree
55  // the datasheet says there's a 500 mV offset
56  // ((volatge - 500mV) times 100)
57  Serial.print(", degrees C: ");
58  float temperature = (voltage - .5) * 100;
59  Serial.println(temperature);
60
61  // if the current temperature is lower than the baseline
62  // turn off all LEDs
63  if (temperature < baselineTemp + 2) {
64    digitalWrite(2, LOW);
65    digitalWrite(3, LOW);
66    digitalWrite(4, LOW);
67  } // if the temperature rises 2-4 degrees, turn an LED on
68  else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) {
69    digitalWrite(2, HIGH);
70    digitalWrite(3, LOW);
71    digitalWrite(4, LOW);
72  } // if the temperature rises 4-6 degrees, turn a second LED on
73  else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) {
74    digitalWrite(2, HIGH);
75    digitalWrite(3, HIGH);
76    digitalWrite(4, LOW);
77  } // if the temperature rises more than 6 degrees, turn all LEDs on
78  else if (temperature >= baselineTemp + 6) {
79    digitalWrite(2, HIGH);
80    digitalWrite(3, HIGH);
81    digitalWrite(4, HIGH);
82  }
83  delay(1);
84}
Note: See TracBrowser for help on using the repository browser.