#include <PID_v2.h>
#define LED_Pin 3
#define ADC A0
double Setpoint ; // will be the desired value
double Input; // photo sensor
double Output ; //LED
//PID parameters
double Kp=10, Ki=5, Kd=1;
//create PID instance
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
const float GAMMA = 1.2;
const float RL10 = 20.0;
void setup()
{
Serial.begin(115200);
Serial.println("LDR infinity test");
//Hardcode the brigdness value
Setpoint = 75;
//Turn the PID on
myPID.SetMode(AUTOMATIC);
//Adjust PID values
myPID.SetTunings(Kp, Ki, Kd);
}
void loop()
{
//Read the value from the light sensor. Analog input : 0 to 1024. We map is to a value from 0 to 255 as it's used for our PWM function.
Input = map(analogRead(ADC), 0, 1024, 0, 255); // photo senor is set on analog pin 5
//PID calculation
myPID.Compute();
//Write the output as calculated by the PID function
analogWrite(LED_Pin,Output); //LED is set to digital 3 this is a pwm pin.
//Send data by serial for plotting
Serial.print(Input);
Serial.print(" ");
Serial.print(Output);
Serial.print(" ");
Serial.print(Setpoint);
Serial.println(" ");
//delay(100);
// 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");
// }
// Serial.print(voltage);
// Serial.println(" V.");
delay(100);
}