Ollo 2024-11-28 21:44:39 +01:00
parent 4ed8b150eb
commit ab1325f07f
2 changed files with 89 additions and 10 deletions

17
include/controller.h Normal file
View File

@ -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 */

View File

@ -1,18 +1,80 @@
#include <Arduino.h> #include <Arduino.h>
#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() { 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() { 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");
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;
} }
// put function definitions here: Serial.print("Setting fan speed to ");
int myFunction(int x, int y) { Serial.print(fanSpeedPercent);
return x + y; Serial.println(" %");
setFanSpeedPercent(fanSpeedPercent);
actualFanSpeedRpm = getFanSpeedRpm();
Serial.print("Fan speed is ");
Serial.print(actualFanSpeedRpm);
Serial.println(" RPM");
Serial.println();
delay(DELAY_TIME);
} }