how to put this 2 code together

Hi,

I have 2 codes blow, i use for linear accuator to positional stops. code 1 does sets the 0 point or home position, Code 2 is the main code does all the calculations stops and positions on the actuator,
how to put this two code in one code ? when i turn Arduino on i need program set home position only ones like the code 1, then continue Code 2,

thanks for advice.
Oz.

// This code Sets motor position 0 //

#include <AccelStepper.h>
AccelStepper stepperA(1, A0, A1);

int homeButton = 8;
int RedLed = 7;
byte homeval;

void setup() {

  stepperA.setMaxSpeed(1000);
  stepperA.setAcceleration(800);
  pinMode(homeButton, INPUT);
  pinMode(RedLed, OUTPUT);
  stepperHome();  //runs routine to home motor
}
void loop() {
  stepperA.moveTo(400);   // random position to end for testing
  stepperA.runToPosition();
}
void stepperHome() {   //this routine should run the motor
  homeval = digitalRead(homeButton);
  while (homeval == HIGH)
  {
    stepperA.moveTo(-600); //backwards slowly till it hits the switch and stops
    stepperA.run();
    digitalWrite(RedLed, HIGH);   //indicates it's doing something
    homeval = digitalRead(homeButton);
  }
  digitalWrite(RedLed, LOW);   //indicates it's doing something
  stepperA.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}
/code]




[code]// this is main calculator code for the stepper motor//
#include <AccelStepper.h> // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//LCD Setup----------------------------------------------------------
#define I2C_ADDR 0x3F  // Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
// Global variables--------------------------------------------------
static char outstr[15];
byte index = 0;
byte homeval;
char numbers[16]; // Plenty to store a representation of a float
int RedLed = 7;
int homeButton = 8;
// Keypad Setup----------------------------------------------------
const byte ROWS = 4;   // Four Rows
const byte COLS = 4;   // Four Columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', '.'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition
// AccelStepper Setup-----------------------------------------------
AccelStepper stepperA(1, A0, A1);  // 1 = Easy Driver interface

void setup(void) {

  float len = 000.000;
  //  AccelSteppers speed and acceleration setup
  stepperA.setMaxSpeed(1500);
  stepperA.setAcceleration(800);
  pinMode(RedLed, OUTPUT);
  {
    lcd.begin (20, 4); //  our LCD is a 20x4, change for your LCD if needed

    // LCD Backlight ON
    lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
    lcd.setBacklight(HIGH);
  }

  int mode = 0;
  lcd.setCursor(0, 1);
  lcd.print("Select cutting stop");
  lcd.setCursor(0, 3);
  lcd.print("VERTCL[A]  HORIZN[B]");
}

void loop() {
  char key = keypad.getKey();
  if (key != NO_KEY)
    lcd.print(key); //not sure this is correct because it prints every key
  {
    if (key == 'C')
    {
      index = 0;
      numbers[index] = '\0';
    }

    else if (key == '.')
    {
      numbers[index++] = '.';
      numbers[index] = '\0';
    }
    else if (key >= '0' && key <= '9')
    {
      numbers[index++] = key;
      numbers[index] = '\0';
    }
    else if (key == '#')
    {
      digitalWrite(RedLed, HIGH);
      float len = atof(numbers);
      dtostrf(len, 7, 3, outstr);

      lcd.clear(); lcd.setCursor (0, 1); lcd.print( " Wait..."); lcd.setCursor (4, 3); lcd.print("cur-pos");
      lcd.setCursor (13, 3); lcd.print(outstr); lcd.setCursor (0, 0); lcd.print("    Vertical cut                        ");

      stepperA.runToNewPosition(len * 1354.666666665203627);      //  800 steps for 1 cm 2032 for T2, 1354.667 T

      index = 0;
      numbers[index] = '\0';

      digitalWrite(RedLed, LOW);
      lcd.clear();   lcd.setCursor (4, 3); lcd.print("cur-pos"); lcd.setCursor (13, 3);
      lcd.print(outstr); lcd.setCursor (0, 0); lcd.print("    Vertical cut                        ");
    }
  }
}
/code]

Code tags are great, aren't they?

I'm starting a project with a home button but using a hall effect sensor so I thought I would comment to keep track of your progress and see if I can learn while you learn.

Your first block of code is missing the "[" at the [/code] tag.

stepperA.runToNewPosition(len * 1354.666666665203627);

What are we measuring here? Protons? (Sorry, just trying to make a joke.) :)

Please don't do this...

      lcd.clear(); lcd.setCursor (0, 1); lcd.print( " Wait..."); lcd.setCursor (4, 3); lcd.print("cur-pos");
      lcd.setCursor (13, 3); lcd.print(outstr); lcd.setCursor (0, 0); lcd.print("    Vertical cut                        ");

It's a thousand times easier to debug if every statement has its own line. Yes this is legal C code but it's not a good idea.

Normally the "Plz combine 2 codez" question is very difficult to answer. Yours is very easy. First take the stepperHome(){...} function and copy it into the second program. Put it anywhere that's not already inside the {} braces of another function. Then call that function from setup() exactly like the first program does.

Thanks for the feedback. It is good to know it worked.