I wrote the script below in order to count the number of times that I press the SparkFun Qwiic Twist. But when I press the button, I get series of running numbers on the Serial Monitor and not a counter incrementation by 1. Any ideas how can I solve it?
Script:
#include "SparkFun_Qwiic_Twist_Arduino_Library.h"
TWIST twist;
int twistPressCounter;
void setup() {
twistPressCounter = 0;
Serial.begin(9600);
if(twist.begin() == false)
{
Serial.println("Twist does not appear to be connected. Please check wiring. Freezing...");
while(1);
}
}
void loop() {
if(twist.isPressed()) {
twistPressCounter++;
Serial.print(twistPressCounter);
}
Serial.println();
}
isPresseddoes no debouncing; tryisClickedfirst.twist.isPressed... right now it prints about 1000 times in one second as long as the button is pressed ... so, if you quickly click the button, then it probably prints about 50 times, or more