2022-12-04 11:55:37 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: rain.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-12-06 10:57:55 -05:00
|
|
|
// VERSION: 0.1.2
|
2022-12-04 11:55:37 -05:00
|
|
|
// DATE: 2022-11-23
|
|
|
|
// PURPOSE: Arduino library for rain sensor (analog).
|
|
|
|
// URL: https://github.com/RobTillaart/RAIN
|
|
|
|
// https://www.tinytronics.nl/shop/nl/sensoren/vloeistof/regensensor
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
#define RAIN_LIB_VERSION (F("0.1.2"))
|
2022-12-04 11:55:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
class RAIN
|
|
|
|
{
|
|
|
|
public:
|
2022-12-06 10:57:55 -05:00
|
|
|
// port = analogPort,
|
|
|
|
// powerPin is optional, 255 == not used => see readme.md
|
|
|
|
RAIN(uint8_t port, uint8_t powerPin = 255);
|
2022-12-04 11:55:37 -05:00
|
|
|
|
|
|
|
// set the ADC parameters
|
|
|
|
// can be changed runtime, e.g if voltage fluctuates.
|
|
|
|
bool begin(float maxVoltage, uint16_t maxSteps);
|
|
|
|
|
|
|
|
// returns steps
|
|
|
|
float raw(uint8_t times = 1);
|
|
|
|
// returns voltage
|
2022-12-06 10:57:55 -05:00
|
|
|
// the lower the voltage the wetter.
|
2022-12-04 11:55:37 -05:00
|
|
|
float read(uint8_t times = 1);
|
|
|
|
|
|
|
|
|
|
|
|
// ANALYSIS
|
2022-12-06 10:57:55 -05:00
|
|
|
// read the sensor when it is dry to get a reference (calibration).
|
|
|
|
// will be used by percentage(), can be used to setLevel().
|
|
|
|
void setDryReference(float dryRef);
|
|
|
|
float getDryReference();
|
|
|
|
|
|
|
|
// returns last read value as percentage of DryReference (if set).
|
|
|
|
// indicating wetness 0 == DRY 100 == WET
|
|
|
|
// percentage assumes / implies "linear" behaviour
|
2022-12-04 11:55:37 -05:00
|
|
|
float percentage();
|
2022-12-06 10:57:55 -05:00
|
|
|
|
|
|
|
// delta with respect to previous read().
|
2022-12-06 05:26:48 -05:00
|
|
|
float delta();
|
|
|
|
|
2022-12-04 11:55:37 -05:00
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
// level = 1..4
|
|
|
|
// level 0 == 0 Volt ==> WET)
|
|
|
|
// level 4 ==> DRY
|
2022-12-04 11:55:37 -05:00
|
|
|
// user is responsible that values are increasing voltages.
|
2022-12-06 05:26:48 -05:00
|
|
|
bool setLevel(uint8_t nr, uint16_t milliVolts);
|
2022-12-04 11:55:37 -05:00
|
|
|
uint8_t getLevel();
|
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
// will only work when set in constructor.
|
|
|
|
void powerOn();
|
|
|
|
void powerOff();
|
|
|
|
|
2022-12-04 11:55:37 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t _port;
|
2022-12-06 10:57:55 -05:00
|
|
|
uint8_t _powerPin = 255; // 255 means not set.
|
2022-12-04 11:55:37 -05:00
|
|
|
float _maxVoltage;
|
|
|
|
uint16_t _maxSteps;
|
|
|
|
float _mVstep;
|
|
|
|
float _voltage;
|
2022-12-06 10:57:55 -05:00
|
|
|
float _dryRefVoltage;
|
2022-12-06 05:26:48 -05:00
|
|
|
float _previous;
|
|
|
|
uint16_t _level[5] = { 0, 1000, 2000, 3000, 4000 }; // millivolts
|
2022-12-04 11:55:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|