1

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();
}
3
  • 1
    Likely because isPressed does no debouncing; try isClicked first. Jun 15 at 19:23
  • Thanks a lot for the fast respond Jun 15 at 19:31
  • print only when there is a change in the value of 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
    – jsotola
    Jun 15 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.