merging sketches help! please

Hi Guys,

Im new to this programming lark and have been reading other tutorials on how to merge two sketches and i don't seem to be able to merge them without errors,
I'm trying to control two relays at the same time as operating a servo in a random way for both of them.
i have each sketch separately and they both work but I'm wanting to merge them so they both run together not one after the other.

can anyone help please,
see sketches below.

relay sketch:

void setup() {                
 // initialize the digital pins as an output.
 
 
 pinMode(13, OUTPUT); 
 pinMode(12, OUTPUT);    
}


void loop() {

digitalWrite(13, HIGH);   // Turns ON Relay on digital pin 4
 delay(2000);              // wait for 2 second
 digitalWrite(13, LOW);   // Turns OFF Relay on digital pin 4
 delay(4000);              // wait for 4 second

 digitalWrite(13, HIGH);   // Turns ON Relay on digital pin 4
 delay(1000);              // wait for 1 second
 digitalWrite(13, LOW);   // Turns OFF Relay on digital pin 4
 delay(2000);              // wait for 2 second

 digitalWrite(12, HIGH);    // Turns ON Relay on digital pin 5
 delay(5000);              // wait for 5 second
 digitalWrite(12, LOW);    // Turns OFF Relay on digital pin 5
 delay(500);              // wait for .5 second
 
 digitalWrite(13, HIGH);   // Turns ON Relay on digital pin 4
 delay(2000);              // wait for 2 second
 digitalWrite(13, LOW);   // Turns OFF Relay on digital pin 4
 delay(4000);              // wait for 4 second
 
 digitalWrite(13, HIGH);   // Turns ON Relay on digital pin 4
 delay(3000);              // wait for 3 second
 digitalWrite(13, LOW);   // Turns OFF Relay on digital pin 4
 delay(6000);              // wait for 6 second

 digitalWrite(12, HIGH);    // Turns ON Relay on digital pin 5
 delay(5000);              // wait for 5 second
 digitalWrite(12, LOW);    // Turns OFF Relay on digital pin 5
 delay(500);              // wait for .5 second

 digitalWrite(12, HIGH);   // Turns ON Relay on digital pin 4
 delay(2000);              // wait for 2 second
 digitalWrite(12, LOW);   // Turns OFF Relay on digital pin 4
 delay(4000);              // wait for 4 second

 
}

servo sketch:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
 for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 180 degrees
   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(8);                       // waits 15ms for the servo to reach the position
   
 }
 
 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(8);                       // waits 15ms for the servo to reach the position
   
 }
 
 for (pos = 80; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(40);                       // waits 15ms for the servo to reach the position
   
 }  
 
 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(0);                       // waits 15ms for the servo to reach the position
 
 }
 
 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(9);                       // waits 15ms for the servo to reach the position
 }

 for (pos = 80; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(60);                       // waits 15ms for the servo to reach the position
 }
}

thanks for the future help

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum. I bet your code does not have smileys :slight_smile:

This Simple Stepper Code may get you started.

How can you expect anything to happen on pin 4 with this code?

digitalWrite(13, HIGH);   // Turns ON Relay on digital pin 4

If you want a responsive program don't use delay() anywhere. Have a look at how millis() is used to manage timing without blocking in several things at a time

...R
Planning and Implementing a Program

Thanks for the reply and advice, code now in a code box.
Pin 4 output goes to a separate relay.

john321123:
Pin 4 output goes to a separate relay.

That was NOT the point of my comment. The fact that you THINK you are using pin 4 makes things worse :slight_smile:

Read the line of code more carefully.

...R

Arr I see what you mean now sorry, it's a copied program from some where off the forum.

Hey!

I'm not sure were you're stuck.....
What is it that your trying to achieve?
From what i can tell you want the servos to move in the specified order/arrangement, right?
If so then all you have to do is add void Setup() from the first sketch into the second one and do the same for the void loop(). Remember the order of your code determines the order in which the arduino will act...
Oh and dont forget to change the pin number to whatever you want!!
Its a good habit to put include statements at the very top of your code.

Best of Luck!!