per-sensor calibration, indicator leds
This commit is contained in:
parent
6fec798425
commit
2470935e11
1 changed files with 91 additions and 73 deletions
96
drop.ino
96
drop.ino
|
|
@ -1,49 +1,43 @@
|
|||
#include <Arduino.h>
|
||||
#include "helpers.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
/// a connected pot
|
||||
typedef struct {
|
||||
PotConfig &config;
|
||||
|
||||
PotConfig *config;
|
||||
|
||||
unsigned long last_watering;
|
||||
// anything changing per pot goes here
|
||||
} Pot;
|
||||
|
||||
Pot pots[length(POT_CONFIGS)];
|
||||
/// all connected pots
|
||||
Pot pots[POT_COUNT];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(6, OUTPUT); //Enable
|
||||
pinMode(5, OUTPUT); //Puls
|
||||
pinMode(13, OUTPUT); //Status LED
|
||||
pinMode(4, OUTPUT); //Enable6 halten
|
||||
pinMode(2, OUTPUT); //Puls5-
|
||||
pinMode(3, OUTPUT); //Direction4-
|
||||
|
||||
// link pots to their config
|
||||
for (unsigned int i = 0; i < length(pots); i++) {
|
||||
pots[i].config = POT_CONFIGS[i];
|
||||
|
||||
// TODO maybe set pinMode for valve and sensor pins?
|
||||
}
|
||||
pinMode(PUMP_LED_PIN, OUTPUT);
|
||||
pinMode(PUMP_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for (unsigned int i = 0; i < length(pots); i++) {
|
||||
Serial.println("LOOP");
|
||||
|
||||
for (unsigned int i = 0; i < POT_COUNT; i++) {
|
||||
Serial.print("Pot ");
|
||||
Serial.println(i);
|
||||
per_pot(pots[i]);
|
||||
}
|
||||
|
||||
|
||||
Serial.println("");
|
||||
delay(LOOP_DELAY_MS);
|
||||
}
|
||||
|
||||
void per_pot(Pot &pot) {
|
||||
Serial.println(pot.config.name);
|
||||
int percentage = get_humidity(pot.config->sensor);
|
||||
if (percentage < 0) {
|
||||
Serial.println(F("sensor not connected"));
|
||||
return;
|
||||
}
|
||||
|
||||
int sensorVal = analogRead(pot.config.sensor_pin);
|
||||
int percentage = humidity_percentage(sensorVal);
|
||||
Serial.print(sensorVal);
|
||||
Serial.print(F(" -> "));
|
||||
Serial.print(percentage);
|
||||
Serial.println(F("%"));
|
||||
|
||||
|
|
@ -54,7 +48,7 @@ void per_pot(Pot &pot) {
|
|||
} else if (percentage < MIN_HUMIDITY_PERCENT) {
|
||||
Serial.println(F("too dry -> watering"));
|
||||
water_pot(pot);
|
||||
} else if (pot.last_watering + MAX_WATERING_INTERVAL_MS < millis()){
|
||||
} else if (pot.last_watering + MAX_WATERING_INTERVAL_MS < millis()) {
|
||||
Serial.println(F("not been watered for a long time -> watering"));
|
||||
water_pot(pot);
|
||||
} else {
|
||||
|
|
@ -63,37 +57,45 @@ void per_pot(Pot &pot) {
|
|||
}
|
||||
|
||||
void water_pot(Pot &pot) {
|
||||
open_valve(pot.config.valve_pin);
|
||||
start_pump();
|
||||
digitalWrite(pot.config->led_pin, HIGH);
|
||||
|
||||
set_valve(pot.config->valve_pin, HIGH);
|
||||
set_pump(HIGH);
|
||||
|
||||
delay(WATER_TIME_MS);
|
||||
stop_pump();
|
||||
close_valve(pot.config.valve_pin);
|
||||
pot.last_watering = millis();
|
||||
|
||||
set_pump(LOW);
|
||||
set_valve(pot.config->valve_pin, LOW);
|
||||
|
||||
digitalWrite(pot.config->led_pin, LOW);
|
||||
}
|
||||
|
||||
int humidity_percentage(int sensorValue) {
|
||||
// Sensor has a range of 236 to 520
|
||||
/// get humidity sensor value as a percentage
|
||||
int get_humidity(Sensor &sensor) {
|
||||
// take average of multiple measurements
|
||||
int sensorVal {0};
|
||||
for (unsigned int i = 0; i < MEASUREMENT_COUNT; i++) {
|
||||
sensorVal += analogRead(sensor.pin);
|
||||
Serial.println(sensorVal);
|
||||
delay(MEASUREMENT_DELAY);
|
||||
}
|
||||
|
||||
sensorVal /= MEASUREMENT_COUNT;
|
||||
|
||||
// Sensor has a range of e.g. 236 to 520
|
||||
// We want to translate this to a scale or 0% to 100%
|
||||
// More info: https://www.arduino.cc/reference/en/language/functions/math/map/
|
||||
return map(sensorValue, SENSOR_CALIBRATION_WET, SENSOR_CALIBRATION_DRY, 100, 0);
|
||||
return map(sensorVal, sensor.calibration_wet, sensor.calibration_dry, 100, 0);
|
||||
}
|
||||
|
||||
void start_pump() {
|
||||
// TODO start pump
|
||||
void set_pump(uint8_t state) {
|
||||
digitalWrite(PUMP_LED_PIN, state);
|
||||
digitalWrite(PUMP_PIN, state);
|
||||
delay(PUMP_DELAY_MS);
|
||||
}
|
||||
|
||||
void stop_pump() {
|
||||
// TODO stop pump
|
||||
delay(PUMP_DELAY_MS);
|
||||
}
|
||||
|
||||
void open_valve(int valve) {
|
||||
// TODO open valve
|
||||
delay(VALVE_DELAY_MS);
|
||||
}
|
||||
|
||||
void close_valve(int valve) {
|
||||
// TODO close valve
|
||||
void set_valve(uint8_t valve, uint8_t state) {
|
||||
digitalWrite(valve, state);
|
||||
delay(VALVE_DELAY_MS);
|
||||
}
|
||||
Loading…
Reference in a new issue