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
  • klong84
    0 klong84 over 12 years ago

    Hi,

    I think you should use "else if" for all checks on the delay buttons, currently only deciding B/W the last button and the else block.

    I don't see you using a debounce on the buttons, see examples "debounce": <file><examples><debounce> .

    Keith

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

    Hi,

    I think you should use "else if" for all checks on the delay buttons, currently only deciding B/W the last button and the else block.

    I don't see you using a debounce on the buttons, see examples "debounce": <file><examples><debounce> .

    Keith

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
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