// This code is by Chris Staecker // You may use it for any purpose, without attribution #include // may have to install the above library, in standard repo // connect stylus to pin 2, overlay sheet to GND (or the other way round- doesn't matter) const int styluspin = 2; // connections for display: CLK to 3, DIO to 4, VCC to 5V, GND to GND const int clkpin = 3; const int diopin = 4; // connections for button: (viewed from front) // bottom-left leg to 3V, bottom-right leg to 10K ohm resistor, then to GND, top-right leg to pin 5 const int buttonpin = 5; TM1637Display display = TM1637Display(clkpin, diopin); int count = 0; bool contact = false; int buttonpress = 0; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // make the stylus's pin an input: pinMode(styluspin, INPUT); digitalWrite(styluspin, HIGH); // writing a HIGH to an INPUT pin // connects the internal pullup resistor // see https://forum.arduino.cc/t/input-pin-always-reads-high-even-with-nothing/42310/11 count = 0; // Set the brightness to 5 (0=dimmest 7=brightest) display.setBrightness(5); display.showNumberDec(0); pinMode(buttonpin, INPUT); } // the loop routine runs over and over again forever: void loop() { // read the state of the pushbutton value: buttonpress = digitalRead(buttonpin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonpress == HIGH) { count = 0; display.showNumberDec(count); } // read the input pin: int stylusstate = digitalRead(styluspin); if (stylusstate == HIGH) { contact = false; } else { if (not contact) { contact = true; count++; display.showNumberDec(count); } } delay(50); // if delay is too small, it may register several hits, too big, may miss some }