user58396: 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);
  }

}


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

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


58396: <p>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.</p>

<pre><code>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);
  }

}
</code></pre>
 313: <p>When I have a state-machine problem such as this, I diagram it first - here's mine, based on how I understand your requirements:</p>

<p><a href="https://i.stack.imgur.com/CtD4R.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CtD4R.jpg" alt="State Diagram"></a></p>

<p>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.</p>

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

<pre><code>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)
}
</code></pre>
