How do i combine 2 codes in 1 Arduino mega? (capping machine)

Hey, I have 2 separate codes for my capping machine that i need to combine into 1 for my Arduino Mega. The first code is for a LCD display to count the cap's in the holister and the second one is for the machine that tightens the caps.

I want 2 combine the 2 codes into 1 for my Arduino Mega...

What is the best way to combine the 2?
Or is there someone that can put them together for me?

Thank you in advance! :slight_smile:

CODE 1 (Display Cap Counter)

//YWROBOT

//Compatible with the Arduino IDE 1.0

//Library version:1.1

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

// this constant won't change:

const int Up_buttonPin = 2; // the pin that the pushbutton is attached to

const int Down_buttonPin = 3;

// Variables will change:

int buttonPushCounter = 0; // counter for the number of button presses

int up_buttonState = 0; // current state of the up button

int up_lastButtonState = 0; // previous state of the up button

int down_buttonState = 0; // current state of the up button

int down_lastButtonState = 0; // previous state of the up button

bool bPress = false;

void setup()

{

Serial.begin(9600);

pinMode( Up_buttonPin , INPUT_PULLUP);

pinMode( Down_buttonPin , INPUT_PULLUP);

lcd.init(); // initialize the lcd

// Print a message to the LCD.

lcd.backlight();

lcd.setCursor(0,0);

lcd.print("Please Select:");

lcd.setCursor(2,1);

lcd.print(buttonPushCounter);

}

void loop()

{

checkUp();

checkDown();

if( bPress){

   bPress = false;

  lcd.setCursor(2,1);

  lcd.print("               ");

  lcd.setCursor(2,1);

  lcd.print(buttonPushCounter);

}

}

void checkUp()

{

up_buttonState = digitalRead(Up_buttonPin);

// compare the buttonState to its previous state

if (up_buttonState != up_lastButtonState) {

// if the state has changed, increment the counter

if (up_buttonState == LOW) {

    bPress = true;

  // if the current state is HIGH then the button went from off to on:

  buttonPushCounter++;

  Serial.println("on");

  Serial.print("number of button pushes: ");

  Serial.println(buttonPushCounter);

} else {

  // if the current state is LOW then the button went from on to off:

  Serial.println("off");

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state, for next time through the loop

up_lastButtonState = up_buttonState;

}

void checkDown()

{

down_buttonState = digitalRead(Down_buttonPin);

// compare the buttonState to its previous state

if (down_buttonState != down_lastButtonState) {

// if the state has changed, increment the counter

if (down_buttonState == LOW) {

    bPress = true;

  // if the current state is HIGH then the button went from off to on:

  buttonPushCounter--;

 

  Serial.println("on");

  Serial.print("number of button pushes: ");

  Serial.println(buttonPushCounter);

} else {

  // if the current state is LOW then the button went from on to off:

  Serial.println("off");

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state, for next time through the loop

down_lastButtonState = down_buttonState;

}

CODE 2 (Cap Tightener and Conveyor)

int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;
int motor3pin1 = 6;
int motor3pin2 = 7;
int motor1ENA = 9;
int motor2ENB = 10;
int motor3ENA = 11;

void setup() {
// put your setup code here, to run once:
pinMode (12, INPUT);
pinMode (13, INPUT);

pinMode (motor1pin1, OUTPUT);
pinMode (motor1pin2, OUTPUT);
pinMode (motor2pin1, OUTPUT);
pinMode (motor2pin2, OUTPUT);
pinMode (motor3pin1, OUTPUT);
pinMode (motor3pin2, OUTPUT);
pinMode (motor1ENA, OUTPUT);
pinMode (motor2ENB, OUTPUT);
pinMode (motor3ENA, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

//Controling speed (0 = off and 255 = max speed):
analogWrite(motor1ENA, 250); // ENA pin
analogWrite(motor2ENB, 250); // ENB pin
analogWrite(motor3ENA, 250); // ENA pin

//Controlling spin direction of motors:
if (digitalRead(12) == HIGH){
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
}
else{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
}

if (digitalRead(13) == HIGH){
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
digitalWrite(motor3pin1, HIGH);
digitalWrite(motor3pin2, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
delay(4000); // Wait for 4000 millisecond(s)
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
delay(5000); // Wait for 5000 millisecond(s)
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
}
else{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
digitalWrite(motor3pin1, LOW);
digitalWrite(motor3pin2, LOW);
}

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Get rid of all the delays.
Come up with a sound specification.
Tell us what the pins are wired to - don't make us guess.
Post your best effort, or advertise in the "jobs and paid consultancy" section.

Remember to use code tags when posting code.

Count the cap's what?

What is a "holister"?

by replacing the delay()s with non-blocking timing based on millis().

The best way to combine the two sketches is to follow these instructions

for more details

https://www.google.de/search?as_q=arduino+merge+two+sketches
best regards Stefan