-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperature_Controller.ino
More file actions
135 lines (116 loc) · 3.19 KB
/
Temperature_Controller.ino
File metadata and controls
135 lines (116 loc) · 3.19 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// shady nikooei
// --- Pin Definitions ---
// LM35 Temperature Sensor Pin
const int TEMP_SENSOR_PIN = A0;
// Control LEDs
const int FAN_LED_PIN = 8;
const int HEATER_LED_PIN = 9;
// Seven Segment Display Pins (A, B, C, D, E, F, G)
const int SEG_A = 2;
const int SEG_B = 3;
const int SEG_C = 4;
const int SEG_D = 5;
const int SEG_E = 6;
const int SEG_F = 7;
const int SEG_G = 13;
// Digit Selector Pins for the two 7-segment displays
const int DIGIT_UNITS_PIN = 11;
const int DIGIT_TENS_PIN = 12;
// --- Global Variables & Constants ---
// Patterns for displaying digits 0-9 on a common cathode 7-segment display
byte digitPatterns[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};
// State variables for hysteresis logic
bool isFanOn = false;
bool isHeaterOn = false;
void setup() {
Serial.begin(9600);
// Set all segment and control pins to OUTPUT mode
int outputPins[] = {
SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G,
DIGIT_UNITS_PIN, DIGIT_TENS_PIN,
FAN_LED_PIN, HEATER_LED_PIN
};
for (int pin : outputPins) {
pinMode(pin, OUTPUT);
}
}
void loop() {
float temperature = readTemperature();
int tempInt = (int)temperature;
//Serial.print("Temperature: ");
//Serial.println(temperature);
Serial.println(temperature);
applyControlLogic(temperature);
// Display the temperature repeatedly for a short duration to ensure stable visibility
unsigned long startTime = millis();
while (millis() - startTime < 200) {
displayTemperature(tempInt);
}
}
/**
* Reads the raw analog value from the LM35 sensor and converts it to Celsius.
*/
float readTemperature() {
int sensorValue = analogRead(TEMP_SENSOR_PIN);
float voltage = sensorValue * (5.0 / 1023.0);
float tempCelsius = voltage * 100.0;
return tempCelsius;
}
/**
* Applies the hysteresis logic to control the fan and heater LEDs.
*/
void applyControlLogic(float temp) {
// Fan control logic
if (temp > 30) {
isFanOn = true;
} else if (temp < 25) {
isFanOn = false;
}
digitalWrite(FAN_LED_PIN, isFanOn);
// Heater control logic
if (temp < 20) {
isHeaterOn = true;
} else if (temp > 25) {
isHeaterOn = false;
}
digitalWrite(HEATER_LED_PIN, isHeaterOn);
}
/**
* Displays an integer temperature on the two 7-segment displays
* by rapidly switching between the tens and units digit.
*/
void displayTemperature(int temp) {
if (temp < 0 || temp > 99) return;
int tens = temp / 10;
int units = temp % 10;
// Show tens digit
digitalWrite(DIGIT_UNITS_PIN, LOW);
digitalWrite(DIGIT_TENS_PIN, HIGH);
showDigit(tens);
delay(5);
// Show units digit
digitalWrite(DIGIT_TENS_PIN, LOW);
digitalWrite(DIGIT_UNITS_PIN, HIGH);
showDigit(units);
delay(5);
}
/**
* Sets the segment pins to display a single digit (0-9).
*/
void showDigit(int digit) {
int segmentPins[] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G};
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digitPatterns[digit][i]);
}
}