commit 6864a37ed5c3a933bcb5f03dea098d7359df9c8d Author: Ano-sys Date: Tue Apr 1 14:47:35 2025 +0200 Initial push diff --git a/FanControlArduino.ino b/FanControlArduino.ino new file mode 100644 index 0000000..a6406de --- /dev/null +++ b/FanControlArduino.ino @@ -0,0 +1,57 @@ +#define PIN_RPM 9 +#define PIN_PWM 10 + +volatile unsigned int pulseCount = 0; +const unsigned int pulsesPerRevolution = 2; + +unsigned long previousMillis = 0; +unsigned int rpm = 0; + +void rpmISR() { + pulseCount++; +} + +int getSpeedForTemperature(){ + static int i = 0; + switch(i++ % 5){ + case 0: return 100; + case 1: return 140; + case 2: return 180; + case 3: return 220; + case 4: return 255; + } + return 255; +} + +void setup() { + Serial.begin(115200); + + pinMode(PIN_RPM, INPUT_PULLUP); + pinMode(PIN_PWM, OUTPUT); + + attachInterrupt(digitalPinToInterrupt(PIN_RPM), rpmISR, FALLING); + digitalWrite(PIN_PWM, HIGH); +} + +void loop() { + uint8_t speed = getSpeedForTemperature(); + unsigned long currentMillis = millis(); + if(currentMillis - previousMillis >= 1000){ + noInterrupts(); + unsigned int count = pulseCount; + pulseCount = 0; + interrupts(); + + rpm = (count * 60) / pulsesPerRevolution; + + Serial.println("Current Voltage: " + String(speed)); + Serial.println("Current RPM: " + String(rpm)); + Serial.println(); + + previousMillis = currentMillis; + } + + analogWrite(PIN_PWM, speed); + + delay(1000); +}