Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2
3#include CONFIG_HEADER
4#include "aliases.h"
5
6#include "EEPROMConfig.h"
7#include "SerialLogger.h"
11
12// Beware: Lots of preprocessor fuckery to get conditional compilation based on config settings below
13
14// Humidity sensor
15#ifdef HUMIDISTAT_DHT
16DHT dht(config::PIN_DHT, DHT22);
17DHTHumiditySensor hs(&dht);
18#endif
19#ifdef HUMIDISTAT_SHT
20SHTSensor sht;
21SHTHumiditySensor hs(&sht);
22#endif
23
24// Thermistors
26 ThermistorReader(config::PIN_T1),
27 ThermistorReader(config::PIN_T2),
28 ThermistorReader(config::PIN_T3),
29 ThermistorReader(config::PIN_T4)
30};
31
32// Input
33VoltLadder voltLadder;
35
37
38// PWM frequency and resolution: MCU-dependent
39#if defined(ARDUINO_TEENSYLC)
40const uint8_t pwmRes = 16;
41#elif defined(ARDUINO_TEENSY40)
42const uint8_t pwmRes = 15;
43#else
44const uint8_t pwmRes = 8;
45#endif
46
47// Humidity controller
48#ifdef HUMIDISTAT_CONTROLLER_SINGLE
50SingleHumidistat humidistat(&hs, &eepromConfig.configStore, {{config::PIN_S1, config::PIN_S2}}, pwmRes);
51using cHumidistat = SingleHumidistat;
52#endif
53#ifdef HUMIDISTAT_CONTROLLER_CASCADE
54#include "sensor/FlowSensor.h"
56FlowSensor flowSensors[] = {FlowSensor(config::PIN_F1), FlowSensor(config::PIN_F2)};
57CascadeHumidistat humidistat(&hs, &eepromConfig.configStore, flowSensors, {config::PIN_S1, config::PIN_S2}, pwmRes);
58using cHumidistat = CascadeHumidistat;
59#endif
60
61// UI
62#ifdef HUMIDISTAT_UI_CHAR
63#include <LiquidCrystal.h>
64#include "ui/CharDisplayUI.h"
65LiquidCrystal liquidCrystal(config::PIN_LCD_RS, config::PIN_LCD_ENABLE, config::PIN_LCD_D0, config::PIN_LCD_D1,
66 config::PIN_LCD_D2, config::PIN_LCD_D3);
67CharDisplayUI ui(&liquidCrystal, &buttonReader, &humidistat, trs);
68#endif
69#ifdef HUMIDISTAT_UI_GRAPH
70#include <U8g2lib.h>
72
74
75#ifdef ARDUINO_TEENSYLC
76U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, config::PIN_LCD_CS);
77#endif
78#ifdef ARDUINO_TEENSY40
79U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, config::PIN_LCD_SCLK, config::PIN_LCD_MOSI, config::PIN_LCD_CS);
80#endif
82#endif
83
85
86void setup() {
87#ifdef ARDUINO_AVR_UNO
88 // Set PWM frequency on D3 and D11 to 490.20 Hz
89 // See: https://arduinoinfo.mywikis.net/wiki/Arduino-PWM-Frequency
90 TCCR2B = TCCR2B & B11111000 | B00000100;
91#endif
92#if defined(ARDUINO_TEENSYLC) || defined(ARDUINO_TEENSY40)
93 // Set PWM frequency to 250 Hz
94 analogWriteFrequency(config::PIN_S1, 500);
95 // Increase PWM resolution from default 8-bits
96 analogWriteResolution(pwmRes);
97#endif
98
99 hs.begin();
100 serialLogger.begin(config::serialRate);
101 ui.begin();
102}
103
104void loop() {
106 ui.update();
107 humidistat.update();
108 serialLogger.update();
109#ifdef HUMIDISTAT_UI_GRAPH
110 spr.update();
111#endif
112}
Read button state from a voltage ladder-style keypad.
void sample()
Sample the button signal. Call this in the main loop.
Control humidity using cascade PID: outer PID loop sets setpoints of two inner flow controllers,...
TUI for 16x2 character LCD. Holds a reference to a LiquidCrystal instance for writing to display....
Implementation of the HumiditySensor interface for the DHT22/AM2302 sensor.
Load/save an (internal) ConfigStore in EEPROM.
ConfigStore configStore
Read flow rate using a Omron D6F-P0010 MEMS flow sensor.
Definition FlowSensor.h:9
TUI for 128*64 px graphical display using U8g2. Holds references to a U8g2lib instance for writing to...
Implementation of the HumiditySensor interface for the Sensirion SHT85 sensor.
Logs humidistat data over serial.
'Runs' a setpoint profile.
Control humidity using PID by driving two solenoid valves. Adjust the public setpoint variable and ca...
Driver for thermistor thermometers.
const uint8_t pwmRes
Definition main.cpp:44
void setup()
Definition main.cpp:86
VoltLadder voltLadder
Definition main.cpp:33
SerialLogger< cHumidistat > serialLogger & humidistat
Definition main.cpp:84
ButtonReader buttonReader(config::PIN_BTN, &voltLadder)
ThermistorReader trs[]
Definition main.cpp:25
EEPROMConfig eepromConfig
Definition main.cpp:36
void loop()
Definition main.cpp:104
const uint32_t serialRate
Serial communication symbol rate (baud)
Definition config.h:33
uint16_t dt
Global interval for PID/logger (based on polling rate of sensor, in millis)