Buzzer Duration Using the while() and millis() Commands

Поделиться
HTML-код
  • Опубликовано: 13 окт 2020
  • Here, I show you two methods to make a buzzer (or any chunk of code) run for a certain number of iterations or loops, and for a certain amount of time. To keep track of the time, I incorporate the millis() command.
  • НаукаНаука

Комментарии • 17

  • @kavishapro
    @kavishapro Год назад

    Hi i incorporated this code and my Led runs for a set amount of time , BUT i cant do other functions until the end of the duration . Is this normal or might i have incorporated your code wrong? , i can share my code if needed.

  • @gnaneswar7783
    @gnaneswar7783 2 года назад

    Can you please tell the code for 10 beeps and turn off??

  • @sanjaybatra6593
    @sanjaybatra6593 Год назад +1

    suppose duration =3000 , while loop will run for 3000 millisecond ( or approximate 3000 times)
    my question is : isn't it bad practice to execute this line of code { digitalWrite(buzzerPin, HIGH) } 3000 times ?

    • @PCRduino
      @PCRduino  Год назад

      Thanks for your question! I don't see how this would hurt the microcontroller, as the voltage remains the same throughout the loop. (After the first iteration, it is setting the pin that is already at 5V to 5V.) But I do see your point, that the code could be simplified if the digitalWrite() command is moved from Line 25 to Line23. But I don't think the controller will be overtaxed either way.

  • @dawoodips
    @dawoodips 3 года назад

    Excellent sir
    I was looking exactly this code , but plz guide me how I can add code to my project " water level indicator using ultrasonic sensor " . When the water reaches high level the buzzer should on for 10 seconds then it should off automatically . Plz guide me sir ..you u want codes I can upload it . Waiting for kind reply

    • @PCRduino
      @PCRduino  3 года назад +1

      Hi Dawoodhusain. You can just add the code that starts at 9:49 in the video. But this may be overkill. The reason to use a while loop and the millis() function is so your code can be doing other things while doing something else for a certain amount of time. But you may not need to do that. You can probably get by with a simple delay(10000) command, which PAUSES the code for 10,000ms or 10s. I do not have my microcontroller with me, but the code would look something like this:
      int buzzerPin = 2;
      void setup(){
      pinMode(buzzerPin, OUTPUT);
      }
      void loop(){
      // turn ON the buzzer for 10 seconds:
      digitalWrite(buzzerPin, HIGH);
      delay(10000); // pause program for 10 seconds
      // turn OFF the buzzer for 5 seconds:
      digitalWrite(buzzerPin, LOW);
      delay(5000); // pause program for 5 seconds
      }
      So, you should create the global buzzer pin value (first line of code). Then you MUST put the pin in the proper pinMode (line inside the setup() function). Finally, you can use the digitalWrite() function to turn on your buzzer with a HIGH argument, a delay() to keep it on for a specified amount of time, and finally another digitalWrite() command to send the buzzer LOW to turn it off after the delay.
      Not sure if this is what you are looking for. Hope it helps.

    • @dawoodips
      @dawoodips 3 года назад

      @@PCRduino thanks sir for the reply that code i have tried sir the buzzer wont stop after ten seconds instead it continuous to beep here i have uploaded the code plz check it sir ..
      ..........................................
      const int trigPin = 2;
      const int echoPin = 3;
      int ledA = 13;
      int ledB = 12;
      int ledC = 11;
      int ledD = 10;
      int ledE = 9;
      int buzzer = 8;
      int A = 20;
      int B = 16;
      int C = 12;
      int D = 8;
      int E = 5;
      int max_distance = 200;
      void setup() {
      Serial.begin(9600);
      pinMode (ledA, OUTPUT);
      pinMode (ledB, OUTPUT);
      pinMode (ledC, OUTPUT);
      pinMode (ledD, OUTPUT);
      pinMode (ledE, OUTPUT);
      pinMode (buzzer, OUTPUT);
      digitalWrite(ledA, LOW);
      digitalWrite(ledB, LOW);
      digitalWrite(ledC, LOW);
      digitalWrite(ledD, LOW);
      digitalWrite(ledE, LOW);
      digitalWrite(buzzer, LOW);
      }
      void loop() {
      long duration, inches, cm;
      pinMode(trigPin, OUTPUT);
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      pinMode(echoPin, INPUT);
      duration = pulseIn(echoPin, HIGH);
      cm = ( duration / 29 ) / 2;
      inches = cm * 0.393701;
      inches = microsecondsToInches(duration);
      cm = microsecondsToCentimeters(duration);
      if (inches < max_distance) {
      Serial.print(inches);
      Serial.print("in");
      Serial.print(cm);
      Serial.print("cm");
      Serial.println();
      delay(100);
      digitalWrite (ledA, HIGH);
      }
      if (inches >= A) {
      digitalWrite (ledA, HIGH);
      digitalWrite (ledB, LOW);
      digitalWrite (ledC, LOW);
      digitalWrite (ledD, LOW);
      digitalWrite (ledE, LOW);
      }
      else if (inches >= B) {
      digitalWrite (ledA, LOW);
      digitalWrite (ledB, HIGH);
      digitalWrite (ledC, LOW);
      digitalWrite (ledD, LOW);
      digitalWrite (ledE, LOW);
      }
      else if (inches >= C) {
      digitalWrite (ledA, LOW);
      digitalWrite (ledB, LOW);
      digitalWrite (ledC, HIGH);
      digitalWrite (ledD, LOW);
      digitalWrite (ledE, LOW);
      }
      else if (inches >= D) {
      digitalWrite (ledA, LOW);
      digitalWrite (ledB, LOW);
      digitalWrite (ledC, LOW);
      digitalWrite (ledD, HIGH);
      digitalWrite (ledE, LOW);
      }
      else if (inches >= E) {
      digitalWrite (ledA, LOW);
      digitalWrite (ledB, LOW);
      digitalWrite (ledC, LOW);
      digitalWrite (ledD, LOW);
      digitalWrite (ledE, HIGH);
      }
      else {
      digitalWrite (ledA, HIGH);
      digitalWrite (ledB, HIGH);
      digitalWrite (ledC, HIGH);
      digitalWrite (ledD, HIGH);
      digitalWrite (ledE, HIGH);
      }

      }
      long microsecondsToInches(long microseconds) {
      return (microseconds / 74) / 2;
      }
      long microsecondsToCentimeters(long microseconds) {
      return (microseconds / 29) / 2;
      }

    • @dawoodips
      @dawoodips 3 года назад

      @@PCRduino sir actually int A=20 is the my tank overflow , when the water reaches this level i want to on the buzzer for 10 seconds after that the buzzer should stop

    • @PCRduino
      @PCRduino  3 года назад

      @@dawoodips Hi again. I see that you have set up the pinMode for your buzzer properly in OUTPUT mode and you set it to LOW, which is fine but is not necessary. I do not see where you have actually turned on the buzzer, so this makes me think that you have plugged in the buzzer to the motherboard improperly. The buzzer's positive (+) terminal should be in pin 8 and its ground pin (which is the shorter pin) should be connected to GND. Are you doing that? What kind of buzzer are you using? Is it a discrete component or connected to some board?

  • @sethmoening9650
    @sethmoening9650 2 года назад

    What if we wrote the following?
    while (startTime < millis () < endTime)

    • @PCRduino
      @PCRduino  2 года назад +1

      So, that kind of logic statement works in a math class, but not in a programming class. In programming, you need to use what's known as logical-and operators to join two or more conditional expressions. Logical-and is entered as &&. Something like this:
      while ( (startTime < millis()) && (millis() < endTime))
      or:
      while ((millis() > startTime) && (millis() < endTime))
      See?

    • @sethmoening9650
      @sethmoening9650 2 года назад +1

      @@PCRduino 10-4. Thank you!!

  • @prasadpatil7246
    @prasadpatil7246 2 года назад

    Hello sir, Sir i am working on a project where i have to initiate the beep in ready to drive system and stop it after say 3 seconds or 5 seconds. So how to do it ?

  • @sethmoening9650
    @sethmoening9650 2 года назад

    What if we wrote the following?
    while (startTime < millis ( ) < endTime)

    • @sethmoening9650
      @sethmoening9650 2 года назад

      So the difference between “while” and “for” loops
      while loops check that condition and do “thing x” if the condition is met and cycles through the loop and will keep doing “thing x” as long as that condition is met
      for loops will go into the “for loop” and do
      “thing y” FOR a specified number of times depending on the arguments that are fed into that function
      Correct??

    • @PCRduino
      @PCRduino  2 года назад

      That is correct.

    • @PCRduino
      @PCRduino  2 года назад

      So, that kind of logic statement works in a math class, but not in a programming class. In programming, you need to use what's known as logical-and operators to join two or more conditional expressions. Logical-and is entered as &&. Something like this:
      while ( (startTime < millis()) && (millis() < endTime))
      or:
      while ((millis() > startTime) && (millis() < endTime))
      See?