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);


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);



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);
  digitalWrite(4, HIGH); //enable pullups to make pin high

void loop()
  press1 = digitalRead(button1);
  if (press1 == LOW)
  else {

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


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.


This link also shows you how to do this:-