ARDUINO SENDS DOUBLE INCREMENTS WITH ROTARY

Hi Guys and Gals,

I am having a strange behavior from Arduino for a project where I need to send a rotary value with every tick. To control each tick and each direction I have used one of the standard codes posted without interrupts as the processor will be needed to do other tasks as the code progresses.

The issue is in each if statements in the code the system should send me one increment either increasing or decreasing. But maybe it is too fast so it completes the loop faster than I think therefore does the loop one more time and sends me 2 values instead of one. The incrementation of the values are correct nevertheless quantity of the value to be sent by arduino is expected to be only one.

Basically I am expecting a count starting from 0 to 360 and with each turn in one direction it should increment by one or decrement by one. Attached is the code and all help will be appreciated.

BTW: I have tried to put delays without access after each loop.

Thank you in advance

const int smallRot_PinA = 4;
const int smallRot_PinB = 5;
int smallRot_position;
int smallRot_PinA_currentState = LOW;
int smallRot_PinA_lastState = HIGH;

void update_small_encoder(){

  smallRot_PinA_currentState = digitalRead(smallRot_PinA);
  
  if((smallRot_PinA_lastState == LOW) && (smallRot_PinA_currentState == HIGH)) {
    if (digitalRead(smallRot_PinB) == LOW){
      smallRot_position--;
    } else {
      smallRot_position++;
    }
    while (smallRot_position >= 360) {smallRot_position -= 360;}; 
    while (smallRot_position < 0) {smallRot_position += 360;};
    Serial.print("n1sr="); 
    Serial.println(smallRot_position);
    Serial.println("firstloop");
    Serial.println(smallRot_PinA_lastState);
    Serial.println(smallRot_PinA_currentState);
     } 

void setup() {
//  pinMode(bigRot_PinA, INPUT_PULLUP);
//  pinMode(bigRot_PinB, INPUT_PULLUP);
  pinMode(smallRot_PinA, INPUT_PULLUP);
  pinMode(smallRot_PinB, INPUT_PULLUP);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(xfrButton, INPUT_PULLUP);
  
  Serial.begin(19200);
}

void loop() {
  
    
    if(Serial.available() > 0){
      serialData = Serial.readStringUntil('\n');
      checkSerialData();
      }
    if(configDone == true){
        update_small_encoder();
//        update_big_encoder();
//      checkButtonState();
      }
}

Delays without access to what?

What does that even mean?

Shouldn't you set smallRot_PinA_lastState = smallRot_PinA_currentState somewhere?

It's possible your code was written for a different type of encoder than the one you have. Some encoders go through a full cycle of both switches between detents, and always have both switches open at a detent. Others also have a detent half way through the cycle, so at a detent both switches may be open or both may be closed. In the datasheets, the first type has the same number of detents per revolution as pulses per revolution. The second type has half as many pulses as detents per revolution. If your code was written for the second type, but you're using the first type, then you will get two ticks per detent. I haven't studied your code, but just wanted to raise this possibility.

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