Help in coding - to drive two stepper motors simultaneously

Hi,

I have written a code to drive one stepper motor in particular steps by the touch of a button.

HIGH for clockwise direction and LOW for anticlockwise direction.

However the code doesn't work for me for two stepper motors in sync when i tried to rewrite the code.

My stepper is 28BYJ-48 using ULN2003A driver.

#include <Stepper.h> //including stepper motor library
#include <EEPROM.h>


int stepIN1Pin = 2;         
int stepIN2Pin = 3;
int stepIN3Pin = 4;
int stepIN4Pin = 5;
int stepIN5Pin = 9;         
int stepIN6Pin = 10;
int stepIN7Pin =11;
int stepIN8Pin = 12;
int stepsPerRevolution = 4096; // amount of steps per revolution


const int button1Pin = 8; 


Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin,stepIN5Pin, stepIN7Pin, stepIN6Pin, stepIN8Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);

myStepper.setSpeed(5);
}



void loop() {
   int Dooropen = EEPROM.read(255);
   
 
 
  int button1State = digitalRead(button1Pin);

  if(button1State == LOW) 
  if (Dooropen == 1){
  myStepper.step(stepsPerRevolution/8);
  EEPROM.write(255,2);}
  if(button1State == HIGH) 
  if(Dooropen == 2){
  myStepper.step(-stepsPerRevolution/8);
  EEPROM.write(255,1);}
}

Please suggest a solution for me.

Thanks in advance.

Gokz

If you want 2 steppers to run at the same time you need code that makes each one move one step in turn rather than one of them do (say) 100 steps followed by the other doing 100 steps.

I think you will find it easier to achieve this with the AccelStepper library as it can run the motors without blocking until all the steps are complete.

...R

I think you will find it easier to achieve this with the AccelStepper library as it can run the motors without blocking until all the steps are complete.

Here is a link to a Wiki which contains a test sketch for running two stepper motors like yours in parallel:
https://arduino-info.wikispaces.com/SmallSteppers
You have to modify the sketch as it drives both steppers in opposite directions.

But this is pretty easy. By studying the AccelStepper library documentation You will find it out yourself.

Hi all,

I used the accelstepper to get the work done. I have used the code given below to obtain a clockwise and anticlockwise rotation with the control Pin. (Low and High)

However I'm facing a small problem. As soon as I power up the arduino, the motor makes a rotation without receiving any signal from the control pin.

Is there anyway to stop this movement in the start and just wait for the control signal.

#include <AccelStepper.h>
#include <EEPROM.h>

#define FULLSTEP 4
#define HALFSTEP 8

// motor pins
#define motorPin1  2     
#define motorPin2  3     
#define motorPin3  4     
#define motorPin4  5     
                     
                        
#define motorPin5  10     
#define motorPin6  11   
#define motorPin7  12    
#define motorPin8  13    


const int button1Pin = 8;                        

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);



void setup()   
{
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(400.0);
  stepper1.setSpeed(800);
  stepper1.moveTo(1024);  // 1 revolution 
  
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(400.0);
  stepper2.setSpeed(800);
  stepper2.moveTo(1024);  // 1 revolution 

pinMode(button1Pin, INPUT);
}


void loop()  
{
  int button1State = digitalRead(button1Pin);
  
  if(button1State == LOW) 
  {
  stepper1.moveTo(-1500);
  stepper2.moveTo(-1500);
  }
  
  if(button1State == HIGH) 

  {
  stepper1.moveTo(1500);
  stepper2.moveTo(1500);
  }
   stepper1.run();
   stepper2.run();
}

Thanks in advance

Gokz

Might this line in setup() be the cause?

stepper1.moveTo(1024);  // 1 revolution

...R

I tried removing the line, but still the same.

And another question I always ask when I see " I have a pushbutton (or switch) AND:

pinMode(Pin, INPUT);

Do you have a pullup / pulldown resistor from Pin to GND or VDD?

You have code in loop() that causes the stepper to move when button1Pin is LOW and when it is HIGH. It will always be one or the other so something will always happen.

If you use pinMode(button1Pin, INPUT_PULLUP); and wire your button so it pulls the pin LOW when pressed you can be sure at startup that the section if(button1State == HIGH) will operate. The Accelstepper library will assume the stepper was at 0 so it will move it to 1500.

If, in setup(), you "trick" the library to think the motor is already at 1500 then there will be no movement until you press the button. I believe the function setCurrentPosition () will allow you to do that.

...R

I haven't used a resistor yet. I use a web browser to sent LOW and HIGH signal to the PIN 8 with help of a WIFI wemos D1 board. How should the wiring be then ?

Thanks

gokzee:
I haven't used a resistor yet. I use a web browser to sent LOW and HIGH signal to the PIN 8 with help of a WIFI wemos D1 board. How should the wiring be then ?

How was I supposed to know you are doing that. I assumed it was a human pressing a button. Please give a full description of your project if you want sensible advice.

It may well be that no resistor and no INPUT_PULLUP is required. Do you know what is the default state of the output from your Wemos board?

...R

Sorry R. I thought i will make it simple to understand and basically not worry about the wifi part.

So the project is to control the door opening and closing using an android app. But for now I'm using a web address to sent the control signal.

http://192.168.100.1/door/0 for LOW signal
http://192.168.100.1/door/1 for HIGH signal

these addresses set the PIN 8 to LOW and HIGH accordingly which I use as control pin for two motors which is connected to my Arduino Uno. My mobile is connected to D1 Wemos and use mobile to sent LOW and HIGH signal.

Default state of Wemos OUTPUT is LOW.

I have noticed when i restart the UNO with a previous HIGH signal input, motor doesn't move. But when i restart the UNO with a previous LOW signal input, motor moves.

Not sure where I'm doing wrong.

Any Suggestions ?

Thanks

gokzee:
Any Suggestions ?

No. Because I have no idea what Wemos is or does or how it is working in your project.

When you say " have noticed when i restart the UNO with a previous HIGH signal input," do you mean that the Wemos thing is already working and producing a HIGH (or LOW) output.

If that is true isn't it reasonable for the Arduino to respond to that immediately it starts?

Of course you could easily program the Arduino to ignore inputs for a certain amount of time after it starts. Or you could program it to do nothing until the state of the input changes. There are many options.

I suspect you need to spend a little time thinking about the logic of your system and only examine the code when you fully understand the logic it is required to deal with.

...R