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
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?
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.
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.