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--;
}
}
