Stepper motor rotation control

Dear Friends. I have the following code to control the rotation of a stepper motor. However I need help in the following: I need only turn the engine by pressing the switch 90 and back to 0 ° to remove the switch. I can not find the solution ... is to control a small door (just open and close) is that it is simple .... but not what else to do .... please help. Thank you very much in advance. A greeting.-

This code causes the motor to rotate in one direction and then in another depending on the state of the switch. But I do not get to stop himself ....

The code is:

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int buttonPin = 2;
int delayTime = 100; 
int pos = 0;

void setup() {
pinMode(motorPin1, OUTPUT); 
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}

void loop() {
int button_state = digitalRead(buttonPin);
if(button_state == HIGH){
digitalWrite(motorPin1, HIGH); 
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
}
else{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
}
}

diegoe:
Dear Friends. I have the following code to control the rotation of a stepper motor. However I need help in the following: I need only turn the engine by pressing the switch 90 and back to 0 ° to remove the switch. I can not find the solution ... is to control a small door (just open and close) is that it is simple .... but not what else to do .... please help. Thank you very much in advance. A greeting.-

This code causes the motor to rotate in one direction and then in another depending on the state of the switch. But I do not get to stop himself ....

Sorry, but I don't understand that.

What sort of switch do you have that you need to turn through 90 deg?

Post a link to the datasheet for the stepper motor you are using.

What stepper motor driver are you using?

...R
Stepper Motor Basics
Simple Stepper Code

Hi,
In your sketch you do not declare pin2 as an input.
Also do you have the gnd of the 2803 connected to the gnd of the UNO?
What is your 12V power suppy?

Tom... :slight_smile:

If you have a 48-pole stepper, then 90 degrees is 12 steps.

If the button is down, you want the stepper to move to position 12.
If the button is up, you want the stepper to move to position 0.

The stepper starts in position 0.

the loop makes the following decisions:

read the button, and decide where the stepper should be
if the stepper is already at that position, then do nothing and return.
look at what the time is since the last time the stepper was moved
if this is less then the delay time, do nothing and return
Otherwise,
increment or decrement the position by 1 to move it closer to where it should be .
change the outputs to match the current position (see below).
record the time at which this step was taken.

To change the outputs of the stepper to match the current position, do the following:

Extract bit 0 and bit 1 of the position:
boolean bit0 = (position & 0b01);
boolean bit1 = (position & 0b10);

if bit1 is set then reverse bit0
bit0 = (bit0 != bit1);

This gives you your pattern.
digitalWrite(motorPin1, bit0 ? HIGH : LOW);
digitalWrite(motorPin2, bit1 ? HIGH : LOW);
digitalWrite(motorPin3, bit0 ? LOW : HIGH);
digitalWrite(motorPin4, bit1 ? LOW : HIGH);

A difficulty is that position 0 of the stepper might not actually be the 'down' position exactly, depending on how the stepper is oriented. You may have to define the open and close positions as numbers other than 0 and 12.

Another difficulty is that this procedure never turns the stepper off. It may be that in the up position, the steeper shoul hold, but in the down position, it should be turned off. This can be added later, once you have your door opening and closing.

Hello again. I think I'm getting something but .... is not possible for me. I wrote the following code, the engine walk 1000 steps in one direction and 1000 steps back in the opposite direction:

#include <Stepper.h>

#define motorSteps 200
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define ledPin 13

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);

  // Initialize the Serial port:
  Serial.begin(9600);
}

void loop() {
  
  // Step forward 1000 steps:
  Serial.println("Forward");
  myStepper.step(1000);
  delay(1000);

  // Step backward 1000 steps: 
  Serial.println("Backward");
  myStepper.step(-1000);
  delay(1000);
}

However, I need you to press a switch, the motor moves 1000 steps and stops completely, then pressing again switch back to the original position (-1000 steps) ..... but I can not, I have this code in which the engine walk 1000 steps but again do the same:

#include <Stepper.h>

#define motorSteps 200;
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define buttonPin 2
#define ledPin 13

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);

  // Initialize the Serial port:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

 
void loop() {

int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
  
  // Step forward 1000 steps:
  Serial.println("Forward");
  myStepper.step(1000);
  delay(1000);
}
  else {
 
  // Step backward 1000 steps: 
  Serial.println("Backward");
  myStepper.step(-1000);
  delay(1000);
}
}

Please I need your help. I do not know how to make it work as I need. I need to open a door and then close it ........... please makes me add missing in the code. Thanks for your help.

Hello again. Maybe I explain evil. The engine should always have a "home" position. Whenever the switch is open (low) should be in that position or return to it if the engine has moved. Instead, pressing the switch (high) should always go to a certain position (1000 steps for example). Thanks again.

Sorry, no programming know, I'm not an engineer or student, just that I like electronics. I have no job and this is for a small job of a model they have asked me so I need your help. Thank you.

Think about your problem like this

Suppose you have a variable called stepperDestination. Normally it holds the value 0. While you hold the button it gets the value 1000. When you release the button it goes back to 0.

Now suppose you call a function such as

mystepperMoveTo(steppperDestination);

This will probably be much easier with the AccelStepper library because it keeps track of the actual position.

...R

How many motor steps does it take to move the door 90 degrees?

The engine should always have a "home" position.

Arduino has no way to tell where the motor is, you need a limit switch that switches when the door is closed.