the range of pwm is 0-255 and i use directionpin (9) to determine if a pulse on steppin (10) shall increase or decrease the value in PWMOUT. Stepping from 0 to 255 and back to zero randomly gives me a final pwm value of between 0 and 5. Printing the value of the PWM every change shows that the highest value of pwm reached is between 248 and 255. i also see some overflow negative steps beeing ignored. Am i missing something or is the Micro to slow or is it my code that is to slow?

Suggestions? Fixes?

INFO: The step/dir signal generator generates 256 steps/rev and 6000 rev/min and having an acceleration setting of 1000000rev/square_second.

With 9600 baud serial speed you can send 960 characters per second at most.

So if PWMOUT is three digits and print line (println) adds two control characters for "carriage return" and "line feed" you try to send 5 characters each time the loop runs.

But as you can only send 960 characters per second at most due to a very slow baud rate, this means each run through the loop needs 5/960s =more than 5 millisconds. This means your loop will run slower than 200 times per second.

With such a slow loop function, outbraked to less than 200 runs per second, you cannot detect fast changing signals with a frequency of more than 100 Hertz.

Perhaps use a higher baudrate such like Serial.begin(250000) and never stuff the Serial send buffer until it is full and delays your sketch with each additional character you try to stuff into the send buffer while it is filled up to maximum buffer size!

OK I understand the serial not beeing fast so i made some changes.Im upping the serial speed to 250000, removed all unnecessary code and tried having the analog write in two different places. but still im missing steps. I tried setting the step generator at different speeds but no matter if i step slowly or fast, stepping up and down between 25% and 75% causes the pwm to drift which i interpretate as missed steps.

I want to process 255 steps within 1/100 of a second. This means handling 255*100 step pulses every second. Reading my code i need to process three digitalRead and one analogWrite every pulse. Doing some reading online tells me that a digitalRead takes about 8us which adds up to 24us for the reads and analogWrite i have not found any info on but i assume it is about twice the time of a digitalRead which would end up in 40us each cycle total.

A rough estimation is that i should have 50% headroom for each cycle so speed of my code should not render missing steps.

For sake of experimenting i lowered the frequency of step pulses to roughly 1/10 of my target but found myself losing even more steps?!?

I want to process 255 steps within 1/100 of a second. This means handling 255*100 step pulses every second. Reading my code i need to process three digitalRead and one analogWrite every pulse. Doing some reading online tells me that a digitalRead takes about 8us which adds up to 24us for the reads and analogWrite i have not found any info on but i assume it is about twice the time of a digitalRead which would end up in 40us each cycle total.

A rough estimation is that i should have 50% headroom for each cycle so speed of my code should not render missing steps.

For sake of experimenting i lowered the frequency of step pulses to roughly 1/10 of my target but found myself losing even more steps?!?

Can someone explain?

What length are the ticks? If you are going to read ticks that way, then at least you will want to ensure you don't read the same tick twice.