LDR always gives same value ? #PROBLEM

Hey guys. First of all, my english is not good, forgive me if I type something wrong :roll_eyes:

Let me introduce my program as quickly as possible: There’s a LDR and PIR sensor for automatic mode. I’ve a IR remote control and it will be the main control unit. I’m controlling the system with this remote control. There’s 3 mode: ON - OFF - AUTOMATIC(SENSOR) MODE.

My problem is : last week I finished my automatic/remote controlled room lamp circuit/program. I decided to put all circuit in a deodorant bottle but it was too small and I gave up this idea and I decided to put this things in a bigger box.

After this process, a problem happened. When I re-check the program LDR gives me always the same value in serial monitor. This value is 14. I changed my arduino-cables-breadboard and even resistors.

By the way, when I unplug LDR, (I don’t know “unplug” is the correct word?) program still gives this 14 value at serial monitor. Can you help me ? Thank you.

I’m using Arduino Pro Mini 5V 16MHz.

Here’s my program

#include "IRremote.h"

#define segA 2//connecting segment A to PIN2
#define segB 3// connecting segment B to PIN3
#define segC 4// connecting segment C to PIN4
#define segD 5// connecting segment D to PIN5
#define segE 6// connecting segment E to PIN6
#define segF 7// connecting segment F to PIN7
#define segG 8// connecting segment G to PIN8


int LDR = A0;
byte sensor = 11;
byte bulb = 12;
int val = 0;
int val2 = 0;
int RCV_PIN = 10;
int buzzer = 13;
int COUNT=0;//count integer for 0-9 increment

IRrecv irrecv(RCV_PIN);
decode_results results;

void setup()
{
  pinMode(sensor,INPUT);
  pinMode(bulb,OUTPUT);
  digitalWrite(bulb, HIGH); //rölenin baÅŸlangıç durumu
  Serial.begin(9600);
  irrecv.enableIRIn();
 for (int i=2;i<9;i++)
{
pinMode(i, OUTPUT);// taking all pins from 2-8 as output
}

  }


void loop()  {
  
  val=analogRead(LDR);
  val = map(val,0,1024,0,100);
  Serial.println(LDR);
  delay(200);
  if (irrecv.decode(&results)) { // have we received an IR signal?

        
 
    irrecv.resume(); // receive the next value
    delay (500);
  }  
  
  if (results.value == 0xFFE21D) {
    digitalWrite(bulb,LOW);
    Serial.println("ANA KUMANDA: sistem acik");
  digitalWrite(segA, HIGH);
  digitalWrite(segB, HIGH);
  digitalWrite(segC, HIGH);
  digitalWrite(segD, HIGH);
  digitalWrite(segE, LOW);
  digitalWrite(segF, LOW);
  digitalWrite(segG, HIGH);   
    
    
  }

  if (results.value == 0xFFA25D){
    digitalWrite(bulb,HIGH);
    Serial.println("ANA KUMANDA: sistem kapali");
    digitalWrite(segA, LOW);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, LOW);
    digitalWrite(segE, LOW);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW); 
    
    }
    

  if (results.value == 0xFF629D) {
   
    Serial.println("ANA KUMANDA: sistem otomatik modda");
  digitalWrite(segA, HIGH);
     digitalWrite(segB, HIGH);
     digitalWrite(segC, LOW);
     digitalWrite(segD, HIGH);
     digitalWrite(segE, HIGH);
     digitalWrite(segF, LOW);
     digitalWrite(segG, HIGH);
  

  if (results.value !=0xFF629D){
  if (digitalRead(sensor) == HIGH && val<=50 ) {
    Serial.println("Hareket belirlendi !");
    digitalWrite(bulb,LOW);
    delay(1000);
    }
  else if (digitalRead(sensor) == LOW || val>50 ){
  digitalWrite(bulb,HIGH);
  }

}

else 

 if (digitalRead(sensor) == HIGH && val<=50 ) {
    Serial.println("Hareket belirlendi !");
    digitalWrite(bulb,LOW);
    delay(1000);
    }
  else if (digitalRead(sensor) == LOW || val>50 ){
  digitalWrite(bulb,HIGH);
  }
}




}

Instead of this:-

val=analogRead(LDR);
  val = map(val,0,1024,0,100);
  Serial.println(LDR);

perhaps you really meant this:-

val=analogRead(LDR);
  val = map(val,0,1024,0,100);
  Serial.println(val);

Edit: Also, 'analogRead()' can only return values between 0 and 1023, so instead of this:-val = map(val,0,1024,0,100);you probably should really have this:-val = map(val,0,1023,0,100);

OldSteve: Instead of this:-

val=analogRead(LDR);
  val = map(val,0,1024,0,100);
  Serial.println(LDR);

perhaps you really meant this:-

val=analogRead(LDR);
  val = map(val,0,1024,0,100);
  Serial.println(val);

Edit: Also, 'analogRead()' can only return values between 0 and 1023, so instead of this:-val = map(val,0,1024,0,100);you probably should really have this:-val = map(val,0,1023,0,100);

You are the best. It works again. Thank you so much !

ugurkirelli: You are the best. It works again. Thank you so much !

Glad I could help. :)

you probably should really have this:- Code: [Select]

val = map(val,0,1023,0,100);

With that mapping, you would only ever see 100% for an input of 1023.

map (i, 0, 1024, 0, 101) would give a better distribution.

AWOL: With that mapping, you would only ever see 100% for an input of 1023.

map (i, 0, 1024, 0, 101) would give a better distribution.

Yeah, right. I can't argue. I remember seeing that once before, (and making a bit of a study of it), for mapping analogue read values to an 8-bit variable.:-

map (i, 0, 1024, 0, 256)
map (i, 0, 1024, 0, 256)

sp. i >>= 2;

AWOL: map (i, 0, 1024, 0, 256)

sp. i >>= 2;

Yeah, that was the end result, or i/4 actually. Is i>>=2 more code efficient than i/=4, or does it generate the same code?

My brain is going to declare its independence. What is the final situation ? lol

ugurkirelli: My brain is going to declare its independence. What is the final situation ? lol

Sorry for getting off-track. This is best:-map (i, 0, 1024, 0, 101)

OldSteve:
Yeah, that was the end result, or i/4 actually.
Is i>>=2 more code efficient than i/=4, or does it generate the same code?

The compiler would hopefully use “>> 2” whichever you wrote.

AWOL:
The compiler would hopefully use “>> 2” whichever you wrote.

Good. And thanks.