Hi I am using a Arduino Mega a small reduce speed stepper motor(very slow and limited speed of 15), and a stepper motor driver(picture shown below).I want to write code for 2 stepper motors moving at the same time. I've searched everywhere for it but cannot find it. The code shown below is a version that doesn't work. I was wondering if there is another way to do it.
#include <arduino.h>
#include <Stepper.h>
int stepIN1Pin = 46;
int stepIN2Pin = 48;
int stepIN3Pin = 50;
int stepIN4Pin = 52;
int step2in1Pin = 37;
int step2in2Pin = 35;
int step2in3Pin = 33;
int step2in4Pin = 31;
int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution,
stepIN1Pin, stepIN3Pin,
stepIN2Pin, stepIN4Pin);
Stepper myStepper2(stepsPerRevolution,
step2in1Pin, step2in3Pin,
step2in2Pin, step2in4Pin);
int echoPin = 12;
int trigPin = 11;
void setup()
{
// set the RPM
myStepper.setSpeed(16);
myStepper2.setSpeed(15);
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
float distanceCentimeters;
int pulseLenMicroseconds;
void forward()
{
myStepper.step(1);
myStepper2.step(1);
}
void turn(String Direction)
{
if (Direction == "left")
{
myStepper.step(150);
myStepper.step(5);
}
else if (Direction == "right")
{
myStepper.step(5);
myStepper.step(150);
}
}
void backward()
{
myStepper.step(-1);
myStepper2.step(-1);
}
void loop()
{
distanceCentimeters = pulseLenMicroseconds / 29.387 / 2;
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);
// bit-bang a small square wave
// on the trig pin to start the range
// finder
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
// measure the pulse length from the echo pin
pulseLenMicroseconds = pulseIn(echoPin, HIGH);
// calculate the distance using the speed of sound
}