Counting Encoder Pulses After Motor Is Turned Off

Hey all. I am having some trouble here.

I am trying to count the number of encoder pulses which occur after I turn power off to a DC motor. I get some inconsistent values and I believe that is to be expected but the values do not coincide with the actual motor counts. I won't go into how (it's long and convoluted), but I am measuring the counts another way (which cannot be used in the arduino).

Here's my code:

void findZero(){
  analogWrite(tip120PIN, maximumPWM);//Turn motor on
  while(findingZero){
    if((PIND & 0b00010000)){currentState = HIGH;}
    else {currentState = LOW;}
    
    if(driveMotor){//If we are driving the motor for analysis
      if(currentState == LOW && lastState == HIGH){
        pulseCount++;
        //Do stuff with Serial here
      } 
      if(pulseCount >= halfRevolutionPulseCount) {
        driveMotor = false;
        pulseCount = pulseCount - halfRevolutionPulseCount;//Subtract known amount of travel
        lastMotorSignalTime = millis();//Get Current Time
        analogWrite(tip120PIN, 0);//Turn off motor
      }
    }else {//If the motor is still rotating after PWM has ceased.
      if(currentState == LOW && lastState == HIGH){
        pulseCount++;
        lastMotorSignalTime = millis();
      }
      if((millis() - lastMotorSignalTime) > 2000 ) {//Encoder signal has not occurred for 100miliseconds
        sendTwoByteSignal(pulseCount);//Send pulseCounts of the motor drift
        findingZero = false; inprogress = false; recievingPosition = true;
      }
    } 

    lastState = currentState;//Collect last encoder state for comparisons
  }//End while loop
  
}//End function

Is there something I am doing wrong?

Hardware issue - you haven't debounced the encoder pulses - maybe this is needed?

You also could have transients from switching the motor off generate rogue pulses. Are
the encoder and motor wiring kept apart? Are you filtering noise from the encoder output?

Thanks MarkT,

Unfortunately the motor encoder and wiring came as one. So they are in a ribbon. It's very similar to this product - Pololu - 50:1 Metal Gearmotor 37Dx54L mm 12V with 64 CPR Encoder (No End Cap) so I don't know if I can keep the encoder and motor wiring apart.

I will look into debouncing and filtering noise from the encoder. If you have any suggestions or resources I'd appreciate it.

Could my PWM be responsible for creating this noise. I am driving the motor via a TIP120 circuit.

Yes, obviously, that's why the power and sensor wires should be run separately if possible.
If you have access to an oscilloscope you could figure out if there's a problem and how
bad if so... Do you know what sort of encoder? Can you provide a link to the details of
your encoder? If it is optical with analog outputs you probably need to add proper hysteresis
with 74HC14 or similar anyway.

No oscilliscope available unfortunately.

It's a hall effect encoder. Pololu - 50:1 Metal Gearmotor 37Dx54L mm 12V with 64 CPR Encoder (No End Cap) It's whatever that thing is, there's no product sheet, or separate encoder for reference unfortunately.

I think I hit a dead end using this motor-encoder bundle then. I think I will have to switch to a stepper. I can't remove that PCB from the motor I don't think to make my own circuit.

Hall switches don't need hysteresis, so its either interference from the motor wires or actually
valid pulses - without an oscilloscope how can you tell the difference??

Without an oscilliscope I can't tell the difference.

The issue is that I am not detecting 'enough' pulses. So I am going with interference of the encoder lines or the arduino interrupt processes skewing this. either way I'm out of luck so I bought a stepper.

Thanks MarkT for helping me think through this.