Alarm system coding

Hi, i want to add a condition so my buzzer starts buzzin only if 2 conditions are met:
1.light intensity<70
2.buzzer is pressed
I tried many times but it doesnt work, what do i need to change?
This is what i have done:

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
int LEDpin = 11;          // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;  
const int buttonPin = 2;    
int buttonState = 0; 
const int buzzerPin = 9; 
// declaring the PWM pin</p>

void setup() {

// We'll send debugging information via the Serial monitor
  Serial.begin(9600);  
  pinMode(buttonPin, INPUT);
  pinMode(LEDpin, OUTPUT); 
  pinMode(photocellPin, INPUT);

  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);


}

void loop(void) {
  // put your main code here, to run repeatedly:
  //cod photocelula 
photocellReading = analogRead(photocellPin); 

 
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading
 
  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0
  photocellReading = 1023 - photocellReading;
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 0, 1023, 0, 1023);
  analogWrite(LEDpin, LEDbrightness);
 delay(100);

  //cod buton
buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(LEDpin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(LEDpin, LOW);
  }
  
//cod buzzer
int ldrStatus = analogRead(photocellPin);
int buttonState = digitalRead(buttonPin);

if (ldrStatus <= 70) 
if  (buttonState == 1)
{
 

tone(buzzerPin, 2000);

digitalWrite(LEDpin, HIGH);

delay(100);

noTone(buzzerPin);

digitalWrite(LEDpin, LOW);

delay(100);

Serial.println("----------- ALARM ACTIVATED -----------");

 } 
 else   {

noTone(buzzerPin);

digitalWrite(LEDpin, LOW);

Serial.println("ALARM DEACTIVATED");

 } 
  
   
}

Combine your tests in a single if using &&.

As it is if both are satisfied but then the light reading rises, the alarm will never be silenced.

Edit: Clarity.

1 Like

I see that you tried to post the code properly but was not successful. Read the forum guidelines to see how to properly post code and some good information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post

To take an action when 2 conditions are met, use the logical and (&&) operator.

if(lightIntensity < 70 && buzzer == LOW)
{
    // do something
}
1 Like
  1. What Arduino are you using?
  2. What does "doesnt work" mean?
  3. How is your button pin wired? Does it have a pulldown resistor? Is it a momentary button?
  4. Why are you not mapping to 0-255?
  1. Please post your code properly.

Thanks, i fixed my post
i tried this command but it doesnt work...

Hi,
5.Done
1.Arduino UNO
2.Work, meaning that why doesnt it respect my conditions. The buzzer makes sound even if i specified the second condition
3.Its a momentary button
4.I tried something and i forgot to change it back to 0-255.Its my first time coding something in arduino and i dont understand everything that happenes here.

@groundFungus answered that in post 3

How is your button pin wired? Does it have a pulldown resistor?

Yes, i have a pulldown resistor. My buttonpin is wired to 5v, digital pin 2 and connected to the led trough a resistor.

Please show the latest code after fixing the error, noted by other users.

its not fixed, i havent worked on it since i posted it.

but i will do it gladly after i finish it

Did you fixed the errors, pointed in posts#2 #3 #4 ?
If so - please show the revised code.
If not - fix it first, test the code and only than ask again.

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