// Proposal to check for an infinite value.
//
// Docs: https://docs.wokwi.com/parts/wokwi-photoresistor-sensor
// Code taken from that page.
//
// Start the simulation, move the slider to the right for maximum flux.
// When the 'lux' is 'inf', then a message is displayed.
//

// These constants should match the photoresistor's "gamma" and "rl10" attributes
// The defaults are RL10 = 50, and GAMMA = 0.7
// They have different values here and in the diagram.json to force a zero value for analogRead.
const float GAMMA = 1.2;
const float RL10 = 20.0;

void setup() 
{
  Serial.begin(115200);
  Serial.println("LDR infinity test");
}

void loop() 
{
  // Convert the analog value into lux value:
  int analogValue = analogRead(A0);
  float voltage = float(analogValue) / 1024.0 * 5.0;
  float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
  float lux = pow(RL10 * 1e3 * pow(10.0, GAMMA) / resistance, (1.0 / GAMMA));

  if(isfinite(lux))
  {
    Serial.print("The brightness is ");
    Serial.print(lux);
    Serial.println(" lx");
  }
  else
  {
    Serial.println("Too bright to measure");
  }

  delay(750);
}