-2

I have a project that require me to use magnetic contact sensor. I will put the sensor at the door. So the idea is i want every time someone open the door, the buzzer will make a sound, but it will only stay for 3 seconds. When the door is close, buzzer will do nothing and when someone open the door again, the buzzer will make a sound for 3 seconds again. That is the loop. Here is my code. Sorry it's messy. I changed it few times and it still not working like what I want.

const int buzzer = 13; 
const int sensor = 4;

int state; // 0 close - 1 open switch

void setup()
{
  pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
  state = digitalRead(sensor);

  if (state == HIGH){
    tone(buzzer,1000);
    delay(3000);
    noTone(buzzer);
  }

  else{
    noTone(buzzer);
  }

}
  • 1
    You have to make your requirement more clear... what after the last 3 seconds? – Michel Keijzers Jul 26 at 10:27
  • You could change the delays delay(1000); and delay(5000); to delay(3000);? No? Or what exactly seems to be the problem you are facing? – Duck Dodgers Jul 26 at 12:08
  • I'm still new here so I don't know how to write longer at my question or at least tell the detail. I'm using Magnetic contact sensor to put it at the door. So I want my buzzer to make a sound everytime someone open the door. But the sound will only stay for 3 seconds. Then when I close the door, and open back, it will make sound again. – Hanim Jul 27 at 11:05
  • 1
    being new here is no reason for not describing clearly what it is that you want to happen? – jsotola Jul 27 at 20:04
  • You have tried to cram your entire question into the subject line. That isn't all that helpful. Try reading how to ask a good question. The code is good to see, but it would also help to have you describe what happens, compared to what you expect to happen. – Nick Gammon Aug 6 at 6:48
2

When I have a state-machine problem such as this, I diagram it first - here's mine, based on how I understand your requirements:

State Diagram

Most state machines are only activated when an external event happens, so not much happens inside the state-boxes. But since your application only does this one thing, you can actually do your testing inside the states. This is the difference between between blocking- and non-blocking programming - we can afford to block (delay until something happens) because there is nothing else we have to do in the meanwhile.

And my code for the state-machine (which I haven't tested, but it compiles without error):

const int buzzer = 13; 
const int sensor = 4;

int state; // 0 close - 1 open switch

void setup()
{
   pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
   // Wait for sensor HIGH
   while( digitalRead(sensor) == LOW )
      ;

   // Sensor HIGH - Alarming
   tone(buzzer, 0.52);
   delay(3000);

   // Wait for sensor LOW
   noTone(buzzer);
   while( digitalRead(sensor) == HIGH )
      ;

   // (goes to Wait for sensor HIGH)
}
  • “diagram it first” is really the best advice. – Edgar Bonet Jul 29 at 8:53
  • I'm sorry for late reply. I'm new here so i don't really understand the concept. i thought your code belong to other person, so i reply to him. I tried your code. It works but I don't want any sound when the sensor is LOW. Your code make the buzzer on for 3 seconds when the sensor is LOW and HIGH. I just want the buzzer turn on when the sensor HIGH. But your code is really helpful. Thank you. – Hanim Aug 7 at 3:33
  • I edit your code a bit and it's working ! Thank you. Serial.println("sensor: " + sensor); while ( digitalRead(sensor) == LOW ); noTone(buzzer); // Sensor HIGH - Alarming tone(buzzer, 400); delay(3000); // Wait for sensor LOW noTone(buzzer); while( digitalRead(sensor) == HIGH ) ; // (goes to Wait for sensor HIGH) – Hanim Aug 7 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.