-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhp_server_fan_controller.ino
82 lines (69 loc) · 1.87 KB
/
hp_server_fan_controller.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
const int fan_pin = 2;
const int alert_pin = 3;
const int pwm_in_pin = 4;
const int pwm_out_pin = 5;
const int led_pin = LED_BUILTIN;
int fan_state = 0;
unsigned long start = 0;
unsigned long went_high = 0;
unsigned long went_low = 0;
int pwm_in_value = 0;
int pwm_out_value = 100;
void setup() {
pinMode(fan_pin, INPUT);
pinMode(alert_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
pinMode(pwm_in_pin, INPUT);
pinMode(pwm_out_pin, OUTPUT);
digitalWrite(led_pin, LOW);
Serial.begin(115200);
analogWrite(pwm_out_pin, pwm_out_value);
}
void loop() {
// Set the fan speed high or low depending on whether server PWM signal is
// above a threshold.
pwm_in_value = pulseIn(pwm_in_pin, HIGH);
if (pwm_in_value > 40) {
if (pwm_out_value != 1000) {
pwm_out_value = 1000;
analogWrite(pwm_out_pin, pwm_out_value);
}
}
else {
if (pwm_out_value != 100) {
pwm_out_value = 100;
analogWrite(pwm_out_pin, pwm_out_value);
}
}
Serial.print("Server PWM width: ");
Serial.println(pwm_in_value);
Serial.print("Output PWM value: ");
Serial.println(pwm_out_value);
went_high = 0;
went_low = 0;
start = millis();
// Read the fan pulses for a period of time to make sure it goes high and
// low. If it doesn't change then then fan has stopped moving and we need
// to alert the server by setting alert_pin high.
while (true) {
fan_state = digitalRead(fan_pin);
if (fan_state == HIGH) {
went_high += 1;
} else {
went_low += 1;
}
if (millis() > start + 500) {
if (went_high > 1000 && went_low > 1000) {
digitalWrite(alert_pin, LOW);
digitalWrite(led_pin, LOW);
Serial.println("Status: OK\n");
}
else {
digitalWrite(alert_pin, HIGH);
digitalWrite(led_pin, HIGH);
Serial.println("STATUS: FAILURE\n");
}
break;
}
}
}