Go Down

Topic: The buzzer rings me as soon as I turn on the alarm switch (Read 217 times) previous topic - next topic

crevatinfrancesco

Hi everyone.
I'm Francesco Crevatin.
This week I created a code for the alarm on my kitchen door ... but there's a problem.
As soon as I switch on the battery switch to power Arduino (to which I have connected 2 large buzzers, 2 LEDs and 1 ultrasonic sensor) they play for one-half according to the 2 buzzers.
What should I write in the code to make them turn on only when it detects that the French door has been opened ?. I put the code below to show you what I wrote.
Good day.

Francesco Crevatin

TheMemberFormerlyKnownAsAWOL

Code: [Select]
int trig=2;
int echo=3;
int blu=4;
int red=5;
int cicalino1=6;
int cicalino2=7;
void setup(){
  pinMode(trig,OUTPUT);
  pinMode(echo, INPUT);
  pinMode(blu,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(cicalino1,OUTPUT);
  pinMode(cicalino2,OUTPUT);
  Serial.begin(9600);
}
void loop(){
  digitalWrite( trig, LOW ); // mette LOW l'uscita del trigger
  //invia un impulso di 10 microsecondi sul trigger
  digitalWrite( trig, HIGH );
  delayMicroseconds( 10 );
  digitalWrite( trig, LOW );
  long duration = pulseIn( echo, HIGH ); // legge la durata dell'eco di ritorno
  long distance = 0.034 * duration / 2;  // approssimando a 340 m/s la velocità del suono
  Serial.print( "durata: " );
  Serial.print( duration );
  Serial.print( " , " );
  Serial.print( "distanza: " );
  //dopo 38ms si è fuori dalla portata del sensore
  if ( duration > 10000000 ) {
    Serial.println( "fuori portata");
  } else {
    Serial.print( distance );
    Serial.println( " cm" );
  }
  /**************************************************************************************************************/
  if(distance>50){
   digitalWrite(blu, HIGH);
    digitalWrite(red,LOW);
    digitalWrite(cicalino1, LOW);
    digitalWrite(cicalino2, LOW);
  }else{
    digitalWrite(blu, LOW);
    digitalWrite(cicalino2, HIGH);
    digitalWrite(red, HIGH);
    digitalWrite(cicalino1, HIGH);
    delay(250);
    digitalWrite(blu,LOW);
    digitalWrite(cicalino2, HIGH);
    digitalWrite(red,LOW);
    digitalWrite(cicalino1, LOW);
    delay(250);
  }
  delay(500);
}
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

jremington

Quote
What should I write in the code to make them turn on only when it detects that the French door has been opened
Please describe what sort of signal the French door emits when it has been opened, and how that signal is communicated to the Arduino.

crevatinfrancesco

The window door does not emit any type of signal but the ultrasonic sensor emits its waves on the wall and when the window door is opened the sensor detects that the distance is no longer the same and therefore the buzzers should sound. But what must not happen is that when I turn on the switch to power Arduino YOU ​​DO NOT HAVE TO TURN ON THE BUZZERS. But I don't know what to write in the code to make them shut up until the intruder opens the door.
Thank you and good day.

Francesco Crevatin

Go Up