countdown timer with rotary encoder (noob ;))

Trying to make simple countdown timer with encoder.
When setting time with encoder, when minutes hit 59, hours are ment to be incresed by 1, but hours are getting incremented
uncontrollably. :o

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
Encoder myEnc(D5, D2);
int brojanje = 0;
int hours = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("Basic NoInterrupts Test:");
}

long position  = -999;

void loop() {
  long newPos = myEnc.read();
  if (newPos != position) {
    position = newPos / 4;
    brojanje = position;
  }
    if (brojanje < 0) {
      brojanje = 0;
    }
    if (brojanje >= 59){
      brojanje = 0;
      position = 0;
      newPos = 0;
      hours = hours + 1;
      delay(10);
    }
Serial.println((String) +hours+":"+brojanje);
  }

Looks like you need to detect when minutes [u]becomes[/u] greater than 59 not when it [u]is[/u] greater than 59

Some counting variable in rottary enc is messed up.

Example, when i turn it CCW by 5 - 6 turns, in serial monitor is 0:0. But when i start to turn it CW i need to turn it again 6 turns to get counting from zero. Cant figure it out.

why do you set position to newPos/4 but compare that value to newPos? won't this condition always be true?

    long newPos = myEnc.read();
    if (newPos != position) {
        position = newPos / 4;

gcjr: why do you set position to newPos/4 but compare that value to newPos? won't this condition always be true?

    long newPos = myEnc.read();
    if (newPos != position) {
        position = newPos / 4;

To get increment by one, not by four.

To get increment by one, not by four.

but you need to do that after the test

or

   long newPos = myEnc.read() / 4;
    if (newPos != position) {
        position = newPos;
 if (brojanje >= 59){
      brojanje = 0;
      position = 0;
      newPos = 0;
      hours = hours + 1;
      delay(10);
    }

Maybe you should changes the value '59' ? should be less than '60' isn't it ?

@nielyay yes youre right, just if >59

myEnc.read(); Returns the accumulated position. This number can be positive or negative. myEnc.write(newPosition); Set the accumulated position to a new number.

If i hit above 59, i must use myEnc.write = 0; to reset position of encoder. (Basic usage of encoder.h libary) :angry: :angry: