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);
}