element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need help editing arduino code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 18 replies
  • Answers 7 answers
  • Subscribers 394 subscribers
  • Views 1862 views
  • Users 0 members are here
  • buzzer
  • code
  • arduino
Related

Need help editing arduino code

jpock69
jpock69 over 6 years ago

After my timer runs out i want my buzzer to buzz 5 times. Right now it buzzes once this is my code. What would i change to beep more than once

 

#include <LiquidCrystal.h>

const int
    RS = 12,
    EN = 11,
    D4 = 5,
    D5 = 4,
    D6 = 3,
    D7 = 2;
const int
    pinActiveBeeper = 7;    //whatever pin you're using for the active beeper
   
LiquidCrystal lcd( RS, EN, D4, D5, D6, D7 );

const unsigned long
    culPeriod = 5ul*60ul*1000ul; //5 minutes x 60 seconds/min x 1000mS/second

//nice string generation for LCD and serial monitor
char
    szStr[16];  
//timer variables
// timeStart is set in setup() and marks the beginning of the countdown
// timeNow is updated each pass of the loop and used to check how much time (in milliseconds) has elapsed
unsigned long
    timeNow,
    timeStart;

void setup()
{
    //set up the serial monitor
    Serial.begin(9600);
    //initialize the 16x2 LCD, clear it and print "Countdown" on the first line
    lcd.begin(16, 2);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Countdown:");

    pinMode( pinActiveBeeper, OUTPUT );
    digitalWrite( pinActiveBeeper, LOW );
   
    //get the time marking the beginning of the countdown
    timeStart = millis();
   
}//setup

void loop()
{
    //these "static" variables are used to know when to update the display
    //basically, when one of the minutes, seconds or tenths changes, the display
    //is updated and a message sent to the serial monitor
    static unsigned long
        lastminutes = 0,
        lastseconds = 0,
        lasttenths = 0;
    //from the time remaining in milliseconds, calculate the "human readable" min/sec/tenths
    unsigned long
        minutes,
        seconds,
        tenths;
    unsigned long
        timeRemaining;

    //get the millisecond count now
    timeNow = millis();

    //culPeriod is set to the number of milliseconds in 5 minutes (5 x 60 x 1000)
    //if the difference between the time we started setup() and now is equal to
    //or greater than that value the countdown is done
    if( (timeNow-timeStart) < culPeriod )
    {
        //not there yet; calc the time remaining in milliseconds
        timeRemaining = culPeriod - (timeNow - timeStart);
        //conver to minutes, seconds and tenths of a second
        minutes = timeRemaining / 60000ul;
        seconds = (timeRemaining - (minutes * 60000ul))/1000ul;
        tenths = (timeRemaining - (minutes * 60000ul) - (seconds * 1000ul)) / 100ul;
        //if any of those digits has changed since the last pass, send the updated
        //values to the LCD and serial monitor
        if( (minutes != lastminutes) || (seconds != lastseconds) || (tenths != lasttenths) )
        {
            sprintf( szStr, "%lu:%02lu:%d", minutes, seconds, tenths );
            Serial.println( szStr );
            lcd.setCursor(0, 1);
            lcd.print( szStr );
            //update the last values with the current values
            lastminutes = minutes;
            lastseconds = seconds;
            lasttenths = tenths;
           
        }//if
       
    }//if
    else
    {
        //sound the active beeper (assumes a high on the pin turns it on) for 1-second
        Serial.println( "Beep on" );
        digitalWrite( pinActiveBeeper, HIGH );
        //normally don't use delay but since the action right after this is to halt and catch fire, why not?
        delay(1000);
        Serial.println( "Beep off" );
        digitalWrite( pinActiveBeeper, LOW );

        //halt and wait for reset to re-do countdown
        while(1);
       
    }//else
   
}//loop

 

  • Sign in to reply
  • Cancel

Top Replies

  • baldengineer
    baldengineer over 6 years ago in reply to jpock69 +2 suggested
    for (int x; x<5; x++) { digitalWrite( pinActiveBeeper, HIGH ); //normally don't use delay but since the action right after this is to halt and catch fire, why not? delay(1000); Serial.println( "Beep…
  • baldengineer
    baldengineer over 6 years ago +1 suggested
    If you're just going to while(1) after the buzzing is done anyway, just turn the buzzer on and off 5 times. You could either do multiple digitalWrite(HIGH) and digitalWrite(LOW) or use a for() loop.
  • kulky64
    kulky64 over 6 years ago in reply to jpock69 +1 suggested
    Try initialize variable x to 0 in your for cycle, like this: for ( int x=0; x< 5 ; x++)
Parents
  • baldengineer
    0 baldengineer over 6 years ago

    If you're just going to while(1) after the buzzing is done anyway, just turn the buzzer on and off 5 times. You could either do multiple digitalWrite(HIGH) and digitalWrite(LOW) or use a for() loop.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    i put multiple digitlewrite highs and lows and got 0 beeps, how would you add them in. i feel like im going it wrong

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    i put multiple digitlewrite highs and lows and got 0 beeps, how would you add them in. i feel like im going it wrong

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • baldengineer
    0 baldengineer over 6 years ago in reply to jpock69

         for (int x; x<5; x++) {
            digitalWrite( pinActiveBeeper, HIGH );
            //normally don't use delay but since the action right after this is to halt and catch fire, why not?
            delay(1000);
            Serial.println( "Beep off" );
            digitalWrite( pinActiveBeeper, LOW );
            delay(1000);
    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    i still get 0 beeps, any other way you know that would be quick, easy and work?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • baldengineer
    0 baldengineer over 6 years ago in reply to jpock69

    Your original statement was that your code provided a single beep. All I did was use a loop to repeat that code 5 times. So if now you get zero beeps, then the code you are using now must be different from what you posted above.

     

    If you are seeing the "on" and "off" messages in the serial monitor then it is a hardware issue. It would be helpful to know what kind of buzzer it is. Not all buzzers are self-excited and may require a tone() (PWM signal) to active it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    Before i added the loop i got one, i looped it i get 0. i must of added it wrong.    i have a 2 Pin active buzzer not sure of the exact model.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vimarsh_
    0 vimarsh_ over 6 years ago in reply to jpock69

    Check the buzzer pin connected to and the poop should work all right..

    Just check that you replace the while block in else with the for one..

    Curly braces may also be a problem?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • baldengineer
    0 baldengineer over 6 years ago in reply to jpock69

    jpock69  wrote:

     

    Before i added the loop i got one, i looped it i get 0. i must of added it wrong.   

    Then it would be helpful to see the code after you added the loop.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • kulky64
    0 kulky64 over 6 years ago in reply to jpock69

    Try initialize variable x to 0 in your for cycle, like this:

    for (int x=0; x<5; x++)

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    this was my attempt at adding a loop

     

     

     

    #include <LiquidCrystal.h>

     

     

    const int

        RS = 12,

        EN = 11,

        D4 = 5,

        D5 = 4,

        D6 = 3,

        D7 = 2;

    const int

        pinActiveBeeper = 7;    //whatever pin you're using for the active beeper

       

    LiquidCrystal lcd( RS, EN, D4, D5, D6, D7 );

     

     

    const unsigned long

        culPeriod = 5ul*60ul*1000ul; //5 minutes x 60 seconds/min x 1000mS/second

     

     

    //nice string generation for LCD and serial monitor

    char

        szStr[16];  

    //timer variables

    // timeStart is set in setup() and marks the beginning of the countdown

    // timeNow is updated each pass of the loop and used to check how much time (in milliseconds) has elapsed

    unsigned long

        timeNow,

        timeStart;

     

     

    void setup()

    {

        //set up the serial monitor

        Serial.begin(9600);

        //initialize the 16x2 LCD, clear it and print "Countdown" on the first line

        lcd.begin(16, 2);

        lcd.clear();

        lcd.setCursor(0, 0);

        lcd.print("Countdown:");

     

     

        pinMode( pinActiveBeeper, OUTPUT );

        digitalWrite( pinActiveBeeper, LOW );

       

        //get the time marking the beginning of the countdown

        timeStart = millis();

       

    }//setup

     

     

    void loop()

    {

        //these "static" variables are used to know when to update the display

        //basically, when one of the minutes, seconds or tenths changes, the display

        //is updated and a message sent to the serial monitor

        static unsigned long

            lastminutes = 0,

            lastseconds = 0,

            lasttenths = 0;

        //from the time remaining in milliseconds, calculate the "human readable" min/sec/tenths

        unsigned long

            minutes,

            seconds,

            tenths;

        unsigned long

            timeRemaining;

     

     

        //get the millisecond count now

        timeNow = millis();

     

     

        //culPeriod is set to the number of milliseconds in 5 minutes (5 x 60 x 1000)

        //if the difference between the time we started setup() and now is equal to

        //or greater than that value the countdown is done

        if( (timeNow-timeStart) < culPeriod )

        {

            //not there yet; calc the time remaining in milliseconds

            timeRemaining = culPeriod - (timeNow - timeStart);

            //conver to minutes, seconds and tenths of a second

            minutes = timeRemaining / 60000ul;

            seconds = (timeRemaining - (minutes * 60000ul))/1000ul;

            tenths = (timeRemaining - (minutes * 60000ul) - (seconds * 1000ul)) / 100ul;

            //if any of those digits has changed since the last pass, send the updated

            //values to the LCD and serial monitor

            if( (minutes != lastminutes) || (seconds != lastseconds) || (tenths != lasttenths) )

            {

                sprintf( szStr, "%lu:%02lu:%d", minutes, seconds, tenths );

                Serial.println( szStr );

                lcd.setCursor(0, 1);

                lcd.print( szStr );

                //update the last values with the current values

                lastminutes = minutes;

                lastseconds = seconds;

                lasttenths = tenths;

               

            }//if

           

        }//if

        else

        {

            //sound the active beeper (assumes a high on the pin turns it on) for 1-second

            for (int x; x<5; x++) { 

            digitalWrite( pinActiveBeeper, HIGH ); 

            //normally don't use delay but since the action right after this is to halt and catch fire, why not? 

            delay(1000); 

            Serial.println( "Beep off" ); 

            digitalWrite( pinActiveBeeper, LOW ); 

            delay(1000); 

    } 

     

     

            //halt and wait for reset to re-do countdown

            while(1);

           

        }//else

       

    }//loop

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • baldengineer
    0 baldengineer over 6 years ago in reply to jpock69

    Use the suggestion from @kulky64. I've run into that problem in the past. Change the for-statement to:

     

     

    for (int x=0; x<5; x++) {

     


    I might have lead you astray on that one.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • jpock69
    0 jpock69 over 6 years ago in reply to baldengineer

    thanks it works fine now

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube