I need help wi̇th servo motor

  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?

#include <Servo.h>
Servo sg901;
Servo sg902;
Servo sg903;
Servo sg904;
Servo sg905;
Servo sg906;
Servo sg907;
Servo sg908;
Servo sg909;
int pos = 0;
unsigned long simdikizaman;
unsigned long oncekizaman1 = 0;
unsigned long oncekizaman2 = 0;
unsigned long oncekizaman3 = 0;
unsigned long oncekizaman4 = 0;
unsigned long oncekizaman5 = 0;
unsigned long oncekizaman6 = 0;
unsigned long oncekizaman7 = 0;
unsigned long oncekizaman8 = 0;
unsigned long oncekizaman9 = 0;
void setup()
{
sg904.attach(4);
sg902.attach(2);
sg903.attach(3);
sg901.attach(1);
sg905.attach(5);
sg906.attach(6);
sg907.attach(7);
sg908.attach(8);
sg909.attach(9);
}
void loop(){
simdikizaman = millis();
if(simdikizaman - oncekizaman4 >= 20)
{
for(pos=0;pos<=18;pos+=1){sg904.write(pos);delay(10);}
for(pos=18;pos>=0;pos-=1){sg904.write(pos);delay(10);}
oncekizaman4 = simdikizaman;
}
if(simdikizaman - oncekizaman6 >= 25000)
{
for (pos = 18; pos >= 0; pos-=1){sg906.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg906.write(pos); delay(5);}
oncekizaman6 = simdikizaman;
}
if(simdikizaman - oncekizaman1 >= 22000)
{
for (pos = 0; pos <= 18; pos+=1){sg901.write(pos); delay(5);}
for (pos = 18; pos >= 0; pos-=1){sg901.write(pos); delay(5);}
oncekizaman1 = simdikizaman;
}
if(simdikizaman - oncekizaman2 >= 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);}
oncekizaman2 = simdikizaman;
}
if(simdikizaman - oncekizaman3 >= 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);}
oncekizaman3 = simdikizaman;
}
if(simdikizaman - oncekizaman5 >= 107000)
{
for (pos = 0; pos <= 18; pos+=1){sg905.write(pos); delay(5);}
for (pos = 18; pos >= 0; pos-=1){sg905.write(pos); delay(5);}
oncekizaman5 = simdikizaman;
}
if(simdikizaman - oncekizaman7 >= 548000)
{
for (pos = 18; pos >= 0; pos-=1){sg907.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg907.write(pos); delay(5);}
oncekizaman7 = simdikizaman;
}
if(simdikizaman - oncekizaman8 >= 549000)
{
for (pos = 18; pos >= 0; pos-=1){sg908.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg908.write(pos); delay(5);}
oncekizaman8 = simdikizaman;
}
if(simdikizaman - oncekizaman9 >= 26000)
{
for (pos = 58; pos >= 40; pos-=1){sg909.write(pos); delay(5);}
for (pos = 40; pos <= 58; pos+=1){sg909.write(pos); delay(5);}
oncekizaman9 = simdikizaman;
}
}

waiting for your help...

@ 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?

#include <Servo.h>
Servo sg901;
Servo sg902;
Servo sg903;
Servo sg904;
Servo sg905;
Servo sg906;
Servo sg907;
Servo sg908;
Servo sg909;
int pos = 0;
unsigned long simdikizaman;
unsigned long oncekizaman1 = 0;
unsigned long oncekizaman2 = 0;
unsigned long oncekizaman3 = 0;
unsigned long oncekizaman4 = 0;
unsigned long oncekizaman5 = 0;
unsigned long oncekizaman6 = 0;
unsigned long oncekizaman7 = 0;
unsigned long oncekizaman8 = 0;
unsigned long oncekizaman9 = 0;
void setup() {
  sg904.attach(4);
  sg902.attach(2);
  sg903.attach(3);
  sg901.attach(1);
  sg905.attach(5);
  sg906.attach(6);
  sg907.attach(7);
  sg908.attach(8);
  sg909.attach(9);
}
void loop() {
  simdikizaman = millis();
  if (simdikizaman - oncekizaman4 >= 20) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg904.write(pos);
      delay(10);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg904.write(pos);
      delay(10);
    }
    oncekizaman4 = simdikizaman;
  }
  if (simdikizaman - oncekizaman6 >= 25000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg906.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg906.write(pos);
      delay(5);
    }
    oncekizaman6 = simdikizaman;
  }
  if (simdikizaman - oncekizaman1 >= 22000) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg901.write(pos);
      delay(5);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg901.write(pos);
      delay(5);
    }
    oncekizaman1 = simdikizaman;
  }
  if (simdikizaman - oncekizaman2 >= 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);
    }
    oncekizaman2 = simdikizaman;
  }
  if (simdikizaman - oncekizaman3 >= 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);
    }
    oncekizaman3 = simdikizaman;
  }
  if (simdikizaman - oncekizaman5 >= 107000) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg905.write(pos);
      delay(5);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg905.write(pos);
      delay(5);
    }
    oncekizaman5 = simdikizaman;
  }
  if (simdikizaman - oncekizaman7 >= 548000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg907.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg907.write(pos);
      delay(5);
    }
    oncekizaman7 = simdikizaman;
  }
  if (simdikizaman - oncekizaman8 >= 549000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg908.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg908.write(pos);
      delay(5);
    }
    oncekizaman8 = simdikizaman;
  }
  if (simdikizaman - oncekizaman9 >= 26000) {
    for (pos = 58; pos >= 40; pos -= 1) {
      sg909.write(pos);
      delay(5);
    }
    for (pos = 40; pos <= 58; pos += 1) {
      sg909.write(pos);
      delay(5);
    }
    oncekizaman9 = simdikizaman;
  }
}

