mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
95 lines
1.4 KiB
C++
95 lines
1.4 KiB
C++
//
|
|
// FILE: DMM.cpp
|
|
// AUTHOR: Rob Tillaart
|
|
// VERSION: 0.1.1
|
|
// DATE: 2022-12-19
|
|
// PURPOSE: Library for a DMM class for Arduino UNO.
|
|
// URL: https://github.com/RobTillaart/DMM
|
|
|
|
|
|
#include "DMM.h"
|
|
|
|
|
|
DMM::DMM()
|
|
{
|
|
}
|
|
|
|
|
|
void DMM::begin(uint8_t pin, float maxVoltage, uint16_t maxSteps)
|
|
{
|
|
_pin = pin;
|
|
_maxVoltage = maxVoltage;
|
|
_maxSteps = maxSteps;
|
|
_voltageStep = _maxVoltage/_maxSteps;
|
|
}
|
|
|
|
|
|
void DMM::setMaxVoltage(float maxVoltage)
|
|
{
|
|
_maxVoltage = maxVoltage;
|
|
_voltageStep = _maxVoltage/_maxSteps;
|
|
}
|
|
|
|
|
|
float DMM::getMaxVoltage()
|
|
{
|
|
return _maxVoltage;
|
|
}
|
|
|
|
|
|
void DMM::setGain(float factor)
|
|
{
|
|
_gain = factor;
|
|
}
|
|
|
|
|
|
float DMM::getGain()
|
|
{
|
|
return _gain;
|
|
}
|
|
|
|
|
|
float DMM::readVolts(uint8_t times)
|
|
{
|
|
uint32_t total = 0;
|
|
if (times == 0) times = 1;
|
|
for (int i = 0; i < times; i++)
|
|
{
|
|
total += analogRead(_pin);
|
|
}
|
|
float v = (total * _voltageStep) / times;
|
|
|
|
if (_gain != 1.0) v *= _gain;
|
|
return v;
|
|
}
|
|
|
|
|
|
float DMM::readMilliVolts(uint8_t times)
|
|
{
|
|
return readVolts(times) * 1000.0;
|
|
}
|
|
|
|
|
|
float DMM::readNoise(uint8_t times)
|
|
{
|
|
uint16_t maxV = 0;
|
|
uint16_t minV = _maxSteps;
|
|
|
|
if (times == 0) times = 1;
|
|
for (int i = 0; i < times; i++)
|
|
{
|
|
uint16_t x = analogRead(_pin);
|
|
if (x > maxV) maxV = x;
|
|
if (x < minV) minV = x;
|
|
}
|
|
float v = ((maxV - minV) * _voltageStep) / times;
|
|
|
|
if (_gain != 1.0) v *= _gain;
|
|
return v;
|
|
}
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|