element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 Help with Code for Custom Timer
  • 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 Not Answered
  • Replies 4 replies
  • Subscribers 417 subscribers
  • Views 442 views
  • Users 0 members are here
Related

Help with Code for Custom Timer

e14 Contributor
e14 Contributor over 12 years ago

I'm trying to build a custom timer but my code doesn't seem to function properly. There are 5 switches that control 1 LED each. When each is pressed they light the LED for a set period of time and when the time is up, a piezo buzzer goes off until the reset button is pressed. I really new to programming the arduino, but I thought I had a pretty good program until I tried to run it. Any guidance would be greatly appreciated!

 

Code is as follows:

/* 5 buttons each representing 30, 60, 90, 120, 180 seconds, when pressed, will illuminate their corresponding LED
and after the time duration is complete, a piezo buzzer will pulse until the reset button is pressed.
*/

// constants that will not change. Used to set pin designations.
const int led30Pin = 13;
const int button30Pin = 12;
const int led60Pin = 11;
const int button60Pin = 10;
const int led90Pin = 9;
const int button90Pin = 8;
const int led120Pin = 7;
const int button120Pin = 6;
const int led180Pin = 5;
const int button180Pin = 4;
const int resetPin = 3;
const int buzzerPin = 2;

// variables that will change
int button30State = 0;
int button60State = 0;
int button90State = 0;
int button120State = 0;
int button180State = 0;
int resetState = 0;
int timeState = 0;

