-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex03.ino
82 lines (71 loc) · 2.36 KB
/
ex03.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Arduino Engineering Lab
// Exercise n. 03
// define constants
const int sensor_pin = A0;
const float baseline = 20.0;
const int led_pins[] = {2, 3, 4}; // array of LED pins
const int no_pins = sizeof(led_pins) / sizeof(int); // number of pins
const int wait_next = 2000; // milliseconds between two acquisitions
// initialisation
void setup()
{
// open a serial connection
Serial.begin(9600);
// set the digital pins as outputs using a for loop
for (int cnt = 0; cnt < no_pins - 1; cnt++)
{
pinMode(led_pins[cnt], OUTPUT);
digitalWrite(led_pins[cnt], LOW);
}
}
// main loop function
void loop()
{
// read the sensor value from ADC on AnalogIn pin 0
int sensor_value = analogRead(sensor_pin);
// send the 10-bit sensor value out the serial port
Serial.print("Sensor value: ");
Serial.print(sensor_value);
// convert the ADC reading to voltage
float voltage = (sensor_value / 1024.0) * 5;
// send the voltage level out the serial port
Serial.print(", Voltage: ");
Serial.print(voltage);
// convert voltage to temperature (500 mV offset)
float temperature = (voltage - 0.5) * 100;
// send the temperaure out the serial port
Serial.print(", Degrees in Celsius: ");
Serial.println(temperature);
if (temperature < baseline)
{
// temperature less than 20°C
digitalWrite(led_pins[0], LOW); // OFF
digitalWrite(led_pins[1], LOW); // OFF
digitalWrite(led_pins[2], LOW); // OFF
}
else if (temperature > baseline + 2 &&
temperature < baseline + 8)
{
// temperature between 22°C and 28°C
digitalWrite(led_pins[0], HIGH); // ON
digitalWrite(led_pins[1], LOW); // OFF
digitalWrite(led_pins[2], LOW); // OFF
}
else if (temperature > baseline + 8 &&
temperature < baseline + 12)
{
// temperature between 28°C and 32°C
digitalWrite(led_pins[0], HIGH); // ON
digitalWrite(led_pins[1], HIGH); // ON
digitalWrite(led_pins[2], LOW); // OFF
}
else if (temperature > baseline + 12)
{
// temperature greater than 32°C
digitalWrite(led_pins[0], HIGH); // ON
digitalWrite(led_pins[1], HIGH); // ON
digitalWrite(led_pins[2], HIGH); // ON
}
// wait 2s
delay(wait_next);
}