I’m trying to write my own stepper library to better understand and customize my code. I’m pretty new to this kind of programming and have some questions. The code below is only test code.
I’m using a timer interrupt and have the steps counted and reported every second but the steps reported don’t always match the number of steps that should be called. I’d like to have some interfaces that utilize inputs of steps over real time and think the error is too large to be overlooked.
Please take a look and point out anything I should fix, add, or streamline.
How about delete? Like all the commented out code!
Like the use of Strings where there are not needed.
count is used in the timer interrupt handler and in loop(), but it is not declared volatile. It MUST be.
count is a multi-byte variable. There is currently nothing to prevent the interrupt from firing, and incrementing count, while you are in the processing of resetting it. There should be.
PaulS:
Like the use of Strings where there are not needed.
Can you give me an example? I'm pulling the whole string from the buffer instead of char by char if that's what you are referring to? Alternatives?
PaulS:
count is used in the timer interrupt handler and in loop(), but it is not declared volatile. It MUST be.
Thanks, that fixed the counting problem at some speeds but the stepper seems to be locked up at other speeds and does not change which has nothing to do with the count variable. For example in mode 1, steps per second of over 1000 seem to get locked to certain intervals like 1500 and 1666. Is this due to the interrupt frequency?
Nevermind, I'll answer it: yes it is the interrupt.
1600 steps/s
1000000/1600 = 625us per step
interval of 100us rounds up 625 to 700us per step
1000000/700 = 1428 steps/s which is what I'm getting -___-
So.... how do I avoid this? I need to be able to receive serial at a fairly high rate and start having issues if I make the interval any faster. I'm going to look over the site septillion posted.
I'd rather use the interrupts in C code but am having a hard time wrapping my head around it.
Does anyone have a simple tut on using interrupts in C? Most of the ones I've seen go over my head.
I've tried using the Timer1.setPeriod(microseconds); and it works fine. However, how would I set the speed for different steppers in the same interrupt? Now it's getting complicated.
How many steps per second do you need for each motor? I control 3 motors for a small lathe without using interrupts (just micros() ) and I also take data from my PC for every move (sequence of steps).
So.... how do I avoid this? I need to be able to receive serial at a fairly high rate
Well, stuffing your head in the sand, while Serial.readString() farts around waiting for data is NOT the answer.
Read EACH character as it becomes available. Store the character in a NULL terminated char array. When the end-of-command marker arrives, parse the data.