[SOLVED] How can I scale down a LDR?

Hello all,

I have a code to use a LED bar with a LDR and need to scale the LDR down to a value of 10, so far I get values around 1000.

Here is the Code:

int led[] = { 30, 32, 34, 36, 38, 40, 42, 44, 46, 48 };
int ldrPin = A0;
int ldrRead;



void setup() {
  pinMode(ldrPin, INPUT);
  Serial.begin(9600);

  for (int i = 0; i < 10; i++) {
    pinMode(led[i], OUTPUT);
  }
}
void loop() {

  ldrRead = analogRead(ldrPin);
  Serial.print("Analog reading = ");
  Serial.println(ldrRead);
  Serial.println();
  delay(100);

  for (int i = 0; i <= 10; i++) {
    digitalWrite(led[i], HIGH);
    delay(50);
  }
  for (int i = 0; i <= 10; i++) {
    digitalWrite(led[i], LOW);
  }
  if (ldrRead >= 100) {
    digitalWrite(led[10], HIGH);

  }
else if (ldrRead <= 99) {
    digitalWrite(led[10], LOW);
    }
  }

using map() is an option if you want a linear conversion

int convertedValue = map(analogRead(ldrPin), 0, 1024, 0, 10); // will give you 10 values between 0 and 9

You could multiply by 10/1024.

int convertedValue = analogRead(ldrPin) * 10L / 1024;

Thanks! it works

Also does my function look right

I meant my if and else if statement look right

You could do a bit better as far as looks go. Just use the Autoformat tool in the IDE on your code.

Otherwise, if it does what you want, there is no other right or wrong at this point.

a7

right, to do what? You haven't specified how exactly they should react.

@innerking

if your problem was solved, do a kindness to everyone on the forum, especially to those who helped you.
Write [Solved] before your topic title, so if someone searches and finds your topic, they'll know what the solution looks like.

And if it was solved by some help, please tick Solution box, the one that best describes your solution.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.