Help Needed - To open and close door Using Arduino and 28BYJ-48 step motor

HI,

I’m trying to build a project that uses android to control the opening and closing of a door

I’m using arduino uno, 28BYJ-48 step motor and ULN2003 driver for the same.

Currently I’m using two buttons on a breadboard to control the movement of the motor - forward and reverse. Mobile app will be in next step.

I’m currently using this code

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

//defining pins section

int stepIN1Pin = 2;         
int stepIN2Pin = 3;
int stepIN3Pin = 4;
int stepIN4Pin = 5;
int stepsPerRevolution = 4096; // amount of steps per revolution

const int button1Pin = 8;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 9;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
myStepper.setSpeed(5);
}

void loop() {
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).
  
  //This section layouts the button scheme one button is clockwise rotation, the other counter clockwise rotation  
int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
 if (((button1State == LOW) && !(button2State == LOW)))  // if we're pushing button 1 OR button 2
  myStepper.step(stepsPerRevolution/2);
  if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
  myStepper.step(-stepsPerRevolution/2);
  }

My problem is, if i press button 1 twice - motor moves forward twice , the motor shouldn’t move forward after the assigned limit. Only reverse button should work once motor has moved forward.

Similarly for reverse, the motor should not go reverse beyond the set limit even if the reverse button is pressed twice. only forward button should work once motor has turned reverse.

I read that, we have to assign some limits to the motor movement.

Any ideas, suggestions…please help.

Thanks in advance.

Gokz

Add a variable such as DoorOpen

Set it to TRUE when the Motor moves the door open and set it to false when the Motor Closes the door.

When testing to see if the “open” button was pressed, also test that the door is not already open.

Similarly, when testing to see if the “Close” button was pressed, also test that the door is open.

Hi,

I did something like below but dint work.

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

//defining pins section

int stepIN1Pin = 2;         
int stepIN2Pin = 3;
int stepIN3Pin = 4;
int stepIN4Pin = 5;
int stepsPerRevolution = 4096; // amount of steps per revolution

int Dooropen ;



const int button1Pin = 8;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 9;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

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

}



void loop() {
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).
  
  //This section layouts the button scheme one button is clockwise rotation, the other counter clockwise rotation  
int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
 if (((button1State == LOW) && !(button2State == LOW)))// if we're pushing button 1 OR button 2
  if (Dooropen = false)
  {myStepper.step(stepsPerRevolution/2);}
  Dooropen = true;
  if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
  if (Dooropen = true){
  myStepper.step(-stepsPerRevolution/2);
  }
Dooropen = false;
}

Will i be able to store the status of door (open or close) in EEPROM of arduino so in that during power failure..arduino knows the status of the door.

Any suggestions??

Thanks

Gokz

Will i be able to store the status of door (open or close) in EEPROM of arduino so in that during power failure..arduino knows the status of the door.

Yes, but you shouldn't need to. What should tell the Arduino that the door is open is the fact that the door hit a limit switch. What should tell the Arduino that the door is closed is the fact that it hit a different limit switch. The current state of the limit switches should tell you whether the door is open or closed. If it is somewhere in between, the system operator will need to decide whether it should be opened all the way or closed all the way.

I did something like below but dint work.

It did exactly what it was supposed to. That that was not what you expected simply says that your expectations are wrong. So, what DID the code do? What did you expect it to do?

The convention is camelCase for variable names, so Doopopen should be doorOpen. Since it is supposed to contain true or false, bool is the proper type.

int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

There is no good reason to separate the declaration of the variables from the initialization of them, and plenty of good reasons not to.

  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
  if (Dooropen = false)

= is the assignment operator. Assigning false to doorOpen in an if statement is probably not what you want to do. == is the equality operator.

Hi,

Thanks for the reply.

I was trying with the EEPROM of arduino…but I’m missing something. Only one button works now. I have to restart the arduino to make the other button effective all the time.

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

int stepIN1Pin = 2;         
int stepIN2Pin = 3;
int stepIN3Pin = 4;
int stepIN4Pin = 5;
int stepsPerRevolution = 4096; // amount of steps per revolution

int Dooropen = EEPROM.read(255);


const int button1Pin = 8;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 9;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
myStepper.setSpeed(5);
}



void loop() {
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).
  
  //This section layouts the button scheme one button is clockwise rotation, the other counter clockwise rotation  
  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
  if(((button1State == LOW) && !(button2State == LOW))) // if we're pushing button 1 OR button 2
  if (Dooropen == 1){
  myStepper.step(stepsPerRevolution/2);
  EEPROM.write(255,2);}
  if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
  if(Dooropen == 2){
  myStepper.step(-stepsPerRevolution/2);
  EEPROM.write(255,1);}
}

Any Suggestions!!

Thanks,

Gokz

Found out the mistake. this is the correct code. however this is not a complete fail-proof solution in case of a power failure.

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

int stepIN1Pin = 2;         
int stepIN2Pin = 3;
int stepIN3Pin = 4;
int stepIN4Pin = 5;
int stepsPerRevolution = 4096; // amount of steps per revolution




const int button1Pin = 8;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 9;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
myStepper.setSpeed(5);
}



void loop() {
   int Dooropen = EEPROM.read(255);
   
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).
 
  //This section layouts the button scheme one button is clockwise rotation, the other counter clockwise rotation  
  int button1State = digitalRead(button1Pin);
  int button2State = digitalRead(button2Pin);
  if(((button1State == LOW) && !(button2State == LOW))) // if we're pushing button 1 OR button 2
  if (Dooropen == 1){
  myStepper.step(stepsPerRevolution/4);
  EEPROM.write(255,2);}
  if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
  if(Dooropen == 2){
  myStepper.step(-stepsPerRevolution/4);
  EEPROM.write(255,1);}
}