servo on a timer

Ive been trying to make a simple game with 4 servos controlled by 4 potentiometers . Ive got them working with a sensor sheild on my arduino uno with a simple code but wanted to add more features. Ive tried adding an aditional servo which should go from 170 to 10 and back again quickly every 30 seconds but using delay is interupting the control of my other servos. How do i go about adding a timer to the program so that the first 4 servos work independantly from the 5th.

#include <Servo.h>

Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int potpin = A0;
int potpin1 = A1;
int potpin2 = A2;
int potpin3 = A3;
int val;
int val1;
int val2;
int val3;

void setup()
{
myservo.attach(11);
myservo1.attach(10);
myservo2.attach(9);
myservo3.attach(5);
myservo4.attach(6);
}

void loop()

{
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
{
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 179);
myservo1.write(val1);
delay(15);
}
{
val2 = analogRead(potpin2);
val2 = map(val2, 0, 1023, 0, 179);
myservo2.write(val2);
delay(15);
}
{
val3 = analogRead(potpin3);
val3 = map(val3, 0, 1023, 0, 179);
myservo3.write(val3);
delay(15);
}
}

Play around with the blink without delay example in the IDE

Thanks for your quick reply! I think Ive taken the bits I need from the example but it still interupts the servo controls while the servo is moving from open to closed.Is there anthing I can do to give the servo time to move without stopping the controls.

#include <Servo.h>

long previousMillis = 0;
long interval = 10000;
 
Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
 
int potpin = A0;
int potpin1 = A1;
int potpin2 = A2;
int potpin3 = A3;
int val;
int val1;
int val2;
int val3;


void setup() 
{ 
  myservo.attach(11);
  myservo1.attach(10);
  myservo2.attach(9);
  myservo3.attach(5);
  myservo4.attach(6); 
} 
 
void loop() 

{ 
{ 
  val = analogRead(potpin);             
  val = map(val, 0, 1023, 0, 179);      
  myservo.write(val);                   
  delay(15);
}  
{
  val1 = analogRead(potpin1);             
  val1 = map(val1, 0, 1023, 0, 179);      
  myservo1.write(val1);                   
  delay(15); 
}
{
  val2 = analogRead(potpin2);             
  val2 = map(val2, 0, 1023, 0, 179);      
  myservo2.write(val2);                   
  delay(15);
}
{
  val3 = analogRead(potpin3);             
  val3 = map(val3, 0, 1023, 0, 179);      
  myservo3.write(val3);                   
  delay(15);
} 
  unsigned long currentMillis = millis();
 
 if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
     

    myservo4.write(10);
    delay(150);
    myservo4.write(170);
    delay(150);    
    myservo4.write(10);
    delay(150);  
} 
}

but it still interupts the servo controls while the servo is moving from open to closed.Is there anthing I can do to give the servo time to move without stopping the controls.

I don't understand this bit of code:

Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

That is followed by this bit of code:

int potpin = A0;
int potpin1 = A1;
int potpin2 = A2;
int potpin3 = A3;
int val;
int val1;
int val2;
int val3;

This second set of names, using the first bit of code's naming convention, should have used myint1, myint2, ..., myint8 as the names.

Clearly, the 5 servos you have have different purposes. Give the instances names that indicate what they actually do.

Then, we can tell which Servo instance you are talking about in your handwringing statement.

{ 
  val = analogRead(potpin);             
  val = map(val, 0, 1023, 0, 179);      
  myservo.write(val);                   
  delay(15);
}

{What} {are} {these} {curly} {braces} {for} {?}

if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
     

    myservo4.write(10);
    delay(150);
    myservo4.write(170);
    delay(150);    
    myservo4.write(10);
    delay(150);  
}

You might have read the example, but the usual advice is to read, understand, and embrace the blink with delay philosophy. The fact that you left the delay() calls in there means that you didn't really understand, or didn't embrace, the philosophy.

Suppose that YOU are given the job of moving the servo "which should go from 170 to 10 and back again quickly every 30 seconds". Why would you need to hold everything up (delay()) anywhere? You would look at your watch (millis()), and at the designated time, you'd crank the servo around in one step. Now and then, you'd look at your watch, and decide that it was, or wasn't, time to crank it back. You would step back from the game in between times, though.

