Toggling LED/Buzzer within a loop

Hi there,

extremely inexperienced Arduino newb here. So I am in the middle of doing a metronome project, and that includes a buzzer beeping and an LED blinking simultaneously to show the tempo.
There are 2 things I'd like to ask, because I really suck at this and I got no idea how to do it.

  1. How do I make the reset button work all the time? What I mean is that right now, it is set to check if the button is pressed in the beginning of the while loop, which means that 35% of the time it doesn't break the loop when I press it. Is there a way for me to fix that?

  2. If I were to add a button, that turns off the buzzer but keeps the LED blinking, without breaking the loop, is there a way I could do that?

I would appreciate a reply so much!

  • Todd
const int buttonStart = 5;  // button to start the metronome
const int buttonReset = 4;  // button to turn it off
const int buzzer = 3; // buzzer
const int led = 2; // led
const int pot = A0; // potentiometer

const int minBPM = 20; // minimum bpm
const int maxBPM = 240; // maximum bpm

int bpm; // beats per minute

int valStart; // start button value

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 10, 9, 8, 7, 6); // lcd (rs, rw, e, d4, d5, d6, d7)

void setup()
{
  pinMode (buzzer, OUTPUT); 
  pinMode (led, OUTPUT); 
  pinMode (buttonStart, INPUT);
  pinMode (buttonReset, INPUT);

  lcd.begin(16, 2); // lcd setup
  lcd.clear();
}



void loop()
{
  bpm = map(analogRead(pot), 0, 1023, minBPM, maxBPM); // reads potentiometer value, scaled from 0-1023 to the minBPM - maxBPM range
  delay(1) 
  
  lcd.setCursor(0, 0);   
  lcd.print("BPM:");
  lcd.setCursor(0, 1);     // prints out bpm on lcd
  lcd.print(bpm);
  lcd.print("    ");
  
    
  valStart = digitalRead(buttonStart); // reads start button value

  if (valStart == 1)     // if start button is pressed
  {
    
    while(1)   // begins a loop
    {
      if (digitalRead(buttonReset) == HIGH)  // if reset button is pressed
      {
       break; // ends the loop
      }

      

      
  bpm = map(analogRead(pot), 0, 1023, minBPM, maxBPM);   // reads pot value within the loop so that it can be changed during the loop
  lcd.setCursor(0, 1);
  lcd.print(bpm);   
  lcd.print("    ");
  int bpms = 60000 / bpm;   // beats per milliseconds
  int beepTime = 0.1 * bpms; // duration of metronome beeps is 10% of the bpms
  int delayBetweenBeeps = 0.9 * bpms; // time between beeps is the other 90% of the bpms

  tone(buzzer,2000);          // buzzer turns on
  digitalWrite(led, HIGH);    // led turns on
  delay(beepTime);            // duration of metronome beep
  noTone(buzzer);             // buzzer turns off
  digitalWrite(led, LOW);     // led turns off
  delay(delayBetweenBeeps);   // time between beeps

  // this loops until reset button is pressed 
      
    }
    
  }
  
  
}
  1. Avoid using delay(...) which keeps you from doing anything else. Learn to use millis(). There is an excellent tutorial at the top of this forum. millis() may be painful to learn but is very preferable in the long run.

Good Luck!

Also, the ever popular adjunct, doing several things at once.