 # Mapping LDR Output Values

Hi all,

I am currently working on a larger project, however, at the moment I am simply trying to read the output values of 6 LDR sensors. Being analog, their output range is between 0 and 1023. The issue I have is that the minimum value of the sensor is around 650 - that is, when the LDR is completely covered. Now I would like to map these numbers to a range of 0 to 100. I can do that. However what I cannot figure out is to have this minimum value be zero. If I use LDR_val = map(LDR_val, 0, 1023, 0, 100) the minimum value will be ~65, instead of zero. I'm almost certain this issue arises from my 51K ohm resistors used for each LDR. I cannot change these as they are already past removal in my circuit board.

(Breathes)... so does anyone know how to offset this number so my absolute minimum light input (ie. the LDR is covered), is zero and direct light exposure is 100?

Below is my code for this simple sketch. The middle of the void loop displays the max number of each LDR every three seconds - since some of the LDRs are for flashing lights. The end of the code is sending the data values to a Nextion display so I can physically see the numbers.

``````//LDR Constants                             // Photoresistor sensors at respective Arduino analog pins
const int LHL_pin = A0;                     // LHL = Left Headlight
const int LF_pin = A1;                     // RHL = Right Headlight
const int LTL_pin = A2;                      // LF = Left Flasher
const int RHL_pin = A3;                      // RF = Right Flasher
const int RF_pin = A4;                     // LTL = Left Taillight
const int RTL_pin = A5;                     // RTL = Right Rightlight

// LDR Variables                            // Variables store values from photoresistors (0-1023) and
int LHL_read = 0;                           // int means make the variable integer
int RHL_read = 0;
int LF_read = 0;
int RF_read = 0;
int LTL_read = 0;
int RTL_read = 0;

// LDR Flasher Variables
int LHL_val = 0;
int RHL_val = 0;
int LF_val = 0;
int RF_val = 0;
int LTL_val = 0;
int RTL_val = 0;
unsigned long lastZeroedAt;               //Timer for flashers

void setup() {

Serial.begin(9600);                     // Start serial port

// Configure each pin used on Arduino
pinMode(LHL_pin, INPUT);
pinMode(LF_pin, INPUT);
pinMode(LTL_pin, INPUT);
pinMode(RHL_pin, INPUT);
pinMode(RF_pin, INPUT);
pinMode(RTL_pin, INPUT);

}

void loop() {

// read the value from each sensor
LHL_read = analogRead(A0);
LF_read = analogRead(A1);
LTL_read = analogRead(A2);
RHL_read = analogRead(A3);
RF_read = analogRead(A4);
RTL_read = analogRead(A5);

if (LHL_read > LHL_val) LHL_val = LHL_read;
if (LF_read > LF_val) LF_val = LF_read;
if (LTL_read > LTL_val) LTL_val = LTL_read;
if (RHL_read > RHL_val) RHL_val = RHL_read;
if (RF_read > RF_val) RF_val = RF_read;
if (RTL_read > RTL_val) RTL_val = RTL_read;

if (millis() - lastZeroedAt >= 3000)
{
lastZeroedAt = millis();

LHL_val = LHL_read;
LF_val = LF_read;
LTL_val = LTL_read;
RHL_val = RHL_read;
RF_val = RF_read;
RTL_val = RTL_read;
}

/*====================================================================================================================================*/
/*                                                   Sending to Nextion Display                                                       */
/*====================================================================================================================================*/

Serial.print("Home.n0.val=");
Serial.print(LHL_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

Serial.print("Home.n1.val=");
Serial.print(LF_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

Serial.print("Home.n2.val=");
Serial.print(LTL_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

Serial.print("Home.n3.val=");
Serial.print(RHL_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

Serial.print("Home.n4.val=");
Serial.print(RF_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);

Serial.print("Home.n5.val=");
Serial.print(RTL_val);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
``````

All input is appreciated!

Thank you, Connor

(deleted)