made pin 10 control a servo instead of the fan, still need to update the webpage to reflect that, and the stop makes it full and the full makes it stop atm
This commit is contained in:
+45
-29
@@ -1,48 +1,64 @@
|
||||
// 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_A_PIN = 11;
|
||||
const int FAN_B_PIN = 10;
|
||||
// Dual control: Pin 11 = PWM fan, Pin 10 = TT motor (via IRF520 module)
|
||||
// Send via Serial: a0–a9, af, as → fan | b0–b9, bf, bs → TT motor
|
||||
const int FAN_PIN = 11; // Regular PWM fan (can be directly on pin or via transistor)
|
||||
const int MOTOR_PIN = 10; // TT motor → IRF520 SIG terminal
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
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)");
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
pinMode(MOTOR_PIN, OUTPUT);
|
||||
|
||||
// Start both stopped
|
||||
analogWrite(FAN_PIN, 0);
|
||||
analogWrite(MOTOR_PIN, 0);
|
||||
|
||||
Serial.println("Dual controller ready");
|
||||
Serial.println("a0-a9/af/as = Fan | b0-b9/bf/bs = TT Motor");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available() < 2) return; // need fan + cmd
|
||||
if (Serial.available() < 2) return;
|
||||
|
||||
char fan = Serial.read();
|
||||
char cmd = Serial.read();
|
||||
char device = Serial.read(); // 'a'/'A' = fan, 'b'/'B' = motor
|
||||
char cmd = Serial.read();
|
||||
|
||||
// ----- 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; }
|
||||
// Select which pin we're controlling
|
||||
int pin;
|
||||
char name;
|
||||
if (device == 'a' || device == 'A') {
|
||||
pin = FAN_PIN;
|
||||
name = 'A';
|
||||
}
|
||||
else if (device == 'b' || device == 'B') {
|
||||
pin = MOTOR_PIN;
|
||||
name = 'B';
|
||||
}
|
||||
else {
|
||||
Serial.println("?");
|
||||
return;
|
||||
}
|
||||
|
||||
// ----- compute speed -----
|
||||
// Parse command
|
||||
int speed = 0;
|
||||
cmd = tolower(cmd);
|
||||
|
||||
if (cmd >= '0' && cmd <= '9') {
|
||||
speed = map(cmd - '0', 0, 9, 0, 255); // 0-9 → 0-255
|
||||
speed = map(cmd - '0', 0, 9, 70, 255); // ← IMPORTANT for TT motor!
|
||||
// 70 minimum → motor actually starts moving (adjust 60–90 if needed)
|
||||
}
|
||||
else if (cmd == 'f') speed = 255; // full
|
||||
else if (cmd == 's') speed = 0; // stop
|
||||
else {
|
||||
Serial.println("?");
|
||||
return;
|
||||
}
|
||||
else if (cmd == 'f') speed = 255;
|
||||
else if (cmd == 's') speed = 0;
|
||||
else { Serial.println("?"); return; }
|
||||
|
||||
// ----- apply -----
|
||||
analogWrite(*pin, speed);
|
||||
// Apply PWM
|
||||
analogWrite(pin, speed);
|
||||
|
||||
// ----- feedback (percent + which fan) -----
|
||||
// Feedback
|
||||
int percent = map(speed, 0, 255, 0, 100);
|
||||
Serial.print((fan == 'a' || fan == 'A') ? "A" : "B");
|
||||
Serial.print(name);
|
||||
Serial.print(percent);
|
||||
Serial.println("%");
|
||||
}
|
||||
Reference in New Issue
Block a user