3 servo motors and "delay" problems

What I am trying to programmate ?

  • I build up table tennis robot like robopong,butterfly ammicus etc…
    -If I tryed to motors sepparatelly everything was great, but If I used everything in whole one arduino program, everymotor was freezing and not work properly.

I need help please. I am a newbie in arduino programming and I am using much delays and then everything freezes and is delayed.

Please help me to rewrite the arduino code. Thank you very much.

#include <Servo.h>

Servo servo1; // feed servo
Servo motor1;  // brushless main motor
Servo otacachor; //  horizontal rotation servo
int malymot =12; //small motor,helps feed servo to feed balls
int led = 13;  //just led
int potpin1 = A0;  // potenciometer  for main brushless motor
int potpin2 = A3; // potenciometer for servomotor(servo1) (feed servo)
int potpin3= A5; //potenciometer for horizontal rotation servo
int val1 ;     // motor1
int val2;    // servomotor
int val3;   // motor for horizontal rotation
int random1; //randomizer for servo
int random2; //randomizer for horizontal motor
int toogleservo1 = 5 ; //toogle switch for activate the randomizer for feed servo
int toogleservo2= 6; //toogle switch for activate the randomizer for horizontal rotation servo
void setup()
{  pinMode(led, OUTPUT);
  pinMode(malymot, OUTPUT);
  pinMode (toogleservo1, INPUT);
 pinMode (toogleservo2, INPUT);
  motor1.attach(9);  // attaches the servo on pin 9 to the servo object

void loop() 
  { int toogleservo1ON = digitalRead (toogleservo1);
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023) 
     val2 = map(val2, 0, 1023, 2000, 500);  //waiting between 0,5 - 2sek
  if (toogleservo1ON == HIGH){   // randomizer for horizontal servo
       random1 = random (500,2000);  
       servo1.write (172);
       delay (500);
       servo1.write (30);
       digitalWrite(malymot, HIGH);
       digitalWrite(led, LOW);}
    else if (val2 > 1900){    //val2 is for potentiometer just to stop the feed servo
         servo1.write (30);
         delay (15);
         digitalWrite(malymot, LOW);
         digitalWrite(led, HIGH);}
    else if (val1<110){    //to not feed the robot until the brushless motor doesnt work
         servo1.write (30);
         delay (15);
         digitalWrite(malymot, HIGH);
         digitalWrite(led, HIGH);}
    else {
         servo1.write (172);
     delay (500);
     servo1.write (30);
     digitalWrite(malymot, HIGH);
     digitalWrite(led, HIGH);}
  val1 = analogRead(potpin1);            // the main brushless motor 
  val1 = map(val1, 0, 1023, 60, 170);    
  delay (15);
  int toogleservo2ON = digitalRead (toogleservo2);
  val3= analogRead(potpin3);       // horizontal rotation servo
  val3 = map(val3,0,1023,0,180);
   if (toogleservo2ON == HIGH){   // randomizer for horizontal servo
       random2 = random (70,110);  //random the angle of rotation
       otacachor.write (random2);
       delay (val2);    //to delay  the time the feed servo delays

I am using much delays

The correct number of calls to delay() is ZERO.

  { int toogleservo1ON = digitalRead (toogleservo1);

Why is there a useless curly brace there?

You need to completely rewrite your code. The loop() function should do only positive things - read switches, add numbers, move a servo, record when actions happen, etc. It should NOT do negative things, like waste time.

You need to keep track of whether a servo needs to be moved, and, if so, where is needs to move to next, and when it moved last.

You need to do this independently for each servo.

thank you for the reply, could you help me to rewrite the code please ? I am totally noob in programming and dont really understand what you mean… Thank you very much, I really appreciate your effort

See this post: Doing several things at a time and also look up 'State machine'.

@Henry_Best has a glitch in his link. This one works several things at a time


Robin2: @Henry_Best has a glitch in his link. This one works several things at a time

Reminder to self: Do not use forum's post link icon!

Reminder to self: Do not use forum’s post link icon!

It works fine when your edit window is in “View Source” mode.