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);}
}
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.
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.
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.
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.
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 ?
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?
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.
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.