combining sketches

Hi

I got some trouble combining these two codes can you help me?

the codes are

#include <Servo.h>

const int buttonPin = 2;

const int buttonPin2 = 4;

int buttonState = 0;

int buttonState2 = 0;

Servo servoA;

int position = 0;

void setup() {

servoA.attach(9);

pinMode(buttonPin, INPUT);

pinMode(buttonPin2,INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

buttonState2 = digitalRead(buttonPin2);

if(buttonState ==HIGH && position < 180){

servoA.write(position++);

delay(5);

}

if(buttonState2 == HIGH && position > 3){

servoA.write(position--);

delay(5);

}

}

and

int enablePin = 11;

int in1Pin = 10;

int in2Pin = 9;

int switchPin = 7;

int potPin = 0;

int statusPin= 13;

void setup()

{

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT);

pinMode(enablePin, OUTPUT);

pinMode(switchPin, INPUT_PULLUP);

pinMode(statusPin,OUTPUT);

}

void loop()

{

digitalWrite(13,HIGH);

int speed = analogRead(potPin) / 4;

boolean reverse = digitalRead(switchPin);

setMotor(speed, reverse);

}

void setMotor(int speed, boolean reverse)

{

analogWrite(enablePin, speed);

digitalWrite(in1Pin, ! reverse);

digitalWrite(in2Pin, reverse);

}

(Quote tags changed to Code tags by Moderator - use the </> button next time)

Perhaps you should post the combined code you have so far, and say what's right and wrong with it.

You can only have one setup() and one loop().
So, start with one sketch and move stuff from the other into it.

  1. Copy the stuff that is before setup() into the area before setup().

  2. Copy the stuff in setup() into setup().

  3. Copy the stuff in loop() into loop().

  4. Copy any functions that are outside of loop and put them after loop.

  5. Take care of any duplicate pin names, variable names, etc. Compile and fix any errors.

Then check that the combined loop actually does what you want.

Steps 1-4 are pretty easy, and and actually compiled without errors. Rest is up to you now:

#include <Servo.h>

const int buttonPin = 2;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState2 = 0;

Servo servoA;

int position = 0;
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int potPin = 0;
int statusPin = 13;

void setup() {

  servoA.attach(9);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(statusPin, OUTPUT);
}

void loop() {

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState == HIGH && position < 180) {
    servoA.write(position++);
    delay(5);
  }

  if (buttonState2 == HIGH && position > 3) {
    servoA.write(position--);
    delay(5);
  }

  digitalWrite(13, HIGH);

  int speed = analogRead(potPin) / 4;
  boolean reverse = digitalRead(switchPin);
  setMotor(speed, reverse);
}

void setMotor(int speed, boolean reverse)
{
  analogWrite(enablePin, speed);
  digitalWrite(in1Pin, ! reverse);
  digitalWrite(in2Pin, reverse);
}

Okay thanks for your help

This Simple Merge Demo may help.

...R