Close

Hidden feature on the push button detection

A project log for I2C Encoder V2

Upgraded version of the small board for connect multiple rotary encoders on the I2C bus.

SaimonSaimon 10/17/2018 at 11:590 Comments

On the I2C Encoder V2, it's possible to detect when the encoder switch is pushed, released and double pushed. But i have found out that i have accidentally introduced the functionality of detecting also a long push button press. 

Lets check this Arduino code:

......
Encoder.writeDoublePushPeriod(50);  /*Set a period for the double push of 500ms */
......

if ( Encoder.readStatus(PUSHP)&& (Encoder.readStatus(PUSHR)==false)) {
        Serial.println("Long push!");
        /* Write here your code */
      }

      if ( Encoder.readStatus(PUSHP) && Encoder.readStatus(PUSHR)) {
        Serial.println("Fast push!");
        /* Write here your code */
      }

      if ( Encoder.readStatus(PUSHD)) {
        Serial.println("Double push!");
        /* Write here your code */
      }

In the code i'm setting a double push period of 500ms. This means that when i push the encoder the internal counter starts to count and after the 500ms i have an interrupt about the behavior of the switch.

if i have both the flag of pushed and released, means that in the 500ms i have pushed and released the encoder switch, so i have done a fast push.
But of i have only the flag of pushed, means that i have only pressed the switch ad maintained the switch pressed for at least 500ms. Then i did a long push!

Discussions