2022-12-04 11:55:37 -05:00
|
|
|
//
|
|
|
|
// FILE: rain.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-12-06 10:57:55 -05:00
|
|
|
// VERSION: 0.1.2
|
2022-12-04 11:55:37 -05:00
|
|
|
// DATE: 2021-12-03
|
|
|
|
// PURPOSE: Arduino library for a rain sensor
|
|
|
|
// URL: https://github.com/RobTillaart/RAIN
|
|
|
|
// https://www.tinytronics.nl/shop/nl/sensoren/vloeistof/regensensor
|
|
|
|
|
|
|
|
|
|
|
|
#include "rain.h"
|
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
RAIN::RAIN(uint8_t port, uint8_t powerPin)
|
2022-12-04 11:55:37 -05:00
|
|
|
{
|
|
|
|
_port = port;
|
2022-12-06 10:57:55 -05:00
|
|
|
_powerPin = powerPin;
|
2022-12-04 11:55:37 -05:00
|
|
|
_maxVoltage = 5;
|
|
|
|
_maxSteps = 1023;
|
2022-12-06 10:57:55 -05:00
|
|
|
_dryRefVoltage = _maxVoltage;
|
2022-12-04 11:55:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RAIN::begin(float maxVoltage, uint16_t maxSteps)
|
|
|
|
{
|
2022-12-06 10:57:55 -05:00
|
|
|
_maxVoltage = maxVoltage;
|
|
|
|
_maxSteps = maxSteps;
|
|
|
|
_mVstep = _maxVoltage / _maxSteps;
|
|
|
|
_dryRefVoltage = _maxVoltage;
|
|
|
|
|
|
|
|
if (_powerPin != 255)
|
|
|
|
{
|
|
|
|
pinMode(_powerPin, OUTPUT);
|
|
|
|
powerOn();
|
|
|
|
}
|
2022-12-04 11:55:37 -05:00
|
|
|
read();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float RAIN::raw(uint8_t times)
|
|
|
|
{
|
|
|
|
if (times == 0) times = 1;
|
|
|
|
float sum = 0;
|
2022-12-06 10:57:55 -05:00
|
|
|
|
|
|
|
powerOn();
|
2022-12-04 11:55:37 -05:00
|
|
|
for (int i = 0; i < times; i++)
|
|
|
|
{
|
|
|
|
sum += analogRead(_port);
|
|
|
|
}
|
2022-12-06 10:57:55 -05:00
|
|
|
powerOff();
|
|
|
|
|
2022-12-04 11:55:37 -05:00
|
|
|
if (times > 1) sum /= times;
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float RAIN::read(uint8_t times)
|
|
|
|
{
|
2022-12-06 05:26:48 -05:00
|
|
|
_previous = _voltage;
|
2022-12-04 11:55:37 -05:00
|
|
|
_voltage = raw(times) * _mVstep;
|
|
|
|
return _voltage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
void RAIN::setDryReference(float dryRef)
|
|
|
|
{
|
|
|
|
_dryRefVoltage = dryRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float RAIN::getDryReference()
|
|
|
|
{
|
|
|
|
return _dryRefVoltage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 11:55:37 -05:00
|
|
|
float RAIN::percentage()
|
|
|
|
{
|
2022-12-06 10:57:55 -05:00
|
|
|
float p = 100.0 - (_voltage * 100.0 / _dryRefVoltage);
|
|
|
|
if (p < 0) p = 0;
|
|
|
|
return p;
|
2022-12-04 11:55:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 05:26:48 -05:00
|
|
|
float RAIN::delta()
|
|
|
|
{
|
|
|
|
return _voltage - _previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RAIN::setLevel(uint8_t nr, uint16_t milliVolts)
|
2022-12-04 11:55:37 -05:00
|
|
|
{
|
|
|
|
if (nr == 0) return false;
|
|
|
|
if (nr > 4) return false;
|
2022-12-06 05:26:48 -05:00
|
|
|
_level[nr] = milliVolts;
|
2022-12-04 11:55:37 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t RAIN::getLevel()
|
|
|
|
{
|
2022-12-06 05:26:48 -05:00
|
|
|
uint16_t value = _voltage * 1000;
|
2022-12-04 11:55:37 -05:00
|
|
|
for (int index = 4; index > 0; index--)
|
|
|
|
{
|
2022-12-06 05:26:48 -05:00
|
|
|
if (value >= _level[index]) return index;
|
2022-12-04 11:55:37 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 10:57:55 -05:00
|
|
|
void RAIN::powerOn()
|
|
|
|
{
|
|
|
|
if (_powerPin != 255)
|
|
|
|
{
|
|
|
|
digitalWrite(_powerPin, HIGH);
|
|
|
|
delayMicroseconds(100); // time to stabilize, adjust if needed.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RAIN::powerOff()
|
|
|
|
{
|
|
|
|
if (_powerPin != 255)
|
|
|
|
{
|
|
|
|
digitalWrite(_powerPin, LOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 11:55:37 -05:00
|
|
|
// -- END OF FILE --
|
|
|
|
|