How do i combine two Arduino sketches?

The first arduino sketch:
int Distance = 0; // Record the number of steps we've taken

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}

void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step

// Check to see if we are at the end of our move
if (Distance == 3600)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(500);
}
}

The second arduino sketch:
#define DISTANCE 3200

int StepCounter = 0;
int Stepping = false;

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

pinMode(3,INPUT);
}

void loop() {
if (digitalRead(3) == LOW && Stepping == false)
{
Stepping = true;
}

if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

I have tested both programs individually and it worked.
I was wondering if if i can combine both programs together.
Can someone please help? :slight_smile:
Thank you!! Appreciate much!! :slight_smile:

I have tried: #define DISTANCE 200

int StepCounter = 0;
int Stepping = false;
int Distance2 = 0; // Record the number of steps we've taken
void setup1(void) {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void setup2(void) {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

pinMode(3,INPUT);
}
void loop1(void) {
digitalWrite(9, HIGH);
delayMicroseconds(1200);
digitalWrite(9, LOW);
delayMicroseconds(1200);
Distance2 = Distance2 + 1; // record this step

// Check to see if we are at the end of our move
if (Distance2 == 100)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance2 = 0;
// Now pause for half a second
delay(500);
}
}
void loop2(void) {
if (digitalRead(3) == LOW && Stepping == false)
{
Stepping = true;
}

if (Stepping == true)
{
digitalWrite(9, HIGH);
delayMicroseconds(1200);
digitalWrite(9, LOW);
delayMicroseconds(1200);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}
//combine setup
void setup(void){
setup1();
setup2();
}
//combine loop
void loop(void){
loop1();
loop2();
}

Unlucky that it didnt work :frowning: the motor operates without pressing button.

Move the contents of both setup()s into one setup().

Redundant lines can be deleted.

Move the contents of both loop()s into one loop().

You may have to adjust code so things will run properly.

With the changes made above:
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

For a more detailed talk through of this see:- Merging Code