Cleans up debugging code

This commit is contained in:
rbaron 2021-10-09 09:33:02 +02:00
parent 79384cd6b2
commit 85643fe41e
3 changed files with 7 additions and 45 deletions

View file

@ -14,8 +14,7 @@
#define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28) #define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28)
// Deep sleep. // Deep sleep.
// #define PRST_DEEP_SLEEP_IN_SECONDS 2 #define PRST_DEEP_SLEEP_IN_SECONDS 300
#define PRST_DEEP_SLEEP_IN_SECONDS 2 * 3600
// Analog to digital converter (ADC). // Analog to digital converter (ADC).
// Prints out ADC debug info, such as the values read for battery and soil // Prints out ADC debug info, such as the values read for battery and soil
@ -31,7 +30,7 @@
// 1. Two most significant bits are set to 1; // 1. Two most significant bits are set to 1;
// 2. The remaining bits should not _all_ be set to 0; // 2. The remaining bits should not _all_ be set to 0;
// 2. The remaining bits should not _all_ be set to 1; // 2. The remaining bits should not _all_ be set to 1;
#define PRST_BLE_MAC_ADDR "f0:ca:f0:ca:00:10" #define PRST_BLE_MAC_ADDR "f0:ca:f0:ca:01:01"
#define PRST_BLE_ADV_NAME "prst" #define PRST_BLE_ADV_NAME "prst"
// Total time spend advertising. // Total time spend advertising.
#define PRST_BLE_ADV_TIME_IN_MS 1000 #define PRST_BLE_ADV_TIME_IN_MS 1000
@ -67,7 +66,6 @@
// Whether to produce debug messages for the LDR // Whether to produce debug messages for the LDR
#define PRST_ADC_PHOTO_DEBUG 0 #define PRST_ADC_PHOTO_DEBUG 0
// #endif // End of version-specific configuration.
#elif defined(PRST_VERSION_1_2_X) #elif defined(PRST_VERSION_1_2_X)
#define PRST_HAS_PHOTOTRANSISTOR 1 #define PRST_HAS_PHOTOTRANSISTOR 1
@ -75,7 +73,7 @@
#define PRST_PHOTO_V_PIN NRF_GPIO_PIN_MAP(0, 29) #define PRST_PHOTO_V_PIN NRF_GPIO_PIN_MAP(0, 29)
#define PRST_PHOTO_OUT_PIN NRF_GPIO_PIN_MAP(0, 2) #define PRST_PHOTO_OUT_PIN NRF_GPIO_PIN_MAP(0, 2)
#define PRST_ADC_PHOTO_DEBUG 1 #define PRST_ADC_PHOTO_DEBUG 0
#endif // End of version-specific configuration. #endif // End of version-specific configuration.

View file

@ -69,13 +69,9 @@ static void rtc_callback() {
uint16_t lux = 0; uint16_t lux = 0;
#if PRST_HAS_LDR || PRST_HAS_PHOTOTRANSISTOR #if PRST_HAS_LDR || PRST_HAS_PHOTOTRANSISTOR
nrf_gpio_pin_set(PRST_PHOTO_V_PIN); nrf_gpio_pin_set(PRST_PHOTO_V_PIN);
nrf_delay_ms(50);
// Just while debugging. prst_adc_photo_sensor_t photo_read = prst_adc_photo_read(batt_read.voltage);
while (1) { lux = photo_read.brightness;
nrf_delay_ms(1000);
prst_adc_photo_sensor_t photo_read = prst_adc_photo_read(batt_read.voltage);
lux = photo_read.brightness;
}
nrf_gpio_pin_clear(PRST_PHOTO_V_PIN); nrf_gpio_pin_clear(PRST_PHOTO_V_PIN);
#endif #endif

View file

@ -130,16 +130,10 @@ prst_adc_soil_moisture_t prst_adc_soil_read(double battery_voltage) {
prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) { prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) {
nrf_saadc_value_t raw_photo_output = nrf_saadc_value_t raw_photo_output =
sample_adc_channel(PRST_ADC_PHOTO_CHANNEL); MAX(0, sample_adc_channel(PRST_ADC_PHOTO_CHANNEL));
if (raw_photo_output < 0) {
raw_photo_output = 0;
}
prst_adc_photo_sensor_t ret; prst_adc_photo_sensor_t ret;
ret.raw = raw_photo_output; ret.raw = raw_photo_output;
ret.voltage = (3.6 * raw_photo_output) / (1 << PRST_ADC_RESOLUTION); ret.voltage = (3.6 * raw_photo_output) / (1 << PRST_ADC_RESOLUTION);
// ret.voltage = (2.4 * raw_photo_output) / (1 << PRST_ADC_RESOLUTION);
#if PRST_HAS_LDR #if PRST_HAS_LDR
// The photo resistor forms a voltage divider with a 10 kOhm resistor. // The photo resistor forms a voltage divider with a 10 kOhm resistor.
@ -165,37 +159,11 @@ prst_adc_photo_sensor_t prst_adc_photo_read(double battery_voltage) {
// its two terminals is controlled by how much light there is in the ambient. // its two terminals is controlled by how much light there is in the ambient.
// We measure that current by calculating the voltage across a resistor that // We measure that current by calculating the voltage across a resistor that
// is connected in series with the phototransistor. // is connected in series with the phototransistor.
// Some infor:
// - Not all lights are the same. The ALS-PT19 has different current
// responses for incandescent and fluorescent lights and it shows no values
// for sunlight. We have to make some compromises here, aiming for a
// "reasonable" middle ground through calibration.
// - ALS-PT19' minimum voltage is 2.5 V. Our CR2032 battery has a theoretical
// range of 2.0 V to 3.0 V, but in our usage pattern it seems to spend most of
// its life around 2.6 V (https://github.com/rbaron/b-parasite/issues/1). In
// my manual tests it seems we can usually go lower than that. So while we're
// pushing it a bit, we should be okay most of the time.
//
// In order to design the value of the series resistor, we assume Vcc = 2.5V,
// and we want the upper limit of light intensity to correspond to a value of
// Vout < 2.5 V - 0.4 V (saturation) = 2.1 V. Let's call this the "direct
// sunlight" voltage across our resistor. This direct sunlight voltage will
// appear when the phototransistor outputs the direct sunlight current.
//
// In short, what we want:
// A value of R_L such that Vout is < 2.1 V (but close) when the sensor is in
// direct sunlight. While Vcc is 2.5 V, R_L = 470 Ohm outputs Vout ~ 1.7V, so
// there's still some wiggle room for even more sunnier days.
//
// Another caveat: the datasheet shows that the current in the transistor is
// relatively constant when we vary Vcc (Fig.4). This seems to be true for low
// currents (the datsheet uses 100 lx in Fig.4), but not for larger currents.
const float phototransistor_resistor = 470.0f; const float phototransistor_resistor = 470.0f;
const float current_sun = 3.59e-3f; const float current_sun = 3.59e-3f;
// Assuming 10000 lux for the saturation test. Calibration with a proper light // Assuming 10000 lux for the saturation test. Calibration with a proper light
// meter would be better. // meter would be better.
const float lux_sun = 10000.0f; const float lux_sun = 10000.0f;
const float current = ret.voltage / phototransistor_resistor; const float current = ret.voltage / phototransistor_resistor;
ret.brightness = MAX(0, MIN(lux_sun * current / current_sun, UINT16_MAX)); ret.brightness = MAX(0, MIN(lux_sun * current / current_sun, UINT16_MAX));