aduman0
December 31, 2021, 2:06pm
1
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;
}
}
}
aduman0:
while(kontrol = false)
Looks wrong? why are you assigning false to kontrol? Did you mean to use '==' instead to check the value?
aduman0
December 31, 2021, 2:36pm
3
I have changed it to '==' but LDR still not reading in while loop
aduman0
December 31, 2021, 2:43pm
4
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...
aduman0
December 31, 2021, 5:03pm
6
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
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!
aduman0
December 31, 2021, 5:31pm
10
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
aduman0
December 31, 2021, 5:41pm
13
I've tried it. This time deger value showing only once
Please show your attempt, You could be very close.
aduman0
December 31, 2021, 5:58pm
15
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);
}
}
aduman0:
while (kontrol == false)
{
digitalWrite(laserPin, HIGH);
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.
Paul_B
December 31, 2021, 11:56pm
17
"while()" is a bit like "delay()" - it may have its uses, but extremely rarely.
And then there is "while(1)".
aduman0
January 1, 2022, 12:36pm
18
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