This guy does thorough and understandable projects that he also posts on YouTube... I think you should give it a read/look.

I don't understand what you wanto to say with that. Please describe with much more words and in much more details what you mean with

"writing timer interrupts between servo motors"

A timer-interrupt that is placed in the free physical space between your servo-motors?

If english is not your native language write the description in your native language and let do google translate the translation

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.

The entire code is in English below.

#include <Servo.h>
Servo sg901;
Servo sg902;
servo sg903;
servo sg904;
servo sg905;
servo sg906;
Servo sg907;
servo sg908;
servo sg909;

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;

void setup()
{
sg904.attach(4);
sg902.attach(2);
sg903.attach(3);
sg901.attach(1);
sg905.attach(5);
sg906.attach(6);
sg907.attach(7);
sg908.attach(8);
sg909.attach(9);

}

void loop(){

currenttime = millis();

if(currenttime - previoustime4 >= 20)
{
for(pos=0;pos<=18;pos+=1){sg904.write(pos);delay(10);}
for(pos=18;pos>=0;pos-=1){sg904.write(pos);delay(10);}
previoustime4 = currenttime;
}

if(currenttime - previoustime6 >= 25000)
{
for (pos = 18; pos >= 0; pos-=1){sg906.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg906.write(pos); delay(5);}
previoustime6 = currenttime;
}
if(currenttime - previoustime1 >= 22000)
{
for (pos = 0; pos <= 18; pos+=1){sg901.write(pos); delay(5);}
for (pos = 18; pos >= 0; pos-=1){sg901.write(pos); delay(5);}
previoustime1 = currenttime;
}
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;
}
if(currenttime - previoustime5 >= 107000)
{
for (pos = 0; pos <= 18; pos+=1){sg905.write(pos); delay(5);}
for (pos = 18; pos >= 0; pos-=1){sg905.write(pos); delay(5);}
previoustime5 = currenttime;
}
if(currenttime - previoustime7 >= 548000)
{
for (pos = 18; pos >= 0; pos-=1){sg907.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg907.write(pos); delay(5);}
previoustime7 = currenttime;
}
if(currenttime - previoustime8 >= 549000)
{
for (pos = 18; pos >= 0; pos-=1){sg908.write(pos); delay(5);}
for (pos = 0; pos <= 18; pos+=1){sg908.write(pos); delay(5);}
previoustime8 = currenttime;
}
if(currenttime - previoustime9 >= 26000)
{
for (pos = 58; pos >= 40; pos-=1){sg909.write(pos); delay(5);}
for (pos = 40; pos <= 58; pos+=1){sg909.write(pos); delay(5);}
previoustime9 = currenttime;
}
}

@ undisputed60
Format your code, please.

#include <Servo.h>
Servo sg901;
Servo sg902;
servo sg903;
servo sg904;
servo sg905;
servo sg906;
Servo sg907;
servo sg908;
servo sg909;

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;

void setup() {
  sg904.attach(4);
  sg902.attach(2);
  sg903.attach(3);
  sg901.attach(1);
  sg905.attach(5);
  sg906.attach(6);
  sg907.attach(7);
  sg908.attach(8);
  sg909.attach(9);
}

void loop() {
  currenttime = millis();

  if (currenttime - previoustime4 >= 20) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg904.write(pos);
      delay(10);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg904.write(pos);
      delay(10);
    }
    previoustime4 = currenttime;
  }

  if (currenttime - previoustime6 >= 25000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg906.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg906.write(pos);
      delay(5);
    }
    previoustime6 = currenttime;
  }
  if (currenttime - previoustime1 >= 22000) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg901.write(pos);
      delay(5);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg901.write(pos);
      delay(5);
    }
    previoustime1 = currenttime;
  }
  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;
  }
  if (currenttime - previoustime5 >= 107000) {
    for (pos = 0; pos <= 18; pos += 1) {
      sg905.write(pos);
      delay(5);
    }
    for (pos = 18; pos >= 0; pos -= 1) {
      sg905.write(pos);
      delay(5);
    }
    previoustime5 = currenttime;
  }
  if (currenttime - previoustime7 >= 548000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg907.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg907.write(pos);
      delay(5);
    }
    previoustime7 = currenttime;
  }
  if (currenttime - previoustime8 >= 549000) {
    for (pos = 18; pos >= 0; pos -= 1) {
      sg908.write(pos);
      delay(5);
    }
    for (pos = 0; pos <= 18; pos += 1) {
      sg908.write(pos);
      delay(5);
    }
    previoustime8 = currenttime;
  }
  if (currenttime - previoustime9 >= 26000) {
    for (pos = 58; pos >= 40; pos -= 1) {
      sg909.write(pos);
      delay(5);
    }
    for (pos = 40; pos <= 58; pos += 1) {
      sg909.write(pos);
      delay(5);
    }
    previoustime9 = currenttime;
  }
}
1 Like

put the updating directly below the if-condition

 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

best regards Stefan

1 Like

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.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.