Starmbi / hp_BH1750

hp_BH1750
MIT License
31 stars 10 forks source link

luxFactor does not change values? #11

Closed billbill100 closed 8 months ago

billbill100 commented 8 months ago

Hi, Using ESP32 nodeMCU-32 board. I cannot see in any of the examples where 'float luxFactor' is used?

It is specified in the wiki to allow for sensor calibration, however I am obviously using it wrong, as changing the value does nothing.

I have written my code using the 'non blocking' example and below is my function, which takes a reading, increases luxFactor, prints both values and runs in a loop.

Apologies for posting here, I cannot find a way to contact you directly or a forum for your library.

void lightmeter() { float luxFactor; luxFactor = 0.96; while (digitalRead(Button3) == LOW) {} // wait for button release delay(50); // short delay for bounce BH1750.start(); // ask sensor to take a reading

while (digitalRead(Button3) == HIGH) { // wait for button press to return from lightmeter subroutine

if (BH1750.hasValue() == true) {  // non blocking reading
  float lux = BH1750.getLux();
  Serial.print("lux: ");
  Serial.print(lux);
  BH1750.start();  // ask sensor to take a reading

  luxFactor = luxFactor + 0.01;
  if (luxFactor > 1.44) { luxFactor = 0.96; }
  Serial.print("    cal: ");
  Serial.println(luxFactor);
}

lux: 6.67 cal: 1.19 lux: 6.67 cal: 1.20 lux: 6.67 cal: 1.21 lux: 6.67 cal: 1.22 lux: 6.67 cal: 1.23 lux: 6.67 cal: 1.24 lux: 6.67 cal: 1.25 lux: 6.67 cal: 1.26 lux: 6.67 cal: 1.27 lux: 6.67 cal: 1.28 lux: 6.67 cal: 1.29 lux: 6.67 cal: 1.30 lux: 6.67 cal: 1.31 lux: 6.67 cal: 1.32 lux: 6.67 cal: 1.33 lux: 6.67 cal: 1.34 lux: 6.67 cal: 1.35 lux: 6.67 cal: 1.36 lux: 6.67 cal: 1.37 lux: 6.67 cal: 1.38 lux: 6.67 cal: 1.39 lux: 6.67 cal: 1.40 lux: 6.67 cal: 1.41 lux: 6.67 cal: 1.42 lux: 6.67 cal: 1.43 lux: 6.67 cal: 1.44 lux: 6.67 cal: 0.96

Starmbi commented 8 months ago

Hi, your luxFactor is only a local variable. Please use BH1750.luxFactor instead.

billbill100 commented 8 months ago

Thanks for your swift reply :o) The wiki says ' float luxFactor ' which is not helpful for those who have never tried to interact with a library. If it were to say ' luxCal = (luxCal + 0.01); BH1750.luxFactor = (luxCal); '

It would have saved me a good few hours tinkering :o)

Thanks for taking the time to write the library & reply.

void lightmeter() { while (digitalRead(Button3) == LOW) {} // wait for button release delay(50); // short delay for bounce

float luxCal = 0.96; BH1750.luxFactor = luxCal;

BH1750.start(); // ask sensor to take a reading

while (digitalRead(Button3) == HIGH) { // wait for button press to return from lightmeter subroutine

if (BH1750.hasValue() == true) {  // non blocking reading
  float lux = BH1750.getLux();
  float EV = log10(lux * 100 / INCIDENT_CALIBRATION) / log10(2);

  Serial.print("lux: ");
  Serial.print(lux);

  Serial.print("    EV: ");
  Serial.print(EV);

  Serial.print("    cal: ");
  Serial.println(BH1750.luxFactor);

  luxCal = (luxCal + 0.01);
  BH1750.luxFactor = (luxCal);
  if (luxCal > 1.43) { luxCal = 0.96; }

  BH1750.start();  // ask sensor to take a new reading
}

} delay(50); // short delay for bounce }