The code below is not written for you but is something I wrote a while back, and may give you some ideas. It runs 2 steppers simultaneously, with blink without delay on pin 14 to prove nothing blocks, and even a button to change the speed on the fly...
// inspired by https://www.makerguides.com/28byj-48-stepper-motor-arduino-tutorial/
// 19 august 2019
// the accelstepper library... non-blocking
// trying 2 motors
/*
Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO.
More info: https://www.makerguides.com
*/
// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
//motor 1
#define motorPin1_1 8 // IN1 on the ULN2003 driver
#define motorPin1_2 9 // IN2 on the ULN2003 driver
#define motorPin1_3 10 // IN3 on the ULN2003 driver
#define motorPin1_4 11 // IN4 on the ULN2003 driver
//motor 2
#define motorPin2_1 4 // IN1 on the ULN2003 driver
#define motorPin2_2 5 // IN2 on the ULN2003 driver
#define motorPin2_3 6 // IN3 on the ULN2003 driver
#define motorPin2_4 7 // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1_1, motorPin1_3, motorPin1_2, motorPin1_4);
AccelStepper stepper2 = AccelStepper(MotorInterfaceType, motorPin2_1, motorPin2_3, motorPin2_2, motorPin2_4);
//bwod to see if it blocks or not
unsigned long previousBlink;
int blinkTerval = 250;
bool blinkState = false;
byte blinkPin = 14;
// going to use a button to change speed on the fly
int basicSpeed = 500;
int motor1speed;
int motor2speed;
byte speedPin = 2;
void setup()
{
Serial.begin(9600);
Serial.println(".... Accelstepper constant speed, 2 motors ....");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
//turn off L13
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(blinkPin, OUTPUT);
digitalWrite(blinkPin, blinkState);
pinMode(speedPin, INPUT_PULLUP);
// Set the maximum steps per second:
stepper.setMaxSpeed(1000);
stepper2.setMaxSpeed(1000);
Serial.println("\nsetup() done\n");
} //setup
void loop()
{
getSpeed(); //depends on button
doSteps();
doPulse();
} //loop
void doPulse()
{
if (millis() - previousBlink >= blinkTerval)
{
previousBlink = millis();
blinkState = !blinkState;
digitalWrite(blinkPin, blinkState);
}
}//pulse
void getSpeed()
{
if (digitalRead(speedPin)) //not pressed, normal speed
{
motor1speed = basicSpeed;
motor2speed = basicSpeed;
blinkTerval=500;
}
else //pressed, motor 1 slow, motor 2 fast
{
motor1speed = 0.2 * basicSpeed;
motor2speed = 2.0 * basicSpeed;
blinkTerval=100;
}
// Set the actual speed of the motor in steps per second:
stepper.setSpeed(motor1speed);
stepper2.setSpeed(motor2speed);
}//getspeed
void doSteps()
{
// Step the motor with constant speed as set by setSpeed():
stepper.runSpeed();
stepper2.runSpeed();
}//steps
yes i get it but using run() or runSpeed() instead of runToposition() doesnt make the motor move simultaneously.
iam making some mistakes in the loops i guess.
using run() inside while loop makes the motor turn only a single step.
if i use run() outside while loop and inside void loop then motor turns continously.
after i upload this code both motors move one step back and forth continuosly.if i enter two values in the serial monitor (30,50) or (30,80)
both motors are not moving.
if i enter only one value say 30 or 50 then only one motor moves to the given steps and other is not moving.
Your program gets values from the user into two variable nos and noos. As the names are meaningless I have no idea what you want to do with those values.
Always use meaningful variable names - for example stepper1NumberOfSteps or stepper1MillisBetweenSteps. Apart from anything else it makes the logic more obvious.
ok i have changed the variable names.then what is the problem with the code now?
why isnt it working properly?
why are the motors not working together?
can u find the mistakes in the logics?
barath1997:
ok i have changed the variable names.then what is the problem with the code now?
why isnt it working properly?
why are the motors not working together?
can u find the mistakes in the logics?
There seems to be a huge gap in your understanding of how a program works. Changing the names of variables makes no difference to the Arduino. That is only to make things easier for the programmer.
You have the two variables that get data from the user, but you have no code within your program to use that data. How can you expect the program to respond to the user input?
You need lines like stepperX.move(stepperXnumberofsteps); to make use of the data from the user
Have you carefully studied the examples that come with the AccelStepper library so you can see how the programs should be constructed?
...R
PS ... It is not a sin if you don't want to learn programming. If you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.
barath1997:
ya done,but still the issue is not resolved one one motor moves and the other rests still.
Your description of how the program works is not very detailed. For example you don't say how the data entry part of the program is working.
My guess is that it is not actually taking in two numbers for the two motors. What do you see if you print the values of stepperXnumberofsteps immediately after the moveTo() lines?
Using Serial.print() statements to see how variables change is a normal part of debugging a problem.
NOTE also that I suggested stepper.move() rather than stepper.moveTo(), though it won't make any difference in the short term.
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepperX(1, 3, 4); // 1 = Easy Driver interface
// UNO Pin 2 connected to STEP pin of Easy Driver
// UNO Pin 3 connected to DIR pin of Easy Driver
AccelStepper stepperZ(1, 6, 7); // 1 = Easy Driver interface
// UNO Pin 5 connected to STEP pin of Easy Driver
// UNO Pin 6 connected to DIR pin of Easy Driver
// Stepper Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
long TravelZ; // Used to store the Z value entered in the Serial Monitor
int move_finished=1; // Used to check if move is completed
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
// Print out Instructions on the Serial Monitor at Start
Serial.println("Enter Travel distance seperated by a comma: X,Z ");
Serial.print("Enter Move Values Now: ");
// Set Max Speed and Acceleration of each Steppers
stepperX.setMaxSpeed(500.0); // Set Max Speed of X axis
stepperX.setAcceleration(500.0); // Acceleration of X axis
stepperZ.setMaxSpeed(250.0); // Set Max Speed of Y axis slower for rotation
stepperZ.setAcceleration(250.0); // Acceleration of Y axis
}
void loop() {
while (Serial.available()>0) { // Check if values are available in the Serial Buffer
move_finished=0; // Set variable for checking move of the Steppers
TravelX= Serial.parseInt(); // Put First numeric value from buffer in TravelX variable
Serial.print(TravelX);
Serial.print(" X Travel , ");
TravelZ= Serial.parseInt(); // Put Second numeric value from buffer in TravelZ variable
Serial.print(TravelZ);
Serial.println(" Y Travel ");
stepperX.moveTo(TravelX); // Set new move position for X Stepper
stepperZ.moveTo(TravelZ); // Set new move position for Z Stepper
delay(1000); // Wait 1 seconds before moving the Steppers
Serial.print("Moving Steppers into position...");
}
// Check if the Steppers have reached desired position
if ((stepperX.distanceToGo() != 0) || (stepperZ.distanceToGo() !=0)) {
stepperX.run(); // Move Stepper X into position
stepperZ.run(); // Move Stepper Z into position
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0) && (stepperZ.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Enter Next Move Values (0,0 for reset): "); // Get ready for new Serial monitor values
move_finished=1; // Reset move variable
}
}
exactly,the codes work if i input only one value in the serial monitor and one motor moves,but if i enter 2 values both motors are not moving.
pls tell me how to do i clear up this variable problem?
this code i took from onnline,here is see that while i enter two values separated by commas the motors are stationary.
whereas if i enter one value then motor is moving and the value is displayed.
pls see the attachment where u can find the output of this code.