Debian Codegraph @

examples/10.StarterKit_BasicKit/p03_LoveOMeter/p03_LoveOMeter.ino

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