LDR value not reading in while loop

Hello everyone, I'm new to this forum and now i'm working on laser tripwire alarm system with keypad

I'm having trouble with codes ldrPin value not reading in while loop if someone could help me about that i would be happy

Here is my code

#include <Keypad.h>
int ldrPin = A0;
int laserPin = 5;
int buzzerPin = 4;
int sifre_kontrol = 0;
bool kontrol = false;
int deger;

String sifre_giris = "";
String sifre = "3333";

const byte SIRA = 4;
const byte SUTUN = 4;

char tuslar[SIRA][SUTUN] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte siraPin[SIRA] = {13,12,11,10};
byte sutunPin[SUTUN] = {9, 8, 7, 6};

Keypad tustakimi = Keypad(makeKeymap(tuslar), siraPin, sutunPin, SIRA, SUTUN);
 
void setup()
{
pinMode(laserPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
digitalWrite(laserPin, HIGH);
}

void loop()
{
  int deger = analogRead(ldrPin);
  Serial.println(deger);
  char tus = tustakimi.getKey();
  digitalWrite(laserPin, HIGH);
    while(kontrol = false){
    digitalWrite(laserPin, HIGH);

    if(deger < 500){
      digitalWrite(buzzerPin, HIGH);
    }
    else{
      digitalWrite(buzzerPin, LOW);
    }
    }
  if(tus){
       if(sifre_giris.length() < 4){
        sifre_giris += tus;
       }
  }
  if(sifre_giris.length() == 4){
    delay(1000);
    if(sifre == sifre_giris){
    kontrol = true;
    digitalWrite(laserPin, LOW);
    digitalWrite(buzzerPin, LOW);
  }
  else{
    kontrol = false;
  }
  }
  }

Looks wrong? why are you assigning false to kontrol? Did you mean to use '==' instead to check the value?

I have changed it to '==' but LDR still not reading in while loop

This is updated version of my code, i think there is problem with 'if/else' because LDR not reading below them

#include <Keypad.h>
int ldrPin = A0;
int laserPin = 5;
int buzzerPin = 4;
int sifre_kontrol = 0;
bool kontrol = false;
int deger;

String sifre_giris = "";
String sifre = "6666";

const byte SIRA = 4;
const byte SUTUN = 4;

char tuslar[SIRA][SUTUN] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte siraPin[SIRA] = {13,12,11,10};
byte sutunPin[SUTUN] = {9, 8, 7, 6};

Keypad tustakimi = Keypad(makeKeymap(tuslar), siraPin, sutunPin, SIRA, SUTUN);
 
void setup()
{
pinMode(laserPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
digitalWrite(laserPin, HIGH);
}

void loop()
{
  int deger = analogRead(ldrPin);
  char tus = tustakimi.getKey();
  digitalWrite(laserPin, HIGH);
  Serial.print(tus);
  if(tus){
       if(sifre_giris.length() < 4){
        sifre_giris += tus;
       }
  }
  if(sifre_giris.length() == 4){
    delay(1000);
    if(sifre == sifre_giris){
    kontrol = true;
    digitalWrite(laserPin, LOW);
    digitalWrite(buzzerPin, LOW);
  }
  else{
    kontrol = false;
  }
  }
    Serial.println(deger);
    while(kontrol == false){
    digitalWrite(laserPin, HIGH);

    if(deger > 500){
      digitalWrite(buzzerPin, LOW);
    }
    else{
      digitalWrite(buzzerPin, LOW);
    }
    }
  }

Hi,
Where in your code do you read the LDR input?
I do not see any input digitalRead or analogRead.

What is your code supposed to do?
Can you post a circuit diagram?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

This code is reading LDR.

int deger = analogRead(ldrPin);

and this is my circuit. I didn't see any laser in tinkercad so i've placed led

Hi,
Put in a serial print to show your deger value.
Have you measured the voltage at A0 to see if your 1K resistor in the LDR circuit is the optimum value?

Tom... :grinning: :+1: :coffee: :australia:

It does, at the start of loop, but it will never change in your while loop as you never read it again.

I take my hat off! Really good habit to show the new code. In case any unintentional change is made this will show up. Well done!

deger showing correctly above 'if/else' but when i try to get value below 'if/else' not showing correctly.
I haven't measured the voltage

Thank you :slight_smile:

I'll check it

I've tried it. This time deger value showing only once

Please show your attempt, You could be very close. :wink:

I'm sorry here it is, i've only changed here rest is same

    while(kontrol == false){
    digitalWrite(laserPin, HIGH);
    deger = analogRead(A0);
    if(deger > 500){
      digitalWrite(buzzerPin, LOW);
    }
    else{
      digitalWrite(buzzerPin, LOW);
    }
    }

There is nothing inside that loop that would change 'kontrol' or 'deger'. It's going to do the same thing forever. Try changing the 'while' to 'if' so the rest of the sketch can do stuff.

"while()" is a bit like "delay()" - it may have its uses, but extremely rarely.

And then there is "while(1)". :thinking:

I had been used 'if' but when i use 'if' system only resets when password is correct

My system should be like if someone trigger laser, buzzer will alarm. When i enter correct password system will be off