-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureReader.ino
More file actions
81 lines (64 loc) · 1.84 KB
/
TemperatureReader.ino
File metadata and controls
81 lines (64 loc) · 1.84 KB
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
// Ishaan Shete
// TrickFire Robotics Cooling Team
// Team: Clayton, Pasha, Davin, Reyhan, Abdullahi
// 03/16/2025
const int fanPin1 = 2;
const int fanPin2 = 4;
const int fanPin3 = 7;
const int fanPin4 = 8;
const int tempPin1 = A0;
const int tempPin2 = A1;
bool fanOn = false;
unsigned long fanStartTime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
pinMode(fanPin3, OUTPUT);
pinMode(fanPin4, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
float temperature = readTemperature(); //in celcius
Serial.print("Temperature: ");
Serial.println(temperature);
if(temperature > 40.0) {
turnFanOn();
fanStartTime = millis(); //start the timer
}
//keep fan on until temperature drops below 20degrees C
if(fanOn) {
if(millis() - fanStartTime >= fanRunTime || temperature <= 20.0) {
turnFanOff();
fanOn = false;
}
}
delay(500);
}
//few added functions
void turnFanOn() {
digitalWrite(fanPin1, HIGH);
digitalWrite(fanPin2, HIGH);
digitalWrite(fanPin3, HIGH);
digitalWrite(fanPin4, HIGH);
fanOn = true;
Serial.println("Fan ON");
}
void turnFanOff() {
digitalWrite(fanPin1, LOW);
digitalWrite(fanPin2, LOW);
digitalWrite(fanPin3, LOW);
digitalWrite(fanPin4, LOW);
fanOn = false;
Serial.println("Fan OFF");
}
float readTemperature() {
int sensorValue1 = analogRead(tempPin1);
float voltage1 = sensorValue1 * (5.0 / 1023.0); //convert temp1 to voltage
float temp1 = (voltage1 - 0.5) * 100.0; //convert temp1 to degrees C
int sensorValue2 = analogRead(tempPin2);
float voltage2 = sensorValue2 * (5.0/1023.0); //convert temp2 to voltage
float temp2 = (voltage2 - 0.5) * 100.0; //convert temp2 to degrees C
return (temp1 + temp2) / 2.0; //return avg. from both temp sensors
}