-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino-Uno
110 lines (90 loc) · 2.86 KB
/
Arduino-Uno
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
//Pinouts
int THERM_pin = 3;
int AC_pin = 9;
int PWM_pin = 3;
int CAR_pin = 8;
int CAR;
//Circuit Constants
float R2 = 200;
float A = .001454437;
float B = .0002335551;
float C = .0000000872799;
//Fan Settings
float MIN_fan = .15;
float MAX_fan = 1;
int MIN_temp = 82;
int MAX_temp = 92;
byte FAN_speed = 0;
//Calculation Settings
int ADC_depth = 1023; //This is the default depth of an arduino Uno, different boards may have different depths.
int PWM_depth = 255; //This is the default depth of a PWM driver for an arduino, this may change from board to board.
byte FAN_trim = 0;
byte ALG_trim = 0; //This is part of the learning algorithm that finds the optimal fan speed for your desired temperature range.
float COOLANT_temp;
float TEMP_prev;
byte FAN_speed_prev;
//DEBUGGING PINS
int CAR_on = 12;
int AC_on = 13;
void setup() {
//Set pin modes
pinMode(AC_pin,INPUT);
pinMode(CAR_pin,INPUT);
pinMode(PWM_pin,OUTPUT);
digitalWrite(CAR_pin,HIGH);
FAN_speed_prev=128;
//DEBUGGING
// Serial.begin(9600);
// pinMode(CAR_on,OUTPUT);
// pinMode(AC_on,OUTPUT);
}
double Thermister(int RawADC) {
double TEMP;
TEMP = ((1024*R2/RawADC) - R2);
TEMP = log(TEMP);
TEMP = 1 / (A + (B + (C * TEMP * TEMP ))* TEMP );
TEMP = TEMP - 273.15; // Convert Kelvin to Celcius
return TEMP;
}
void loop() {
CAR= digitalRead(CAR_pin);
digitalWrite(CAR_on,LOW);
digitalWrite(AC_on,LOW);
FAN_speed= FAN_speed_prev;
if(CAR == 0) {
// Serial.print("Car is On "); //DEBUGGING LINE
digitalWrite(CAR_on,HIGH);
COOLANT_temp = Thermister(analogRead(3));
//Serial.print(COOLANT_temp); //DEBUGGING LINE
//Serial.print(" C "); //DEBUGGING LINE
if(COOLANT_temp > MIN_temp) {
if(COOLANT_temp > MAX_temp) {
FAN_speed=MAX_fan*PWM_depth;
analogWrite(PWM_pin,FAN_speed);
delay(1000); }
else {
if (TEMP_prev < COOLANT_temp) {
FAN_speed = FAN_speed_prev++;
}
else if(TEMP_prev > COOLANT_temp) {
FAN_speed = FAN_speed_prev--;
}
else {
FAN_speed = FAN_speed_prev;}
//Serial.print(FAN_speed-FAN_speed_prev); //DEBUGGING LINE
//Serial.print(" TRIM "); //
analogWrite(PWM_pin,FAN_speed);
//Serial.print(FAN_speed+ALG_trim); //DEBUGGING LINE
//Serial.print(" Speed "); //DEBUGGING LINE
TEMP_prev = COOLANT_temp;
delay(1000);
}}
else {
FAN_speed = MIN_fan*PWM_depth;
analogWrite(PWM_pin,FAN_speed);
}}
else {
analogWrite(PWM_pin,0); }
//Serial.print(FAN_speed); //DEBUGGING LINE
//Serial.println(" bits"); //DEBUGGING LINE
}