Code to add positive pulse count and subtract negative pulse count at 100K

I know that the Arduino can count pulses, and in some modes, it can count very quickly, but can it also count the pulses back down. In other words, if it takes my CNC machine 2000 pulses to move one inch in positive direction and I move 10 inches in that direction, can I now subtract those 20,000 pulses from the count when I move in the opposite direction? And can it be done if the pulse speed is approaching 100 kilohertz?

If the answer is yes, could someone please point me to where I could find code that would produce those results?


Only with 1 signal? Probably not. You need a second signal for direction. For what Arduino? A 16MHz microcontroller maybe can count on 100 kHz, but I'm not sure. Much depends on the program and what else it will do.

what does the count represent?

presumably code has a known position and a target position. the sign, +/-, of their difference is the direction to move. each step in that direction either increases or decreases the known position. it stops when the known position == target.

If your Arduino has access to the Step and Direction signals typical of a stepper driver I would use an interrupt for Step and an input pin for Direction. On the Step interrupt, read the Direction pin. One value of Direction means Count Up, the other value means Count Down. The Arduino should be able to easily keep up with 100 kHz. That's 160 instruction cycles. You may need to use Direct Register Access to read the Direction pin fast enough.

Thanks, I have step and direction. It's probably never going to hit 100K but it may come close, probably around 95,000/s. It outputs +5vdc for one direction, 0vdc for the other. My pulse width is 3 microseconds.

I only need it for one axis at a time now and I'm using the Uno, but I do have at ATmega 2560 I would use if it would give me multiple axes.


The pulses do represent movement and I'm not talking about just one move. I want zero the axis, run an entire G-code file, then move the machine back to my zero position and see if the counter will return exactly to Zero.

I never expect the counter to go above probably 480,000 pulses (20' on the long axis) but it may have to go into negative numbers as well.

This will be used for a troubleshooting tool for my machine.

I do have step and direction. Sorry I didn't spell that out.

The UNO has two external interrupt pins so two axies should be easy. See the attachInterrupt() documentation.

Use a 'long int' for the count and you can go +/-2.1 billion steps.

Thank you very much!!

seems you're more concerned with a "count" than a position.

again, presumably there is the current known position and a target position that the controller works to have the same values.

you should be able to specify a target position, the bed should move to that position and then you should be able to specify the "original" position and the bed to return "exactly" to that original position.

you should be able to reset the origin of the bed to anything you want.