added functionality for a second fan on pin 10

This commit is contained in:
Lucca Pirovano
2025-11-15 19:12:31 -05:00
parent 81f4a6d1c0
commit 9646403938
3 changed files with 156 additions and 111 deletions
@@ -1,42 +1,48 @@
// Fan control via Serial - works with ultra-responsive Python script
// Pin 11 = PWM output to fan (use transistor/MOSFET/driver for high current!)
// Dual Fan control via Serial works with the Python Flask UI
// Pin 11 = Fan A (PWM) Pin 10 = Fan B (PWM)
// Use transistor/MOSFET/driver for high-current fans!
const int FAN_PIN = 11;
const int FAN_A_PIN = 11;
const int FAN_B_PIN = 10;
void setup() {
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
analogWrite(FAN_PIN, 0); // Start stopped
Serial.println("Fan controller ready. Use 0-9, f=full, s=stop");
pinMode(FAN_A_PIN, OUTPUT);
pinMode(FAN_B_PIN, OUTPUT);
analogWrite(FAN_A_PIN, 0);
analogWrite(FAN_B_PIN, 0);
Serial.println("Dual fan controller ready.");
Serial.println("Format: <fan><cmd> (a/b)(0-9fs)");
}
void loop() {
if (Serial.available() > 0) {
if (Serial.available() < 2) return; // need fan + cmd
char fan = Serial.read();
char cmd = Serial.read();
int speed = 0; // 0 to 255 for analogWrite
// ----- validate fan -----
const int *pin;
if (fan == 'a' || fan == 'A') pin = &FAN_A_PIN;
else if (fan == 'b' || fan == 'B') pin = &FAN_B_PIN;
else { Serial.println("?"); return; }
if (cmd >= '0' && cmd <= '9') {
speed = map(cmd - '0', 0, 9, 0, 255); // 0-9 → 0-255
}
else if (cmd == 'f' || cmd == 'F') {
speed = 255;
}
else if (cmd == 's' || cmd == 'S') {
speed = 0;
}
else {
// Ignore invalid commands, but optionally echo
Serial.print("?");
return;
}
// Apply speed
analogWrite(FAN_PIN, speed);
// Send feedback: percentage
int percent = map(speed, 0, 255, 0, 100);
Serial.print(percent);
Serial.println("%");
// ----- compute speed -----
int speed = 0;
cmd = tolower(cmd);
if (cmd >= '0' && cmd <= '9') {
speed = map(cmd - '0', 0, 9, 0, 255); // 0-9 → 0-255
}
else if (cmd == 'f') speed = 255;
else if (cmd == 's') speed = 0;
else { Serial.println("?"); return; }
// ----- apply -----
analogWrite(*pin, speed);
// ----- feedback (percent + which fan) -----
int percent = map(speed, 0, 255, 0, 100);
Serial.print((fan == 'a' || fan == 'A') ? "A" : "B");
Serial.print(percent);
Serial.println("%");
}