arduino+lcd+keypad+2 stepper motor position control question.

Hi,

I am new to this forum, i have programing question i need help with,
i found this code. i am typing on the keypad the decimal number and making the stepper moves assigned decimal number . and everything works fine ( maybe it need fixing some keypad coding ) , what i need is i want to add one more stepper motor and do same think with second motor and when i done with it i want to go back previous motor, i don't need both motor working same time just need to change motors using one keypad and lcd,
i want program says select motorA or MotorB ,then i want to select A , do the code then go back select B do the code .
and also is it possible to keep track when i go back motor A or B their current position or it has to reset 0.
thank you for your passion to read and any help appreciated.

#include <AccelStepper.h> // AccelStepper Library
#include <Keypad.h> // Keypad Library
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#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
byte index = 0;
char numbers[16]; // Plenty to store a representation of a float

// 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 stepper(1, A2, A3); // 1 = Easy Driver interface
// Arduino A2 connected to STEP pin of Easy Driver
// Arduino A3 connected to DIR pin of Easy Driver

static char outstr[15];

void setup(void) {

float len=000.000;

// AccelStepper speed and acceleration setup
stepper.setMaxSpeed(22000);
stepper.setAcceleration(10000);

{

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

}
}

void loop()

{

char key = keypad.getKey();
if(key != NO_KEY)

lcd.print(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 == '#')
{
float len = atof(numbers); // Do whatever you need to with len

dtostrf(len,7, 3, outstr);

lcd.clear();
lcd.setCursor (13,3);
lcd.print(outstr);

stepper.runToNewPosition(len*2032); // 800 steps for 1 cm

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

}
}
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into sections is fine but large spaces for no reason just make for more scrolling when we're trying to read your code.

When your code requires a library that's not included with the Arduino IDE please always post a link(using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

First you're going to need to add a global variable that will be used to remember which motor is selected.

byte currentMotor = motorAidentifier;

Create an AccelStepper object for each motor:

AccelStepper motorA(1, A2, A3); 
AccelStepper motorB(1, 8, 9);

Initialize both steppers in setup().

You'll need to implement the code for changing the value of currentMotor.

Then you can use the value to currentMotor to determine which motor moves:

if(currentMotor == motorAidentifier) {
  motorA.runToNewPosition(len*2032);        //  800 steps for 1 cm 
}
else {
  motorB.runToNewPosition(len*2032);        //  800 steps for 1 cm 
}

Of course that could be refined a bit but it gets you started.

ozangulec:
i want program says select motorA or MotorB ,then i want to select A , do the code then go back select B do the code .

It's not clear what that means but you can do things like this:

if(currentMotor == motorAidentifier) {
  motorA.runToNewPosition(len*2032);        //  800 steps for 1 cm 
  currentMotor = motorBidentifier;
}  
else {
  motorB.runToNewPosition(len*2032);        //  800 steps for 1 cm 
}

ozangulec:
and also is it possible to keep track when i go back motor A or B their current position or it has to reset 0.

Of course you can keep track of the position. Most simple is to use a global for each:

unsigned int motorAposition = 0;
unsigned int motorBposition = 0;
if(currentMotor == motorAidentifier) {
  motorA.runToNewPosition(len*2032);        //  800 steps for 1 cm 
  motorAposition = len;
}
else {
  motorB.runToNewPosition(len*2032);        //  800 steps for 1 cm 
  motorBposition = len;
}

Much better would be to use arrays indexed by the value of currentMotor, which would prevent the duplicate code, but it might be slightly more advanced than you're ready for now since you're already struggling with fairly basic concepts.

You forgot the Auto Format I asked you to use in my previous reply.