Hello, the servo motors that I have mentioned below increase the time between the if commands with each repetition. Can you help with writing timer interrupts between servo motors?
@ undisputed60
When posting code, please, "format" your code (use IDE or formatter.org) then post your formatted code between code tags (the "</>" symbol in the comment box). This makes for easier reading and troubleshooting.
Hello, the servo motors that I have mentioned below increase the time between the if commands with each repetition. Can you help with writing timer interrupts between servo motors?
hello stefan, when the code runs, the time difference increases between the times I have written in the codes with if. e.g
if(presenttime - previoustime2 >= 23000)
{
for (pos = 18; pos >= 0; pos-=1){sg902.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg902.write(pos); delay(5);}
previoustime2 = currenttime;
}
if(currenttime - previoustime3 >= 24000)
{
for (pos = 18; pos >= 0; pos-=1){sg903.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg903.write(pos); delay(5);}
previoustime3 = currenttime;
}
These codes repeat in 23 seconds in sg902 servo, 24 seconds in sg903 servo. The 1 second difference increases with each repetition. What I want is to keep the 1 second difference constant. the time difference increases in other codes.
Written language is Turkish. Translated by google.
int pos = 0;
unsigned long present;
unsigned long previoustime1 = 0;
unsigned long previoustime2 = 0;
unsigned long previoustime3 = 0;
unsigned long previoustime4 = 0;
unsigned long previoustime5 = 0;
unsigned long previoustime6 = 0;
unsigned long previoustime7 = 0;
unsigned long previoustime8 = 0;
unsigned long previoustime9 = 0;
if (currenttime - previoustime4 >= 20) {
previoustime4 = currenttime; // update IMMIDIATELY
for (pos = 0; pos <= 18; pos += 1) { // this loop needs time to finish
sg904.write(pos);
delay(10);
}
for (pos = 18; pos >= 0; pos -= 1) { // this loop needs time to finish
sg904.write(pos);
delay(10);
}
}
2 for-loops need 180 milliseconds to finish
16 for-loops need 90 ms to finish
all in all 2180+1690 = 1800 milliseconds
this means the updating of millis() happends after 1,8 seconds
and this is to late
You have to organise your code to be 101% non-blocking not only 49% non-blocking
This means the only loop that is looping is void loop()
zero for-loops, zero-while-loops
xfpd ve stefanL38
hello, thank you for the information, but I tried the codes you wrote, but it didn't work. The time difference between the commands is increasing. thanks again
You should post your actual code.
Writing something like
Does not help at all. How should somebody look into your head what your actual code looks like
it might be that
This means you have to get rid of ALL delay()s and get rid of ALL for-loops
This is done through a fundamental RE-structuring of your code to become non-blocking.
You have one loop that is already looping. Huh ? which one?
. void loop()
all looping is done by loop()
and all "sub"-looping is organised by if-conditions.
The if-conditions enable to not loop all the time (like function loop() itself )
but only if desired and only iterate that number of times you want
This means
a for-loop like
for (pos = 0; pos <= 18; pos += 1)
Is replaced by
initialisation pos = 0
and an if-condition if (pos <= 18)
the sequence of for-loops is replaced by a switch-case-break-statement
where the switch-variable controls which part of the code is executed.
The switch-variable is set to "1" to
iterate through your first (former for-loop) the defined number of times
then the switch-variable is set to "2" to
iterate through your second (former for-loop) the defined number of times
etc. etc. etc.
but first post your actual code as a code-section.