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. Commented Jun 15, 2021 at 19:23
  • Thanks a lot for the fast respond Commented Jun 15, 2021 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 Commented Jun 15, 2021 at 21:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.