pause program and continue after button press

This is my first Arduino project using a Mega and TFT. While waiting for the hardware to arrive, I coded five pages and the touchpad. I used delays to simulate the hardware to confirm my flow. Now I am trying to add the hardware. I would like the SecondPage() to pause and wait for a button press, beep and then advance to the ThirdPage().

I tried to activate the interrupt during the delay(1000), but the delay just pauses and then continues to 1000 before advancing to the ThirdPage(). I need a variable delay to compensate for the different elapsed times before the button is pressed, ie someone might take longer than 1000.

Then I tried “while” with changing a flag, creditcardState, when the increment() is used. When the switch is closed, the buzzer beeps but the program does not advance to the ThirdPage(). I also tried “while” with (digitalRead(buzzerPin)) with the same results.

I would appreciate any help to point me in the right direction. The program is 82412 bytes, so I have just included the sections I thought appropriate.

#include <UTFT.h>
#include <URTouch.h>
#include <Servo.h> //use pin 9
int x, y, z = 0;
char Pass[5] = "";
char master[5] = "6187";
int Pcount = 0;
int PassCounter = 0;
char Last[5] = "";
int pos = 0; // variable to store the servo position
Servo myservo; // create servo object to control a servo
int buzzerPin = 11;
const int creditcardPin = 21;
int creditcardState = 0;
unsigned long button_time = 0;
unsigned long last_button_time = 0;
//Page 1
void FirstPage()
{
  myGLCD.clrScr();
  myGLCD.setBackColor(0, 0, 0); // Sets the background color of the area where the text will be printed to black
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.drawBitmap(70, 10, 180, 90, camera); // Prints the security camera jpeg
  myGLCD.setFont(BigFont); // Sets font to big
  myGLCD.print("If you need to reset", LEFT, 130); // Prints the string on the screen
  myGLCD.print("/ reboot the program,", LEFT, 154); // Prints the string on the screen
  myGLCD.print("remove the battery.", LEFT, 178); // Prints the string on the screen
  myGLCD.print("Thank You.", CENTER, 202); // Prints the string on the screen
  myGLCD.setColor(0, 255, 0); // Sets color to green
  {
    for (int L = 23; L <= 341;)
    {
      myGLCD.fillRect(0, 228, L, 230);
      delay (1000); //wait 15 seconds
      L = L + 10;
    }
  }
  myGLCD.clrScr();
}
//Page 2
void SecondPage()
{
  myGLCD.setBackColor(0, 0, 0); // Sets the background color of the area where the text will be printed to black
  myGLCD.setColor(255, 255, 0); // Sets color to yellow
  myGLCD.setFont(BigFont); // Sets font to big
  myGLCD.print("W B and J Bank", CENTER, 10); // Prints the string on the screen
  myGLCD.print("South Wales, NY", CENTER, 34); // Prints the string on the screen
  myGLCD.setColor(255, 0, 0); // Sets color to red
  myGLCD.fillRect(0, 54, 319, 56);
  myGLCD.setColor(255, 255, 255); // Sets color to white
  myGLCD.print("Welcome", CENTER, 60); // Prints the string on the screen
  myGLCD.print("To begin, please", LEFT, 84); // Prints the string on the screen
  myGLCD.print("insert and quickly", LEFT, 108); // Prints the string on the screen
  myGLCD.print("withdraw your card.", LEFT, 132); // Prints the string on the screen
  myGLCD.print("The next menu will", LEFT, 156); // Prints the string on the screen
  myGLCD.print("then display.", LEFT, 180); // Prints the string on the screen
  myGLCD.drawBitmap(64, 204, 191, 30, images3); // Prints the credit card jpeg
  //delay(10000); //interrupt just beeps and pauses delay, advance to ThirdPage at end of delay. Delay needs to be variable not fixed.
  //while (creditcardState == 0) //wait for button press;interrupt just beeps, no advance to ThirdPage
  while (digitalRead(buzzerPin == LOW)) //interrupt just beeps, no advance to ThirdPage
  {
    //do nothing
  }
}
//Page 3
void ThirdPage()
void setup()
{
  // Initial setup

  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  attachInterrupt(digitalPinToInterrupt(21), increment, FALLING); //TFT stops working
  pinMode (creditcardPin, INPUT);
  digitalWrite(creditcardPin, HIGH);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
  FirstPage();
  SecondPage();
  ThirdPage();
  FourthPage();
  drawButtons();
}
void increment() //debounce and sound goodbuzzer when switch is pressed
{
  button_time = millis(); //check to see if increment() was called in the last 250 milliseconds
  if (button_time - last_button_time > 250)
  {
    z++;
    goodbuzzer();
    creditcardState = 1;
    Serial.println(creditcardState);
    last_button_time = button_time;
  }
}
void loop()//reads touchpad buttons, compares entry to master value, if match displays the FifthPage and activate the servo
{
  
}

You’ve probably got have a state model for this. Example states :

Starting
DisplayingPage1
DisplayingPage2
DisplayingPage3

etc.

On a transition from state A to state B you display the appropriate page.
You poll the button in the loop to determine if a transition is required.

You’ll have all sorts of problems if you attempt to use delays.
If you want any timings, e.g. a page is displayed for 10 seconds, use millis() to control the state transition to show the new page.

Take a look at this line again: while (digitalRead(buzzerPin == LOW))

Are you absolutely sure that what you intended to do was to digitalRead pin 0 or 1 depending on wether or not buzzerPin == LOW? Considering that buzzerPin is actually == 11?

Sometimes it just really matters to watch the Placement of those closing parenthesis...

In general, don’t use the delay() function because nothing else can happen during a delay(). If you want a responsive program use millis() to manage timing without blocking. Have a look at how it is done in Several Things at a Time

The delay() function is fine for quick-and-dirty tests, but it is not normally appropriate for a real program.

Using WHILE and FOR also creates blocking code and I never use them unless they will complete within a millisec - preferably less. Just use IF and allow loop() to do the iterations.

…R

Good catch JaBa, unfortunately it still not advance to the ThirdPage(). So I started a new sketch with just the code that I needed to get thru the ThirdPage() and revised the increment() to include the ThirdPage().

button_time = millis(); //check to see if increment() was called in the last 250 milliseconds
if (button_time - last_button_time > 250)
{
z++;
goodbuzzer();
ThirdPage();
last_button_time = button_time;

The sketch now pauses at the SecondPage() and waits for switch push, beeps and advances to ThirdPage().

Now I can start adding back in pieces of the original sketch with additional interrupts. I just hope that I have not created another can of worms!