0

photoresistor

Hi everyone,

I have to write a code based on a basic alarm system on Tinkercad. My code is correct, (there were no errors when uploading) but the wiring or something to do with my LDR are wrong, because my serial monitor continues to stay 0 even when I increase my LDR (photoresisotor value) in my code.. I don't know what's wrong at all.

int threshold = 100; //set sensitivity of LDR - Use serial monitor to adjust.
int sensorPin=4; //set sensorpin to LDR on pin 4
int flashPin = 13; //am LED that lights up when the alarm sounds
int buzzerPin = 3; //Set buzzer to connect to pin 3
int Status = 0; //set arm value to 0, 0=exit delay, 1=armed, 2=triggered.
int sensorValue= 0; //set sensor value to 0


void setup() {
// initialize digital pins as output and input.
pinMode(sensorPin, INPUT);
pinMode(flashPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);}

void loop() {
sensor = analogRead(sensorPin); //this reads the sensorpin value to see if it has detected an intrusion
Serial.println(sensor); //Prints the sensorValue to the serial monitor
if (Status == 0) { //check to see if this is the first time through the loop ie Status=0
delay (3000); //this delay gives you 3 seconds to close the box
tone(buzzerPin,725,40); //this gives a short beep to indicate it is armed
delay (200); //short gap
tone(buzzerPin,725,40); //another short beep gives it a double beep sound.
Status=1; } //sets the value of arm to 1 so it doesn't do this loop again

if (Status==1){
if (sensor>threshold){ //Tests whether the sensor has been triggered ie open circuit
delay (2000); //Wait 2 seconds to allow time to turn off.
Status=2;}} //If intrusion detected, sets alarm status to triggered.

if (Status == 2){ //If alarm is triggered, run this loop.
digitalWrite(flashPin,1); //turns on the led
tone(buzzerPin,725,1000); //Plays the tone
delay (1000);
tone(buzzerPin,330,1000);
delay (1000);}
} //repeat

Here is my code. Mainly my question  is, why does my serial monitor not change its value when I increase my photoresistor value?

0 comments

Please sign in to leave a comment.