Thanks for your reply and I probly didnt explain before that im very new to this and im just meddling my way through at this point. I get that you would check your watch to see how long to wait for the servo to move but without knowing how to check your watch and apply that information im in a pickle. Ill try applying the changes you suggested and repost my code for a bit more help

The code you posted does move the servo every 30 seconds, without needing delay(). It's just that the process of moving the servo needs to use delay(). Right?

Well, no, it doesn't. You check now whether to move from 170 to 10 to 170, based on the time you last moved and now and the interval between moves. It should be apparent that you can keep tack of where you are, and determine whether to move from 170 to 10 based on the time you last moved and now and the interval between moves, or determine whether to move from 10 to 170 based on the time you last moved and now and the interval between moves. Clearly, all you need to do differently is decide, when it is time to move, which direction to move. That is based on which direction you moved last time. Also, it should be clear that the interval between moves is not a constant value. If you move from 170 to 10, the interval between moves is 150 ms. If you move from 10 to 170, the interval between moves is 10000 ms (though you said 30000 at one point).

At least, it's clear to me.

I need the servo to stay at 10 for 30 seconds or so then open to 170 and back to 10 for another 30 seconds

I need the servo to stay at 10 for 30 seconds or so then open to 170 and back to 10 for another 30 seconds

How long should it stay at 170? If you think about it, you want the servo to move to a position and stay there for a while. Then, you want to to move to another position, and stay there for a while. Then, you want to to move to another position, and stay there for a while. Then, you want to to move to another position, and stay there for a while.

It should be obvious that the position to move to and the "for a while" bit are all that is different between moving to 10 and moving to 170.

Its an openening and closing action that im looking for from the servo to release a single ball at a time. the servo doesnt need to stay at 170 just needs to open to 170 then come straight back to 10 again and wait. Ive edited the code a bit and its working for what i need it to do with a very small delay it doesnt interupt the controls noticable.

#include <Servo.h>

long previousMillis = 0;
long interval = 10000;
 
Servo paddle1;
Servo paddle2;
Servo paddle3;
Servo paddle4;
Servo ballrel;
 
int control1 = A0;
int control2 = A1;
int control3 = A2;
int control4 = A3;
int val;
int val1;
int val2;
int val3;
int led = 0;


void setup() 
{ 
  paddle1.attach(11);
  paddle2.attach(10);
  paddle3.attach(9);
  paddle4.attach(5);
  ballrel.attach(6);
  pinMode(led, OUTPUT); 
} 
 
void loop() 

{
{ 
  val = analogRead(control1);             
  val = map(val, 0, 1023, 0, 179);      
  paddle1.write(val);                   
  delay(15);
} 
{
  val1 = analogRead(control2);             
  val1 = map(val1, 0, 1023, 0, 179);      
  paddle2.write(val1);                   
  delay(15); 
}
{
  val2 = analogRead(control3);             
  val2 = map(val2, 0, 1023, 0, 179);      
  paddle3.write(val2);                   
  delay(15);
}
{
  val3 = analogRead(control4);             
  val3 = map(val3, 0, 1023, 0, 179);      
  paddle4.write(val3);                   
  delay(15);
} 
  unsigned long currentMillis = millis();
 
 if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
     
    digitalWrite(led, LOW);
    ballrel.write(170);
    delay(150);
    ballrel.write(10);
    digitalWrite(led, HIGH);  
} 
}
if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
     
    digitalWrite(led, LOW);
    ballrel.write(170);
    delay(150);
    ballrel.write(10);
    digitalWrite(led, HIGH);  
}

The way the code is written contradicts this statement:

the servo doesnt need to stay at 170 just needs to open to 170 then come straight back to 10 again and wait.

What you really need is a way to determine that the servo gets to 170. The code above would simply start the servo moving.

Other code would detect when the servo reached 170, and start it moving back. No delay()s at all.

Can you incorporate some kind of limit switch that the servo presses when it gets to 170?

Thanks for using reasonable names. Makes the code much clearer.

I think im ok for now with how it works (because it actually does finally) but im looking to add other features such as light detectors to the "goal line" which would trigger the ball release or maybe a display which keeps score. Im definatly learning as i go mainly trial and error but much thanks to you guys for your help and any ideas on how to make it work would be greatly apreciated

thanks again