How to separate the two programs.

Hello dear Arduino fans.

I started with Arduino code, but my first problem ocured so quickly.

I need start 2 independent programs from one code in same time. The mini programs works separately, but I need split from one code.

Will be possible help me with this please?

//First code:

#define Relay1 4

void setup()

{

pinMode(10,INPUT_PULLUP);
pinMode(11,INPUT_PULLUP);

pinMode(Relay1,OUTPUT);
pinMode(Relay1,HIGH);

}
void loop()
{

while (digitalRead(10) == HIGH)

digitalWrite(Relay1,LOW);
delay(500);

while(digitalRead(11) == HIGH)

digitalWrite(Relay1,HIGH);
delay(500);
}

//second code

#include <Servo.h>
Servo myservo;
#define Relay1 4

void setup()
{

pinMode(7,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);

pinMode(Relay1,OUTPUT);
pinMode(Relay1,HIGH);

myservo.attach(9);
myservo.write(0);

}

void loop()
{
while (digitalRead ( 8 ) == HIGH)
myservo.write(20);
Serial.print("open");
Serial.println("");
delay(5000);

myservo.write(180);
Serial.println("");
Serial.println("closed");
Serial.print("waiting 30 second");
Serial.println("");
digitalWrite(Relay1,HIGH);

while(digitalRead (7) == HIGH)

myservo.write(180);
digitalWrite(Relay1,LOW);
Serial.println("");
Serial.println("closed");
Serial.print("waiting 30 second");
Serial.println("");
delay(50);

myservo.write(20);
Serial.print("open");
Serial.println("");

}

// Thank you for some feedback.

Juraj

Please use [ code ] tags. The forum software eats some of your code if you don't.

Look at the example Blink_Without_Delay. It's in the "02 Digital" section of the examples in the IDE.

Try to re-write the first one in that way - it's almost identical. The second one will be a little more involved but should not take more than a day or two on your first attempt.

The demo Several Things at a Time is an extended example of BWoD. It may help with understanding the technique.

...R