Counter output to Tft

I have been working on a counter for reloading project. I finally got the number output on the screen when I press the button, but my problem is when it starts counting it doesn't stop. Here is a copy of my sketch.

#include <TFT.h>

/* Simple Counter

  • ——————
  • This is a simple counter that takes a digital input

*/
const int TftPin = 3; // choose the pin for the TFT input
int switchPin =2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
Tft.init();
pinMode(TftPin, OUTPUT); // declare TftPin as output
pinMode(switchPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}

void loop(){
val = digitalRead(switchPin); // read input value
if (val == HIGH) { // check if the input is HIGH button released)
Tft.drawNumber(counter,75,150,4,BLUE);
currentState = 1;
}
else {
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
delay(500);
Serial.println(counter);
Tft.drawNumber(counter,75,150,4,BLUE);
return;
}
}
previousState = currentState;
delay(600);
}

    if(currentState != previousState)
        {
        if(currentState == 1)
            {
            counter = counter + 1;
            delay(500);
            Serial.println(counter);
            Tft.drawNumber(counter,75,150,4,BLUE);
            return;   //////// This is the mistake.  You leave the function before setting 'previousState'.
            }
       }

    previousState = currentState;

I removed the return function, but now it won't count in succession. It just stays at one

Note that with all the delay() calls you'll have to release the button for more than a second for the next press to register. Otherwise I can't spot anything obvious. I'd get rid of the delays.

Thanks John, it worked fine. Just gotta get everything in the right order for the round counter I'm working on for my reloading press.

Thanks