Resetting Rotary Encoder Value to 0

Hey guys,

I’m trying to get my rotary encoder to restart counting from zero once a certain number or above is hit. The idea is that once this number or greater is hit, the LED will light up for 1 second, turn off and then the encoder reset to 0. At the moment the LED lights up once the number is reached but continuously turns off and on after this - no matter the number. I also cannot get the rotary encoder to go back to 0.

Ive attached the wiring schematic and code, this is the encoder I am using: https://www.jaycar.com.au/rotary-encoder-with-pushbutton/p/SR1230

I haven’t wired up the button as I’m not using this function.

#define enc_a 2
#define enc_b 3
int LED1 = 6;

volatile int encoderValue = 0;


void setup() {
 Serial.begin(9600);
 pinMode(LED1, OUTPUT);
 pinMode(enc_a, INPUT_PULLUP);
 pinMode(enc_b, INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
}



void loop() {

 float distancemm = encoderValue*2.445;
 Serial.println("Distance");   
 Serial.print(distancemm);
 Serial.print(" mm    ");
 Serial.println(encoderValue);

  if (distancemm >= 20) {digitalWrite(LED1, HIGH);    // turn on LED1
  delay(1000); // wait for 1000ms
  digitalWrite(LED1, LOW);     // turn off LED
  delay(200);
    encoderValue = 0;
 
  }
  
  digitalWrite(LED1, LOW);     // turn off LED
  delay(200);
}



void encoder() {

 if (digitalRead(enc_a) == digitalRead(enc_b)) {
 encoderValue++;
 }
    else {
    encoderValue--;
    }
}

    encoderValue = 0

See anything wrong with this statement?

I've retried the code with the semicolon (whoops), however it still doesn't work

Except for the missing ; , I can get your code to function properly with my encoder.

I do not believe that you are wired correctly. See the wiring diagram given in https://www.jaycar.com.au/medias/sys_master/images/images/9335750459422/SR1230-manualMain.pdf

There is also some test code to run to confirm that the encoder is working properly.

cattledog:
Except for the missing ; , I can get your code to function properly with my encoder.

I do not believe that you are wired correctly. See the wiring diagram given in https://www.jaycar.com.au/medias/sys_master/images/images/9335750459422/SR1230-manualMain.pdf

There is also some test code to run to confirm that the encoder is working properly.

I just ran the testing code from Jaycar and removed the extra resistors, can now establish the rotary encoder works and is wired correctly. LED still doesnt work and encoder still doesnt reset to 0.

I just ran the testing code from Jaycar and removed the extra resistors, can now establish the rotary encoder works and is wired correctly. LED still doesnt work and encoder still doesnt reset to 0.

Change the LED to pin13 and use the on board LED for now.

Please show the serial output you are getting from your code. I would also stop the continuous printing with this modification to your code.

#define enc_a 2
#define enc_b 3
int LED1 = 13;

volatile int encoderValue = 0;


void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(enc_a, INPUT_PULLUP);
  pinMode(enc_b, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
}



void loop() {

  float distancemm = encoderValue * 2.445;
  static float last_distancemm =0;
  if (distancemm != last_distancemm)
  {
    last_distancemm = distancemm;
    Serial.println("Distance");
    Serial.print(distancemm);
    Serial.print(" mm    ");
    Serial.println(encoderValue);
  }

    if (distancemm >= 20) {
      digitalWrite(LED1, HIGH);    // turn on LED1
      delay(1000); // wait for 1000ms
      digitalWrite(LED1, LOW);     // turn off LED
      delay(200);
      encoderValue = 0;
    }
    digitalWrite(LED1, LOW);     // turn off LED
    delay(200);
  }

  void encoder() {
    if (digitalRead(enc_a) == digitalRead(enc_b)) {
      encoderValue++;
    }
    else {
      encoderValue--;
    }
  }

With my encoder, this is the output and the led on pin 13 lights properly

Distance
0.00 mm    0
Distance
2.45 mm    1
Distance
4.89 mm    2
Distance
7.34 mm    3
Distance
9.78 mm    4
Distance
12.22 mm    5
Distance
14.67 mm    6
Distance
17.11 mm    7
Distance
19.56 mm    8
Distance
22.00 mm    9
Distance
0.00 mm    0

cattledog:
Change the LED to pin13 and use the on board LED for now.

Please show the serial output you are getting from your code. I would also stop the continuous printing with this modification to your code.

#define enc_a 2

#define enc_b 3
int LED1 = 13;

volatile int encoderValue = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(enc_a, INPUT_PULLUP);
  pinMode(enc_b, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
}

void loop() {

float distancemm = encoderValue * 2.445;
  static float last_distancemm =0;
  if (distancemm != last_distancemm)
  {
    last_distancemm = distancemm;
    Serial.println("Distance");
    Serial.print(distancemm);
    Serial.print(" mm    ");
    Serial.println(encoderValue);
  }

if (distancemm >= 20) {
      digitalWrite(LED1, HIGH);    // turn on LED1
      delay(1000); // wait for 1000ms
      digitalWrite(LED1, LOW);    // turn off LED
      delay(200);
      encoderValue = 0;
    }
    digitalWrite(LED1, LOW);    // turn off LED
    delay(200);
  }

void encoder() {
    if (digitalRead(enc_a) == digitalRead(enc_b)) {
      encoderValue++;
    }
    else {
      encoderValue--;
    }
  }




With my encoder, this is the output and the led on pin 13 lights properly


Distance
0.00 mm    0
Distance
2.45 mm    1
Distance
4.89 mm    2
Distance
7.34 mm    3
Distance
9.78 mm    4
Distance
12.22 mm    5
Distance
14.67 mm    6
Distance
17.11 mm    7
Distance
19.56 mm    8
Distance
22.00 mm    9
Distance
0.00 mm    0

Just tried this code and it works!!! I get the same serial print as you, LED goes on and off, and the encoder resets!! Thank you

Currently researching this part:

  static float last_distancemm =0;
  if (distancemm != last_distancemm)

as I'm not quite sure how you fixed it

as I'm not quite sure how you fixed it

I do not believe that limiting the printing to changes in value "fixed" anything.

In your original code when the printing was with each pass through loop, did you have the monitor set to "autoscroll"? It's a little tick box on the lower left. If you did not have it set, I think the serial output would have run off the bottom of the screen before you could see any changes in the values.