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
  • neilk
    0 neilk over 6 years ago

    There's a lot going on in your code. Sometimes it's difficult to see the wood because the trees get in the way!!!

     

    Why not write a simple sketch, using the guidance from baldengineer and kulky64, which just sounds your buzzer 5 times. When you get that working, you can add it to your main sketch.

     

    Neil

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

    There's a lot going on in your code. Sometimes it's difficult to see the wood because the trees get in the way!!!

     

    Why not write a simple sketch, using the guidance from baldengineer and kulky64, which just sounds your buzzer 5 times. When you get that working, you can add it to your main sketch.

     

    Neil

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

    theres alot in my code becuase i am also making a countdown from my LCD. and i might ifi cant get it working here sooon

    • Cancel
    • Vote Up 0 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