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");
}
}