running 2 loops simultaneously

Hello all. I'm pretty new to arduino and code in general. I've taken class before, but have never really been good with "If, for, while, do, else" statements. Structure is my biggest weakness in coding. What I'm trying to accomplish is to have 2 LEDs blinking (alternating) while that happens I can control a servo to move back and forth based on my potentiometer. Sounds simple right? Probably is too, but after trying multiple codes and researching I can't find a solution on my own.

I've tried making another function after and before the loop function. ~no good. I suspect because no piece of code calls the program to enter the function.

Tried a while statement. ~I suspect this might be a valid solution, but i can't get the structure of the code correct.

Put servo code in loop above led code.~ won't work, Code loops and reads one at a time. There is a delay when pot is turned (has to finish previous code first)

So here is the code:

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
 int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 


int led = 10; // sets "led" to pin 10
int led2 = 8;// sets "led2" to pin 8

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT); 
 myservo.attach(9); // set servo control to pin 9
  
}

// the loop routine runs over and over again forever:
void loop() 
{
analogRead(potpin); //read pot on analog pin 0
val = map(val, 0, 1023, 0, 179); //set servo parameters 
myservo.write(val); // set servo position
delay (15); // wait until servo gets there
  
}

//code for led blink (ideally supposed to be part of loop function) 
/*  void blink() {
   delay(15);             
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(250);               // wait for a second
  digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
  delay(250);               // wait for a second
  }
  */

so if anyone could help me out that would be great. This is just me exploring the "situation" so i can put 2 and 2 together on my bigger project

When you call delay() the Arduino will do nothing useful for that amount of time. To interleave two operations you should use the "BlinkWithoutDelay" example as a model for your blink code.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo  
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 


const int ledPin =  8;// the number of the LED pin
const int ledPin2 =  10;// the number of the LED pin
// Variables will change:
int ledState = LOW; 
int ledState2 = LOW;// ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  pinMode(ledPin2, OUTPUT);  
  myservo.attach(9); 
}

void loop()
{
  
/* analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);*/
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
      
      if (ledState == HIGH)
      ledState = HIGH;
       else
        ledState2 = LOW;
        
      if (ledState2 == HIGH)
       ledState2 = HIGH;
        else
        ledState = LOW;
        
        if (ledState2 == LOW)
        ledState2 = HIGH;
        else
        ledState2 = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    digitalWrite(ledPin2, ledState2);
    

  }
}

Thanks for the help!! Figured out how to alternate them. That was a accomplishment for me as i finally got some if and else statements working. Seems like a lot of code for a simple task. Are shift registers what i need? Also I greyed out the loop code for the servo. It doesn't work. I'm not sure why. without a delay is moves upon resting, then never again. With delay it does nothing.

The example for "Two LEDs, millis() timer, no delay()" is here Blink Two LEDs, independent, no delay - Programming Questions - Arduino Forum

Msquare:
The example for "Two LEDs, millis() timer, no delay()" is here http://arduino.cc/forum/index.php/topic,76140.msg574940.html#msg574940

Thanks for this this helps a lot. New code to practice. At first it didn't work. I found my copy paste skills are shaky as well.

Here is what I have

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo  
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 


/* Multiple independent delay - Two blinking LEDs

Working code example that blinks two LEDs, each with its own independent period.
This extends the BlinkWithoutDelay example of a single LED, showing how to
implement multiple independent timing paths in the main loop.

Written by several forum members. In public domain.  Oct 2011.

Note: works even when millis () wraps around to zero after approximately 50 days.
*/

// Which pins are connected to which LED
const byte greenLED = 8 ;
const byte redLED = 10 ;

// Time periods of blinks in milliseconds (1000 milliseconds to a second).
// Time variable and constants are always unsigned long
const unsigned long greenLEDinterval = 555UL ;
const unsigned long redLEDinterval = 1234UL ;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer = 0 ;
unsigned long redLEDtimer = 0 ;

// Variable to know what the current LED state is
int greenLEDState = LOW ;
int redLEDState = LOW ;

void setup() {
  pinMode (greenLED,OUTPUT) ;
  pinMode (redLED,OUTPUT) ;
  greenLEDtimer = millis () ;
  redLEDtimer = millis () ;
    myservo.attach(9); 
}

void loop() {

// Handling the blink of one LED.
// First, check if it is time to change state
  if ( (millis () - greenLEDtimer) >= greenLEDinterval ) {
    // It is time to change state. Calculate next state.
    if (greenLEDState == LOW)
      greenLEDState = HIGH ;
    else
      greenLEDState = LOW ;
    // Write new state
    digitalWrite (greenLED, greenLEDState ) ;
    // Reset timer
    greenLEDtimer = millis () ;
  }

// The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval ) {
    if (redLEDState == LOW)
      redLEDState = HIGH ;
    else
      redLEDState = LOW ;
    digitalWrite (redLED, redLEDState ) ;
    redLEDtimer = millis ()  ;
  }

val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
/* Other code that needs to execute goes here.
   It will be called many thousands of times per second because the above code
   does not wait for the LED blink interval to finish. */

}

I took the basic servo tutorial in examples list and combined it with the code you provided. Works just like i wanted. Thanks.

you can use my timer library, very simple to use. no delays, small footprint.

after you import the library, check the sample and test it.

/*
      Title: Simple Switch Timer
      Library: sstimer.ino
      Description: 
      Created Date: January 17, 2015
      Created By: Rolly Falco Villacacan
      Released into the public domain.
*/

#include <sstimer.h>

//WATER_PUMP SETUP
int PIN_NO1 = 13;
long unsigned OFF_DURATION = 500; //in milliseconds HOW LONG YOU WANT THE PIN TO BE OFF STATE
long unsigned ON_DURATION = 500; //in milliseconds //HOW LONG YOU WANT THE PIN TO BE ON STATE

//LIGHTHING SETUP
int PIN_NO2 = 12;
long unsigned OFF_DURATION2 = 1000; //in milliseconds
long unsigned ON_DURATION2 = 1000; //in milliseconds


sstimer WATER_PUMP(PIN_NO1, ON_DURATION, OFF_DURATION); //create instance for water pump
sstimer LIGHTHING(PIN_NO2, ON_DURATION2, OFF_DURATION2);//create instance for lighting

void setup(){
//...  
}


void loop(){
//...
WATER_PUMP.startsstimer();
LIGHTHING.startsstimer();
//..  
}

The demo several things at a time and the Thread planning and implementing a program illustrate the use of LEDs and servos operating simultaneously.

...R