Hy,
I noticed that as you start writing bigger and bigger programs with if statements, state machines, i2c, other libraries, etc etc... it really matters that you do need to pay attention on how you write your code or things will get slow.
I don`t use delay()s at all, I would only do it when it would make sense to.
Alot of times I have trouble with "if logic" when there is alot of things to take in account , something in that style:
"if this and this, but not this, do this, but also if you do this then dont do this, but also do this if first 3 things are ok" and it goes on and on.
How do you deal with situations like that? I like to look at the whole picture from time to time, to see what I can merge, sometimes there is also something I dont need anymore...
I would also like to ask few things with examples, some may look stupidly obvious, but those are simplified for the sake of it:
Does it matter if you use 2 if statements when you could get away with just 1:
if (x == 1)
{
y++;
}
if (x == 1)
{
y++;
}
instead of:
` if (x == 1)
{
y++;
y++;
}`
Also how bad is it to put a x++; statement to run every loop?
Or if there is a situation that something would execute every 200th loop (follow the loop speed) rather than every x miliseconds, should the millis() method be used anyway?
I like to do this:
x = digitalRead(3);
if (x == 1 && y <= 200)
{
y++;
if(y == 200)
{
a = 1;
y = 0;
}
}
//NOW I HAVE OUTPUT "a" EVERY 200TH LOOP.
Could I take different approach to make it faster?
I know that there is alot of times that For loop could be used, but I don`t like to use it much for some reason, I just dont. Now the question here is, does it even matter? Can a For loop be faster / better / consume less memory and RAM or is it just to tidy up the code?
Does it even make a difference when compiler is done with it?
And one real-life part of the code that will be used actually:
Im currently at the beginning of modification old radio from the 70s.
One of the mods are digital fm tuner and motorised (with stepper motor) frequency scale; I need this because I want to make auto-seek and in order to make this happen I created the "frequecy sweeper", basicly a float that sweeps between 80 and 108 (MHz) when "Seek-" or "Seek+" is pressed, based on that value, program will send DATA to the FM tuner. Also this value gets maped map() to an int that counts steps from 0 to 5700 for the stepper motor.
Here is the very last part of the code:
if (StepRequest > 0)
{
MoveSteps = ScalePos - CurrentStepPosition;
StepRequest = 0;
}
if (MoveSteps != 0)
{
CurrentStepPosition = CurrentStepPosition + MoveSteps;
stepper.step(MoveSteps);
MoveSteps = CurrentStepPosition - ScalePos;
}
StepRequest changes to 1 earlier in the loop when the "Seek" starts, ScalePos is NEW position of where the scale should be (at step 1000 for example) and CurrentStepPosition is where scale is right now.
I had it written this way because Frequency must sweep fast, only a few seconds from 80 to 108MHz (there are also halfs (88.5 MHz)) and stepper must follow. I cant just tell a stepper to go x steps together, because I dont know when auto seek will stop, but I can tell it in the every loop how far behind it is. This results in smooth, fast and accurate motion of the scale, also it stops right when it needs to.
This is all good, but because I`m feeding about 35turns/loop to the stepper, the loop must be fast or motor starts to jitter.
Is there a better way to do it?
Right now it is ok -after I excluded Serial.print(); commands that were ment for debugging purposes, but once I add tuner, bluetooth & usb module, source selector, buttons... Well at that point of learning curve I can say that I know for a fact that loop will slow down enough to cause issues down the road, if not else, constant I2C communication with FM module while auto seek will.
Last thing that I want is a stepper humming to the speed of my program.
If not else I can keep the tempo with millis() and instruct a program to check if steps should be sent earlier in the loop, or is it maybee too fast and it should be sent in the middle of the loop or at the end of it.
Help of speed control also is on an option as motor is really quiet, but at certain RPM.
Thank you all for reading my looong "essey", anwser what part you would like to, have a great day.
