loop problem

Hi all

I'm building a small timer to show the last time an object was used. The micro switch im ising to trigger the timer , when pressed in (ON), The problem is that when it has counted down , the loop just restarts the timer as the switch is still on. How can I stop the timer from restarting until the item is next used.

pic of setup added.
code added .

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

// Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6);  // Software SPI

Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);        // Hardware SPI

// int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 5; //start seconds

int e=0;

void setup()
{
  Serial.begin(9600);
  display.clearDisplay();   // clears the screen and buffer
  display.begin();
  //   display.clearDisplay();   // clears the screen and buffer
  // you can change the contrast around to adapt the display
  // for the best viewing!
  display.setContrast(50);
  //  display.clearDisplay();   // clears the screen and buffer
  pinMode(A2, INPUT_PULLUP);
  
} 
  int readButtonsA2(int pin)
{
  int f,g = 0;
  g=analogRead(pin);  
  if (g<0)
  {
    f=0; 
  }   
else 
  if (g>15 && g<25)
  {
    f=1; 
  }
  return f;
}
  
  
  


void loop()
{
   e=readButtonsA2(2);
  
  if (e==0)
  {
    display.setCursor(0,0);
    display.print("Press button");
    display.display();
    delay(1000);
  }   
  else
    if (e==1)
  { 
    while (minutes > 0 || seconds >= 0)
    {
      {
        display.setCursor(4, 24);
        display.setTextSize(2);
        (minutes < 10) ? display.print("0") : NULL;
        display.print(minutes);
        display.print(":");
        (seconds < 10) ? display.print("0") : NULL;
        display.print(seconds);
        display.display();
        stepDown();
        delay(1000);
        display.clearDisplay();   // clears the screen and buffer
    }
    }
  }
}

void stepDown()
{
  if (seconds > 0)
  {
    seconds -= 1;
  } else
  {
    if (minutes > 0)
    {
      seconds = 59;
      minutes -= 1;
    } else
    {
      trigger();
    }
  }
}

void trigger()
{
  display.clearDisplay();
  display.setCursor(20, 24);
  display.setTextSize(2);
  display.print("END ");
  delay(1000);
  display.display();
  reset();
}

void reset()
{
  asm volatile ("  jmp 0");
}

Not completely sure what you're doing but you should strip away any display related code and just replace with some serial.print. At this early stage of your development, involving displays or user inputs is the biggest beginner blunder so far. You still have not sorted out the program logic yet but you introduced a whole bunch of display code to make it impossible for others to understand.

Post a stripped down code without display just a few serial.print. I'm sure more people will want to comment on that.

Why do you do a reset after countdown reach zero? It easier to keep track of what is happening if yo don't reset all the time, as you lose all your variables. As it is now, your time goes back to what you set it to when variables are declared. If you don't reset, your code will detect time is already up and nothing will happen if button is pressed.

It is easy to add a few lines so your countdown only start when button change from 'not pressed' to 'pressed'. For that you need one more variable, for example previousE. When a new button press is detected, you can set seconds to a value you want, and countdown will start.

Study blinkWithoutDelay and StageChangeDetection examples to see how to time things and
how to detect a change in state as a trigger.