Is there a flaw in this code?
lardom
Posts: 1,659
repeat if ina[4] S++ repeat while ina[4]Is it possible for S to increment more than once each time through the loop in this code? In my circuit an LED lights up each time my IR detector sees an encoder stripe. The problem is that the encoder disk rotation is different each time for the same value. I'm using an LM339 with a trimmer. Does an LM339 need to be debounced?
Comments
This bit of code lets you check for an active input up to the value of TIMER_THRESHOLD (specified in milliseconds).
IMHO, you'll save yourself downstream headaches by avoiding magic #s and single-letter variable names.
I stick with descriptive variable names 'except' when I write small pieces of test code. I want things as simple as I can possibly make them.
As you have probably guessed now that there is "jitter" around the threshold so you need to apply positive feedback to give it that little stable "indent". Just Google "comparator hysteresis".
@erna, I made my own wheel encoders and I'm trying to get them to work properly. I built a basic 'bot' and I want it to track straight. I discovered wheel encoders are necessary to control DC motors.
shouldn't that read
Other than that waitcnt(cnt += ms) should work. I'd argue that waitcnt(ms) is having issues.
OK, in this case you should implement a quadrature detector, that means: have a black/white code wheel and two sensors shifted by 90°. The signal generated is (running forward) 00 01 11 10 00 01 11 10, if now you change direction it will continue 11 01 00 10 11 01. If for any reason you have a flickering signal, 11 10 11 10 11 10, so one bit changes fast, it means you count forward/backward, but you do not make an error in position. Even if this changes happen faster than you can acquire them. So there is no need for debouncing or a hysteresis. There is a quadrature encoder in the OBEX in assembler, easy to use and very fast!
ErNa, quadrature encoders will be one of my next challenges. I will have to learn to read Pasm much better because Spin is not fast enough. Once I can grasp the theory of quadrature encoders then understanding the code will be much easier.
I have a motor with a built in quadrature encoder to experiment with but I want to be able to express in plain language what's happening. Once I understand a process, writing code is basically translation and syntax.
Controlling steppers and servos is simple compared to DC motors. The toughest motor, for me, is a brushless DC motor. I have one without hall sensors. (Maybe I'll give in and get one with built in sensors)
Yesterday I visited the Thomas Edison museum in NJ. I was awestruck walking around that historical place seeing his inventions. He was a strong believer in experimentation. Even at 80 years and deaf he was still at it.