hw+sw works, now comes the hard part
This commit is contained in:
parent
9ad1857a64
commit
06efe8bb02
34
config.hpp
34
config.hpp
|
@ -20,54 +20,56 @@ constexpr unsigned int SECOND = 1000;
|
||||||
constexpr unsigned int MINUTE = SECOND * 60;
|
constexpr unsigned int MINUTE = SECOND * 60;
|
||||||
constexpr unsigned int HOUR = MINUTE * 60;
|
constexpr unsigned int HOUR = MINUTE * 60;
|
||||||
|
|
||||||
constexpr unsigned int MEASUREMENT_COUNT = 1;
|
constexpr unsigned int MEASUREMENT_COUNT = 3;
|
||||||
constexpr int MEASUREMENT_DELAY = 10;
|
constexpr int MEASUREMENT_DELAY = 100;
|
||||||
|
|
||||||
/// Pots will always be watered below this humidity
|
/// Pots will always be watered below this humidity
|
||||||
constexpr int MIN_HUMIDITY_PERCENT = 10;
|
constexpr int MIN_HUMIDITY_PERCENT = 30;
|
||||||
|
|
||||||
/// Pots will never be watered above this humidity
|
/// Pots will never be watered above this humidity
|
||||||
constexpr int MAX_HUMIDITY_PERCENT = 70;
|
constexpr int MAX_HUMIDITY_PERCENT = 70;
|
||||||
|
|
||||||
/// the amount of time a valve needs to open/close
|
/// the amount of time a valve needs to open/close
|
||||||
constexpr int VALVE_DELAY_MS = 1 * SECOND;
|
constexpr int VALVE_DELAY_MS = 1 * SECOND / 4;
|
||||||
|
|
||||||
/// the amount of time the pump needs to start/stop
|
/// the amount of time the pump needs to start/stop
|
||||||
constexpr int PUMP_DELAY_MS = 1 * SECOND;
|
constexpr int PUMP_DELAY_MS = 1 * SECOND / 4;
|
||||||
|
|
||||||
/// the amount of time to water the plot (valve open and pump running)
|
/// the amount of time to water the plot (valve open and pump running)
|
||||||
constexpr int WATER_TIME_MS = 3 * SECOND;
|
constexpr int WATER_TIME_MS = 1 * SECOND;
|
||||||
|
|
||||||
/// how long to wait between loops
|
/// how long to wait between loops
|
||||||
constexpr int LOOP_DELAY_MS = 1 * SECOND; // TODO set higher once it is working
|
constexpr int LOOP_DELAY_MS = 1 * SECOND; // TODO set higher once it is working
|
||||||
|
|
||||||
/// minimum amount of time to wait between watering each pot, even when minimum wetness has been reached
|
/// minimum amount of time to wait between watering each pot, even when minimum wetness has been reached
|
||||||
constexpr int MIN_WATERING_INTERVAL_MS = 1 * SECOND; // TODO set higher once it is working
|
constexpr int MIN_WATERING_INTERVAL_MS = 10 * SECOND; // TODO set higher once it is working
|
||||||
|
|
||||||
/// maximum amount of time to wait between watering each pot, as long as maximum wetness has not been reaached
|
/// maximum amount of time to wait between watering each pot, as long as maximum wetness has not been reaached
|
||||||
constexpr int MAX_WATERING_INTERVAL_MS = 24 * HOUR;
|
constexpr int MAX_WATERING_INTERVAL_MS = 24 * HOUR;
|
||||||
|
|
||||||
constexpr uint8_t PUMP_PIN = 22;
|
constexpr uint8_t PUMP_PIN = 7;
|
||||||
constexpr uint8_t PUMP_LED_PIN = 23;
|
constexpr uint8_t PUMP_LED_PIN = 3;
|
||||||
|
|
||||||
|
constexpr uint8_t OK_LED_PIN = 2;
|
||||||
|
|
||||||
/// Per-pot configuration
|
/// Per-pot configuration
|
||||||
constexpr PotConfig POT_CONFIGS[] = {
|
constexpr PotConfig POT_CONFIGS[] = {
|
||||||
{
|
{
|
||||||
.valve_pin = 2,
|
.valve_pin = 23,
|
||||||
.led_pin = 42,
|
.led_pin = 22,
|
||||||
.sensor = {
|
.sensor = {
|
||||||
.pin = A0,
|
.pin = A0,
|
||||||
.calibration_dry = 520,
|
.calibration_dry = 540,
|
||||||
.calibration_wet = 236,
|
.calibration_wet = 250,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.valve_pin = 3,
|
.valve_pin = 25,
|
||||||
.led_pin = 43,
|
.led_pin = 24,
|
||||||
.sensor = {
|
.sensor = {
|
||||||
.pin = A1,
|
.pin = A1,
|
||||||
.calibration_dry = 520,
|
.calibration_dry = 520,
|
||||||
.calibration_wet = 236,
|
.calibration_wet = 100,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
55
drop.ino
55
drop.ino
|
@ -17,40 +17,52 @@ void setup() {
|
||||||
pinMode(PUMP_LED_PIN, OUTPUT);
|
pinMode(PUMP_LED_PIN, OUTPUT);
|
||||||
pinMode(PUMP_PIN, OUTPUT);
|
pinMode(PUMP_PIN, OUTPUT);
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
pinMode(OK_LED_PIN, OUTPUT);
|
||||||
|
|
||||||
|
set_pump(false);
|
||||||
|
|
||||||
// link pots to their config
|
// link pots to their config
|
||||||
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
||||||
Pot *pot = &pots[i];
|
Pot *pot = &pots[i];
|
||||||
|
|
||||||
pot->config = &POT_CONFIGS[i];
|
pot->config = &POT_CONFIGS[i];
|
||||||
|
pot->last_watering = 0;
|
||||||
|
|
||||||
pinMode(pot->config->valve_pin, OUTPUT);
|
pinMode(pot->config->valve_pin, OUTPUT);
|
||||||
pinMode(pot->config->led_pin, OUTPUT);
|
pinMode(pot->config->led_pin, OUTPUT);
|
||||||
|
|
||||||
|
set_valve(pot->config->valve_pin, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
delay(LOOP_DELAY_MS);
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
Serial.println("LOOP");
|
Serial.println("LOOP");
|
||||||
|
|
||||||
|
digitalWrite(OK_LED_PIN, HIGH);
|
||||||
|
delay(100);
|
||||||
|
digitalWrite(OK_LED_PIN, LOW);
|
||||||
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
||||||
Serial.print("Pot ");
|
Serial.print("Pot ");
|
||||||
Serial.println(i);
|
Serial.println(i);
|
||||||
per_pot(pots[i]);
|
per_pot(pots[i]);
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
delay(LOOP_DELAY_MS);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void per_pot(Pot &pot) {
|
void per_pot(Pot &pot) {
|
||||||
int percentage = get_humidity(pot.config->sensor);
|
if (pot.last_watering + MIN_WATERING_INTERVAL_MS > millis()) {
|
||||||
Serial.print(percentage);
|
Serial.println(F("watered recently -> not watering"));
|
||||||
Serial.println(F("%"));
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int percentage = get_humidity(pot.config->sensor);
|
||||||
if (percentage > MAX_HUMIDITY_PERCENT) {
|
if (percentage > MAX_HUMIDITY_PERCENT) {
|
||||||
Serial.println(F("too wet -> not watering"));
|
Serial.println(F("too wet -> not watering"));
|
||||||
} else if (pot.last_watering + MIN_WATERING_INTERVAL_MS > millis()) {
|
|
||||||
Serial.println(F("watered recently -> not watering"));
|
|
||||||
} else if (percentage < MIN_HUMIDITY_PERCENT) {
|
} else if (percentage < MIN_HUMIDITY_PERCENT) {
|
||||||
Serial.println(F("too dry -> watering"));
|
Serial.println(F("too dry -> watering"));
|
||||||
water_pot(pot);
|
water_pot(pot);
|
||||||
|
@ -87,25 +99,40 @@ int get_humidity(Sensor &sensor) {
|
||||||
int sensorVal {0};
|
int sensorVal {0};
|
||||||
for (unsigned int i = 0; i < MEASUREMENT_COUNT; i++) {
|
for (unsigned int i = 0; i < MEASUREMENT_COUNT; i++) {
|
||||||
sensorVal += analogRead(sensor.pin);
|
sensorVal += analogRead(sensor.pin);
|
||||||
|
Serial.print(F("Measurement "));
|
||||||
|
Serial.print(i);
|
||||||
|
Serial.print(F(" = "));
|
||||||
Serial.println(sensorVal);
|
Serial.println(sensorVal);
|
||||||
delay(MEASUREMENT_DELAY);
|
delay(MEASUREMENT_DELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
sensorVal /= MEASUREMENT_COUNT;
|
sensorVal /= MEASUREMENT_COUNT;
|
||||||
|
Serial.print(F("Average: "));
|
||||||
|
Serial.println(sensorVal);
|
||||||
|
|
||||||
// Sensor has a range of e.g. 236 to 520
|
// Sensor has a range of e.g. 236 to 520
|
||||||
// We want to translate this to a scale or 0% to 100%
|
// We want to translate this to a scale or 0% to 100%
|
||||||
// More info: https://www.arduino.cc/reference/en/language/functions/math/map/
|
// More info: https://www.arduino.cc/reference/en/language/functions/math/map/
|
||||||
return map(sensorVal, sensor.calibration_wet, sensor.calibration_dry, 100, 0);
|
sensorVal = map(sensorVal, sensor.calibration_wet, sensor.calibration_dry, 100, 0);
|
||||||
|
Serial.print(F("Humidity: "));
|
||||||
|
Serial.print(sensorVal);
|
||||||
|
Serial.println(F("%"));
|
||||||
|
return sensorVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_pump(uint8_t state) {
|
void set_pump(bool on) {
|
||||||
digitalWrite(PUMP_LED_PIN, state);
|
Serial.print("setting pump to state ");
|
||||||
digitalWrite(PUMP_PIN, state);
|
Serial.println(on);
|
||||||
|
digitalWrite(PUMP_LED_PIN, on ? HIGH : LOW);
|
||||||
|
digitalWrite(PUMP_PIN, on ? LOW : HIGH);
|
||||||
delay(PUMP_DELAY_MS);
|
delay(PUMP_DELAY_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_valve(uint8_t valve, uint8_t state) {
|
void set_valve(uint8_t valve, bool open) {
|
||||||
digitalWrite(valve, state);
|
Serial.print("setting pump on pin ");
|
||||||
|
Serial.print(valve);
|
||||||
|
Serial.print(" to state ");
|
||||||
|
Serial.println(open);
|
||||||
|
digitalWrite(valve, open ? LOW : HIGH);
|
||||||
delay(VALVE_DELAY_MS);
|
delay(VALVE_DELAY_MS);
|
||||||
}
|
}
|
Loading…
Reference in a new issue