Resetting LPD 3806 encoder

Hi
I have a LPD 3806 encoder that I want to use to determine the position of a solar tracker.
The program will need to send the tracker back to a home position and zero the encoder daily.
I have connected a pushbutton for testing to act as a limit switch, which I want to zero the encoder when HIGH.
The only way I can get it to work is with the below code that resets the board.
What would be a better way of doing this?
Thanks in advance

//Test code for LPD3806 Incrimental Rotary Encoder Written By Nnamdi .M 2/29/2020.


const int encoder_a = 2; // Green - pin 2 - Digital
const int encoder_b = 3; // White - pin 3 - Digital
long encoder = 0;

int buttonState = 0;
const int buttonPin = 11;
const int ledPin = 13;
 
void(* resetFunc) (void) = 0;

void setup() {
  Serial.begin(9600);
  pinMode(encoder_a, INPUT_PULLUP);
  pinMode(encoder_b, INPUT_PULLUP);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  attachInterrupt(0, encoderPinChangeA, CHANGE);
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH); resetFunc();//board reset
      } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
  
  Serial.println(encoder/6.67);//change output to 360  
  //delay(2000);
}

void encoderPinChangeA() {
   encoder += digitalRead(encoder_a) == digitalRead(encoder_b) ? -1 : 1;
    
}

void encoderPinChangeB() {
  encoder += digitalRead(encoder_a) != digitalRead(encoder_b) ? -1 : 1;
  
}

Can't you simply set the encoder variable to zero when the button is activated?

Hi
I have tried this but does not work. encoder == 0;

"==" is a compare, a single "=" is an assignment.

Thank you. Feel a bit stupid now. Works great!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.