Code isn't fully looping as expected

This is my first time trying to code anything with an Arduino (ATtiny85). I'm using a momentary button to change "modes" using a piezo buzzer and LED. The button switches modes as expected, and the components are beeping/flashing as expected in each "mode". What seems odd is that each "mode" will only loop what's in there twice before stopping. For example, the LED will flash twice then stop. Then when I change to the next "mode", the piezo will beep twice then stop.

How can I keep the flashing/beeping to continue indefinitely until manually switched off?

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int led = 0;      // the number of the LED pin
const int speaker = 1;

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int buttonState = LOW;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int counter = 0;

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(speaker, OUTPUT);

  // set initial LED state
  digitalWrite(led, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState)
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState)
    {
      buttonState = reading;

      // only toggle the mode if the new button state is HIGH
      if (buttonState == HIGH) 
      {
        counter++;
      }
    
      if (counter == 0)
      {
        digitalWrite(led, LOW);
      }
      
      else if (counter == 1)
      {
        digitalWrite(led, HIGH);
        delay(100);
        digitalWrite(led,LOW);
        delay(500);
      }
    
      else if (counter == 2)
      {
        digitalWrite(led, LOW);
        tone(speaker, 2730, 50);
        delay(1500);
      }
    
      else if (counter == 3)
      {
        digitalWrite(led, HIGH);
        delay(50);
        digitalWrite(led,LOW);
        delay(300);
        tone(speaker, 2730, 50);
        delay(300);
        digitalWrite(led, HIGH);
        delay(50);
        digitalWrite(led,LOW);
        delay(800);
      }
    
      else
      {
        counter = 0;
      }
    }
  }
  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

Look at ALL the delays before you get the new lastButtonState. You should save it when you first read it.

It's kinda nice to have a bit more understanding of what my code it doing while I am developing it. I like to use print statements to print out the various states or values that give me information that helps me answer questions like why do the thing 2X then stop.

Have you considered using serial prints of the various values produced to 'see' what's going on?

That's your state changer I'd start there with serial prints.

Me, I'd code this separately.

if counter >= MaxNumber )
{
counter = 0;
}

With these you might consider using switch...case - Arduino Reference.

why is the counter processing, toggling the led and speaker only performed when there's a button state change (i.e. press)?

shouldn't the counter processing be done independently

also, if the button is connected between the pin and ground, shouldn't the pin be configured with the internal pullup, INPUT_PULLUP, and should it be tested for LOW?

consider

// constants won't change. They're used here to set pin numbers:
#undef MyHW 
#ifdef MyHW         // my hardware
const int buttonPin = A1;
const int led       = 13;

#else
const int buttonPin = 2;    // the number of the pushbutton pin
const int led = 0;      // the number of the LED pin
#endif

const int speaker = 1;

// Variables will change:
int ledState    = LOW;         // the current state of the output pin
int buttonState = LOW;             // the current reading from the input pin
int counter = 0;

unsigned long msecLst;

enum { Off = HIGH, On = LOW };

void loop () {
    // read the state of the switch into a local variable:
    int but = digitalRead (buttonPin);
    if (buttonState != but) {
        buttonState = but;
        delay (10);         // debounce

        if (buttonState == LOW)  {
            counter++;
            Serial.println (counter);
        }
    }

    if (counter)  {
        unsigned long msec = millis ();
        if ( (msec - msecLst) > 500)  {
            msecLst  = msec;
            ledState = ! ledState;
            digitalWrite (led, ledState);

            if (Off == ledState)
                counter--;
        }
    }
}

void setup () {
    Serial.begin (9600);

    pinMode (buttonPin, INPUT_PULLUP);
    buttonState = digitalRead (buttonPin);

    pinMode (led,       OUTPUT);
    pinMode (speaker,   OUTPUT);

    ledState = Off;
    digitalWrite (led, ledState);
}

I was trying to follow the debounce example HERE because I was experiencing inconsistencies with button presses without it. Where would I move this line to?

I want the device to have a few modes I can choose from depending on preferences and the situation. Sometimes I only want it to flash, sometimes only beep, sometimes both.

I'm not sure I understand your second point. I only want the counter to change when I want it to be in a different mode.

I had the board wired a different way but modified it based on your feedback here so that the button is now between ground and the pin.

Sorry, I'm having a little trouble understanding this structure and getting it to work.

  1. Is there a reason the setup is after the loop in this case?
  2. Is it better to use if statements or switch...case?
  3. Where should the logic be to tell the ATtin85 what to do when the counter is in different states, in the if (counter) section?

probably because the pin was floating without a pull-up (INPUT_PULLUP).

assuming this point

your processing needs to occur even when the button isn't being pressed and held

seemed like you wanted it to repeat something the number of counts

doesn't matter. puts loop closer to the top in the response

switch is easier to read for many cases

ok. wasn't clear from your original post (see above)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.