arduino-photometrics/lib/sensor/photoresistance_ohm_retrieval.cpp
2025-11-26 16:30:22 +01:00

29 lines
899 B
C++
Executable file

#include <Arduino.h>
#include "photoresistance_ohm_retrieval.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){
Serial.println("FATAL: VCC too high !");
while (true); // bloque le programme
}
if (current > mega2560_max_current){
Serial.println("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;
}