How do i mesh separate code together (i cant wrap my head around the top post)

Hey guys, new here just working out the basics… been playing around with some free code for a joystick module i bought (and some hobby grade servos) however it didn’t have example code for the button thats included with the joystick board, so i found some free code that allows me to control a servo using the button only… but for the life of me i cannot work out how to combine 2 bits of code together so i can use both joystick and button control at once. any help would be most appreciated :smiley:

This is the joystick code

#include <Servo.h>

Servo myservo;
Servo myservo2;

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorPin2 = A1;
int buttonPin = 2;
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;
int servoPin = 9;
int servoPin2 = 10;
int maxServo = 180;
int minServo = 0;
int maxPotValue = 1023;

void setup() {

 // declare the ledPin as an OUTPUT:
 pinMode(ledPin, OUTPUT);
 pinMode(buttonPin, INPUT);

 myservo.attach(servoPin);
 myservo2.attach(servoPin2);
}

void loop() {

 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 sensorValue2 = analogRead(sensorPin2);   

 myservo.write(map(sensorValue, 10, maxPotValue, minServo, maxServo));
 myservo2.write(map(sensorValue2, 10, maxPotValue, minServo, maxServo));

 if (digitalRead(buttonPin)) {
    digitalWrite(ledPin, true);
 } else {
    digitalWrite(ledPin, false);
 }

 delay(25);                  

}

and this is the button code

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Don't bother wasting your time looking for this topic's identical twin, I deleted it.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME

This Simple Merge Demo should get you started.

You must make sure that the two programs don't both use the same hardware - such as an I/O pin.

...R

This link also shows you how to do this:- http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html