Misunderstanding of 'State Change Detection'?

Hello good people of Arduino Land. :slight_smile:

Further to my previous questions regarding the water drop controller build.

(Found here for those that may wish for some background):

I have decided to move away from potentiometers as a (timing) data input device - largely due to the ‘mechanical memory’ issue.
To wit, I have decided to learn about rotary encoders.

Aren’t they wonderfully stressful devices?! :smiley:

Nevertheless, I have put together a very simple sketch which works well, and does not need interrupts - I will need six encoders, so working with a Nano will prevent the use of interrupts due to the interrupt pin limitations, and I’m certainly not clever enough to even begin to think about ‘pin change interrupts’ stuff.

I have placed some 0.1μF capacitors from the switches to GND as a crude debounce method, and as long as I don’t rotate the encoder too fast, I’m getting nice clean counts on the serial monitor.
(I plan to use Schmitt triggers for all switches eventually)

I have used the encoder switch (shaft push switch) as a reset button - to reset the count to zero.

This is where my lack of understanding of the ‘state change detection’ is causing a minor issue.

I have read countless posts here regarding the debate as to whether a switch is pressed or a switch becomes pressed…I can only imagine the frustrations of those who respond to such posts with the same passage…over and over…you must have the patience of a saint…no names mentioned, but I bet you know who you are!! :smiley:

Kudos to you all for your dedication and patience!!

So, please forgive me for adding to the ever growing list of those who have perhaps not grasped this concept!

When I press the ‘reset’ button on the encoder, despite me having written in the code for choosing a state change, I’m still seeing a stream of zeros while the switch is (albeit momentarily) pressed.

Here is the sketch:

#include <digitalIOPerformance.h> //Old library which speeds up response of digital pins - on 328 based Arduinos
#include <Wire.h>

const byte encoderPinA = 2; //DATA
const byte encoderPinB = 3; //CLOCK
const byte encoderSw = 4; //SWITCH

int encoderPos = 0;
bool encoderNowPinA = LOW;
bool encoderLastPinA = LOW;
bool swState = HIGH;
bool swStateLast = HIGH;

void setup() {


  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  pinMode(encoderSw, INPUT_PULLUP);

void loop() {

  encoderNowPinA = digitalRead(encoderPinA);
  swState = digitalRead(encoderSw);

  if (swState != swStateLast) {
    if (swState == LOW)
      encoderPos = 0;
    swState = swStateLast;

  if ((encoderLastPinA == LOW) && (encoderNowPinA == HIGH)) {
    if (digitalRead(encoderPinB) == LOW) {
    } else {
    encoderPos = min(98, max(0, encoderPos));
  encoderLastPinA = encoderNowPinA;

…and here is a screenshot of the serial monitor and what happens when I press the button…

Ideally I would like to see only one zero.
In the grand scheme of things, I’m not sure yet whether this will make a huge difference in the final project, but I’m just curious if there is a way to do this simply.

I’m going to go out on a limb and guess that it involves millis().

Is there a neat trick to prevent those messy zeros from ruining my neat serial monitor? :smiley:

Warmest regards and best wishes to you all out there! :slight_smile:


I am using a Keyes KY-040 style encoder…I say style because I think it’s a cheapo knock-off…but it looks identical…save for the ‘Keyes’ brand name…works well enough though.

swStateLast = swState


Thanks a million, I knew it would be something daft!

Works like a charm now!