diff --git a/include/controller.h b/include/controller.h new file mode 100644 index 0000000..c5099a2 --- /dev/null +++ b/include/controller.h @@ -0,0 +1,17 @@ +/** + * @file controller.h + * @author Ollo + * @brief + * @version 0.1 + * @date 2024-11-28 + * + * @copyright Copyright (c) 2024 + * + */ +#ifndef FANLEDCTL_PINS +#define FANLEDCTL_PINS + +#define FAN_PIN 12 +#define SIGNAL_PIN 13 + +#endif /* End FANLEDCTL_PINS */ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..f2554ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,80 @@ #include +#include "controller.h" -// put function declarations here: -int myFunction(int, int); +#define DELAY_TIME 10000 // time between measurements [ms] +#define MIN_FAN_SPEED_PERCENT 24 // minimum fan speed [%] +#define MIN_TEMP 25 // turn fan off below [deg C] +#define MAX_TEMP 40 // turn fan to full speed above [deg C] + +/** + * @brief Called once at start + * Initialize hardware pins + */ void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + + Serial.begin(115200); + + pinMode(FAN_PIN, OUTPUT); + pinMode(SIGNAL_PIN, INPUT); } +/** + * @brief Get the Fan Speed in round per minutes + * + * @return int + */ +int getFanSpeedRpm() { + int highTime = pulseIn(SIGNAL_PIN, HIGH); + int lowTime = pulseIn(SIGNAL_PIN, LOW); + int period = highTime + lowTime; + if (period == 0) { + return 0; + } + float freq = 1000000.0 / (float)period; + return (freq * 60.0) / 2.0; // two cycles per revolution +} + +/** + * @brief Set the Fan Speed + * + * @param p expected percentage + */ +void setFanSpeedPercent(int p) { + int value = (p / 100.0) * 255; + analogWrite(FAN_PIN, value); +} + +/** + * @brief Endless loop + * (is called as fast as possible) + */ void loop() { - // put your main code here, to run repeatedly: -} + float temp = /* FIXME bme.readTemperature(); */ + Serial.print("Temperature is "); + Serial.print(temp); + Serial.println(" deg C"); -// put function definitions here: -int myFunction(int x, int y) { - return x + y; -} \ No newline at end of file + int fanSpeedPercent, actualFanSpeedRpm; + + if (temp < MIN_TEMP) { + fanSpeedPercent = 0; + } else if (temp > MAX_TEMP) { + fanSpeedPercent = 100; + } else { + fanSpeedPercent = (100 - MIN_FAN_SPEED_PERCENT) * (temp - MIN_TEMP) / (MAX_TEMP - MIN_TEMP) + MIN_FAN_SPEED_PERCENT; + } + + Serial.print("Setting fan speed to "); + Serial.print(fanSpeedPercent); + Serial.println(" %"); + setFanSpeedPercent(fanSpeedPercent); + + actualFanSpeedRpm = getFanSpeedRpm(); + Serial.print("Fan speed is "); + Serial.print(actualFanSpeedRpm); + Serial.println(" RPM"); + + Serial.println(); + delay(DELAY_TIME); +}