I m having some problem with my code, its works as it should but its too slow. ( guess I need other boards with more calculating power cuz all i do for days is thinking about efficiency and not about my projects... )
I hope that some genius can figure out how to make it more efficient!
Its not long but especially the last method takes a lot of time to calculate but I dont know any other way to implement it
its too slow because it begins to slow the stepper down when its running, because of calculations etc.
the program works this way:
if BTDevice is not connected: if the switch (schalter in my program) get activated: the stepper does exactly 12 revolutions. if switch gets deactivated after e.g. 4 revolutions, it continues to run until it hits the spot it began on ( in my case at the "top" -> method atTop) so it did 5 full rotations and gets reset so when activated it does 12 rounds again if not stopped in the meanwhile.
if BTDevice is connected: You can send a timer and a counter. counter tells the program how many revs it should do. (from 1 to 12) and the timer is for repeating the revolutions after eg 2 min.
if BT is connected the first case will be deactivated.
the delays only get called when the stepper is not running, and they are necessary because if its not delayed its gonna bug around ( took my freaking long to get that i need a delay) so those dont slow my program..
run is also only called once per loop. it depends on if the BT is connected or not
there a lot of boolean stuff going on what might seem weird. but its all necessary in my Opinion. maybe there is a smarter way to do it but i cant figure it out.
maybe theres also a smarter way to write the atTop method without the long if string... cuz thats kinda hard for the program to "calculate".
@AWOL i dont seem to get what u mean, maybe u can explain me what i can do better
the number is following: 400 steps per Rev( halfstepping)*13.97smth (planetary)
maybe theres also a smarter way to write the atTop method
Maybe there is, but that is not slowing you down significantly. Even if it had to test all 13 conditions in that statement it would still take less than 100 microseconds to do the whole thing. If atTop is true, you then delay for 50 milliseconds which is a lot longer than atTop takes.
You should try to get rid of the delays.
FYI: I measured how long atTop executes when it has to evaluate all 13 conditions in the 'if' statement. I overestimated it a bit. It takes less than 390 nanoseconds.
I think that is plenty fast for what you are doing and is certainly not ruining your program.
if (HC05_Response.length() == 11 && HC05_Response.charAt(0) == 'S'
&& HC05_Response.charAt(10) == 'E') {
What happens if it gets 11 characters but the first one isn't 'S'? Then it never ends. It's always stuck, with the HC05_Response String growing uncontrollably large. Always include a trap to ensure that things don't grow like this. If it gets to 12 and the aboe statement hasn't triggered, you could delete the whole thing and start over, except you may have just missed one character and you're never going to be back in sync with an 'S' in the first position.
The big-S string is slowing you down a bit and chewing up memory but that should not be significant for this program.
The cosntant 5493.4256 appears in a LOT of places in your program. Give it a name and then use that everywhere in your program.
I am not sure yet, i have to test how the fluids behave ( the motor drives a pump made from a syringe to pump booze into shotglases ) but speed we ll be around 1500-2000 steps per sec and with halfstepping. its not too fast.
and as i said the delay doesnt interfere with the program. it ll just get called if the motor is turned off
ill try to put the long number into a variable as said. thanks for that idea.
i also put it down from quarter stepping to half stepping, so speed is half as high and it has to do less calculations per second what helped also