arduino-photometrics/lib/sensor/photoresistance_ohm_retrieval.cpp

30 lines
885 B
C++
Executable file

#include <Arduino.h>
#include "photoresistance_ohm_retrieval.h"
#include "debug_config.h"
SensorOhm::SensorOhm(uint8_t pin_analog_read, float vcc, int32_t rFixed, float current)
: pin_analog_read(pin_analog_read), vcc(vcc), rFixed(rFixed), current(current) {
if (vcc > mega2560_max_vcc){
DLOGLN("FATAL: VCC too high !")
while (true);
}
if (current > mega2560_max_current){
DLOGLN("FATAL: Intensity too high !")
while (true);
}
}
void SensorOhm::setup() {
pinMode(pin_analog_read, INPUT);
}
int32_t SensorOhm::readResistance() {
// The circuit is connected in series.
// The resistance of the photoresistor can be calculated using the voltage divider circuits.
int raw = analogRead(pin_analog_read);
float vOut = (raw / 1023.0f) * vcc;
int32_t res = (int32_t)(rFixed * (vcc - vOut)) / vOut;
return res;
}