int E = 13;
int D = 12;
int C = 11;
int DP = 10;
int B = 9;
int A = 8;
int F = 7;
int G = 6;
int pushButton = 3;
int counter = 0;
int delaytime=2;
#include <Stepper.h>
int motorpin1 = 5;
int motorpin2 = 4;
int motorpin3 = 2;
int motorpin4 = 1;
int Distance = 0;
const int stepsPerRevolution = 2048;
Stepper myStepper(32, 5, 2, 4, 1);
void StepperFunction (){
myStepper.step(stepsPerRevolution);
Distance = Distance + 1;
if (Distance == 1){
while (true);
}
}
void one(){ //This code is to display the Number 1
digitalWrite(E,LOW);
digitalWrite(D,LOW);
digitalWrite(C,HIGH);
digitalWrite(DP,LOW);
digitalWrite(B,HIGH);
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
void Two(){ //This code is to display the Number 2
digitalWrite(E,HIGH);
digitalWrite(D,HIGH);
digitalWrite(C,LOW);
digitalWrite(DP,LOW);
digitalWrite(B,HIGH);
digitalWrite(A,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,HIGH);
}
void Three(){ //This code is to display the Number 3
digitalWrite(E,LOW);
digitalWrite(D,HIGH);
digitalWrite(C,HIGH);
digitalWrite(DP,LOW);
digitalWrite(B,HIGH);
digitalWrite(A,HIGH);
digitalWrite(F,LOW);
digitalWrite(G,HIGH);
}
void setup() {
pinMode(E,OUTPUT);
pinMode(D,OUTPUT);
pinMode(C,OUTPUT);
pinMode(DP,OUTPUT);
pinMode(B,OUTPUT);
pinMode(A,OUTPUT);
pinMode(F,OUTPUT);
pinMode(G,OUTPUT);
pinMode(pushButton,INPUT);
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
pinMode(motorpin3,OUTPUT);
pinMode(motorpin4,OUTPUT);
myStepper.setSpeed(300);
}
void loop() {
int buttonState=digitalRead(pushButton);
if (buttonState==HIGH){
if (counter > 3){ //Make sure the number will only be 1-3
counter = 0;
}
(counter++);
delay(500);
}
if (counter==1){ //Floor 1, display will show 1
one();
}
if (counter==2){ //Floor 2, display will show 2
Two();
StepperFunction();
}
if (counter==3){ //Floor 3
Three();
}
}
So the code above that I am doing is trying to make a basic way to have something similar like a elevator but not exactly. I'm trying to use 1 button to go to floor 1-3 by pressing the button twice it would go to floor 2 and pressing it 3 times would go to floor 3. I want to make it so that the stepper motor would only spin once so that it would go to floor 2 and then the motor would stop. I achieved that but while using the "While" function it makes my code just stop working so I want to know if their is another way to approach this problem. Also I attached a wiring diagram of how my wiring is set up. (Again, this is not suppose to be exactly like a elevator but similar, so please do not criticize me, I am trying to learn).