Hi i have found a sketch to use a rotary encoder and it works as intended but is very sensitive too sensitive, I have not used interrupters before and found i can't use a delay for the function called from the interruptor, so i need to remove the interrupt and just do a simple call of the function from the loop, however as it's a rotary encoder the state does not change when not turning so if i just try to call it from the loop the code assumes its constantly rotating due to the interrupters using change state. any how i need to slow down the doEncoder() function so that it's not so sensitive. I m using a 16 pulse encoder and sometimes it think i turned it two increments, i hear i could get a less sensitive encoder as this is going to be used to scroll through a menu not a motor.
So is there anything i can do with this sketch do make it less sensitive.
#define encoder0PinA 2
#define encoder0PinB 4
#define encoderButton 7
volatile unsigned int encoder0Pos = 0;
int buttonState = 0;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoderButton, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
pinMode(encoderButton, INPUT);
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop() {
// do some stuff here - the joy of interrupts is that they take care of themselves
buttonState = digitalRead(encoderButton);
if (buttonState == HIGH) {
Serial.println("Encoder button pressed !");
delay(200);
}
}
void doEncoder() {
if (digitalRead(encoder0PinA) == HIGH) { // found a low-to-high on channel A
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
// encoder0Pos = encoder0Pos - 1; // CCW
Serial.println ("CCW");
delay(500); // not working
}
else {
//encoder0Pos = encoder0Pos + 1; // CW
Serial.println ("CW");
delay(500); // not working
}
}
else // found a high-to-low on channel A
{
if (digitalRead(encoder0PinB) == LOW) { // check channel B to see which way
// encoder is turning
// encoder0Pos = encoder0Pos + 1; // CW
Serial.println ("CW");
delay(500); // not working
}
else {
// encoder0Pos = encoder0Pos - 1; // CCW
Serial.println ("CCW");
delay(500); // not working
}
}
}
/* Arduino Rotary Encoder Tutorial
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define outputA 2
#define outputB 4
#define encoderButton 7
int counter = 0;
int aState;
int aLastState;
int buttonState = 0;
void setup() {
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
pinMode(encoderButton, INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
Serial.println("start");
}
void loop() {
buttonState = digitalRead(encoderButton);
if (buttonState == HIGH) {
Serial.println("Encoder button pressed !");
delay(200);
}
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState) {
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}