read button press once every 500 ms

this code still doesnt read once every 500 ms :frowning: please help. modified the : Using millis() for timing. A beginners guide and Demonstration code for several things at the same time.

unsigned long startMillis;
unsigned long currentMillis;
unsigned long previousButtonMillis = 0;

const unsigned long period = 10000;  //period during which button input  is valid
const byte buttonPin1 = A1;    //button on pin A1
byte currentButtonState;
byte previousButtonState;
int count = 0;
boolean counting;
const int buttonInterval = 500;  // number of millisecs between button readings

void setup()
  pinMode(buttonPin1, INPUT_PULLUP);
  Serial.println("Press the button as many times as possible in 5 seconds");
  counting = true;    //turn on the counting indicator
  startMillis = millis();  //save the start time

void loop()

  currentMillis = millis();
  previousButtonState = currentButtonState;
  if (currentMillis - startMillis <= period)  //true until the period elapses.  Note that this is the reverse of BWOD
    currentButtonState = digitalRead(buttonPin1);
    if (currentButtonState == LOW and previousButtonState == HIGH)  //the button has become pressed
      count++;    //increment count

      // this only reads the button state after the button interval has elapsed
      //  this avoids multiple flashes if the button bounces
      // every time the button is pressed it changes buttonLed_State causing the Led to go on or off
      // Notice that there is no need to synchronize this use of millis() with the flashing Leds

      if (millis() - previousButtonMillis >= buttonInterval) {

        if (digitalRead(buttonPin1) == LOW) {
          currentButtonState = ! currentButtonState; // this changes it to LOW if it was HIGH
          //   and to HIGH if it was LOW
          previousButtonMillis += buttonInterval;

  else  //period has ended
    if (counting == true)  //if we were counting
      Serial.print("Time is up");
      counting = false;    //prevent the message being displayed again

How do you know? I see no debug info whatsoever to confirm or deny that.

But if you want to read the button every 500ms use the BWOD methode to do the reading, not just some action.

And if you want to do this because of button bounce, just grab a library like Bounce2 to do the heavy lifting ;)