void setup()
{
  // initialize pins as outputs
  pinMode(led30Pin, OUTPUT);
  pinMode(led60Pin, OUTPUT);
  pinMode(led90Pin, OUTPUT);
  pinMode(led120Pin, OUTPUT);
  pinMode(led180Pin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  //initialize pins as inputs
  pinMode(button30Pin, INPUT);
  pinMode(button60Pin, INPUT);
  pinMode(button90Pin, INPUT);
  pinMode(button120Pin, INPUT);
  pinMode(button180Pin, INPUT);
  pinMode(resetPin, INPUT);
 
}

void loop()
{
   // read the state of the pushbutton values:
  resetState = digitalRead(resetPin);
  button30State = digitalRead(button30Pin);
  button60State = digitalRead(button60Pin);
  button90State = digitalRead(button90Pin);
  button120State = digitalRead(button120Pin);
  button180State = digitalRead(button180Pin);

  // check if the pushbutton is pressed.
  // if it is, set all LEDs and buzzer LOW:
  if (resetState == HIGH)
  {    
    // turn LEDs and buzzer off:   
    digitalWrite(led30Pin, LOW); 
    digitalWrite(led60Pin, LOW);
    digitalWrite(led90Pin, LOW);
    digitalWrite(led120Pin, LOW);
    digitalWrite(led180Pin, LOW);
    digitalWrite(buzzerPin, LOW);
  }
  // check if pushbutton is pressed.
  //if it is, set led##Pin HIGH and start timerState decrements.
  //when timeState reaches 0, turn buzzerPin HIGH.
  if (button30State == HIGH)
  {
    //turn led30Pin on
    digitalWrite(led30Pin, HIGH);
    (timeState == 30);
    if(timeState > 0);
    delay (1000);
    (timeState --);
    if(timeState == 0);
    digitalWrite(buzzerPin, HIGH); 
  }
  if(button60State ==HIGH)
  {
    digitalWrite(led60Pin, HIGH);
    (timeState == 60);
    if(timeState > 0);
    delay (1000);
    (timeState --);
    if(timeState == 0);
    digitalWrite(buzzerPin, HIGH);
  }
   if(button90State ==HIGH)
  {
    digitalWrite(led90Pin, HIGH);
    (timeState == 90);
    if(timeState > 0);
    delay (1000);
    (timeState --);
    if(timeState == 0);
    digitalWrite(buzzerPin, HIGH);
  }
  if(button120State ==HIGH)
  {
    digitalWrite(led120Pin, HIGH);
    (timeState == 120);
    if(timeState > 0);
    delay (1000);
    (timeState --);
    if(timeState == 0);
    digitalWrite(buzzerPin, HIGH);
  }
  if(button180State ==HIGH)
  {
    digitalWrite(led180Pin, HIGH);
    (timeState == 180);
    if(timeState > 0);
    delay (1000);
    (timeState --);
    if(timeState == 0);
    digitalWrite(buzzerPin, HIGH);
  }
  //otherwise turn all output pins LOW.
  else {
  digitalWrite(buzzerPin, LOW);
  digitalWrite(led30Pin, LOW);
  digitalWrite(led60Pin, LOW);
  digitalWrite(led90Pin, LOW);
  digitalWrite(led120Pin, LOW);
  digitalWrite(led180Pin, LOW);
  }
}

  • Sign in to reply
  • Cancel
Parents
  • e14 Contributor
    0 e14 Contributor over 12 years ago

    Hi Adam,

     

    It would re useful to tell us more than "doesn't seem to function properly."  i.e. actual observed behaviour & how have you wired it up (are you sure buttons are low until pressed?)

     

    At any rate I can see you have a problem with this and similar sections:

        (timeState == 30);

        if(timeState > 0);

        delay (1000);

        (timeState --);


    I'm guessing this is meant to loop round 30 * 1-sec delays?


    What it actually does is:

        (timeState == 30); // check if timestate is equal to 30. doesn't change timeState. "=" vs "=="

        if(timeState > 0); // if timeState greater than 0 do nothing (semicolon at end indicates empty statement)

        delay (1000);      // delay 1 sec unconditionally

        (timeState --);    // decrement timeState. ok but parentheses here and 3 lines above are not needed

    ...so whichever button you press you'll see the correct led light, a 1 sec delay, then the buzzer.



    There are many ways to write this, but the following (untested) is close to your original intent:

    timeState = 30;

    while (timeState > 0) {

        delay (1000);

        timeState --;

    }


    You might also want to consider how you're switching things off.  As Keith points out the  "//otherwise turn all output pins LOW." else block is only attached to the last if and will, I think, fire every time through the loop unless button180 is currently pressed.


      Regards,

      Colin


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Thanks for the input.

     

    I've changed the code to  look like this for every time button and led.

     

    if (button30State == HIGH)

      {

    digitalWrite(led30Pin, HIGH);

        timeState = 30;

        while(timeState > 0)

        delay (1000);

        timeState --;

        while(timeState <= 0)

        digitalWrite(led30Pin, LOW);

        digitalWrite(buzzerPin, HIGH);

    }

     

    However, now when I press the button, the corresponding led lights up but it never shuts of nor does the buzzer output go high. If I reset the arduino and wait without pressing any buttons, pin 12 goes high and lights the led60Pin LED for some reason.

     

    I have also completely removed the statement at the end:

      else {

      digitalWrite(buzzerPin, LOW);

      digitalWrite(led30Pin, LOW);

      digitalWrite(led60Pin, LOW);

      digitalWrite(led90Pin, LOW);

      digitalWrite(led120Pin, LOW);

      digitalWrite(led180Pin, LOW);

      }

     

    I have not yet added debounce code as I wanted to get the timing functional. All switches have 10k pulldown resistors installed. LEDs are cathode to ground and anode to output pin through 220 ohm resistors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to e14 Contributor

    Thanks for the input.

     

    I've changed the code to  look like this for every time button and led.

     

    if (button30State == HIGH)

      {

    digitalWrite(led30Pin, HIGH);

        timeState = 30;

        while(timeState > 0)

        delay (1000);

        timeState --;

        while(timeState <= 0)

        digitalWrite(led30Pin, LOW);

        digitalWrite(buzzerPin, HIGH);

    }

     

    However, now when I press the button, the corresponding led lights up but it never shuts of nor does the buzzer output go high. If I reset the arduino and wait without pressing any buttons, pin 12 goes high and lights the led60Pin LED for some reason.

     

    I have also completely removed the statement at the end:

      else {

      digitalWrite(buzzerPin, LOW);

      digitalWrite(led30Pin, LOW);

      digitalWrite(led60Pin, LOW);

      digitalWrite(led90Pin, LOW);

      digitalWrite(led120Pin, LOW);

      digitalWrite(led180Pin, LOW);

      }

     

    I have not yet added debounce code as I wanted to get the timing functional. All switches have 10k pulldown resistors installed. LEDs are cathode to ground and anode to output pin through 220 ohm resistors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • gadget.iom
    0 gadget.iom over 12 years ago in reply to e14 Contributor

    Is this line causing the issue?

    while(timeState > 0)

     

    I would expect to see some curly braces for the full block of code to be run.

    • 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 © 2026 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.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube