Elevator project with stepper motor, opto interrupter and buttons

Hi all,
I prepare elevator stand with 2 opto interrupters one stepper motor and 3 buttons. The problem is where i push one button and it finished function other buttons is not available and i dont have a loop. Can you support me? The code is below:
/* Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino without a library: continuous rotation. More info: https://www.makerguides.com */
// Define stepper motor connections:
#define enablePin 8
#define dirPin 6
#define stepPin 7
#define switch0 9
#define switch1 3
#define switch2 4
#define switch3 5
#define opto1 A0
#define opto2 A1
#define opto3 A2
#define ledRed A4
#define ledGreen A5
void setup() {
Serial.begin(9600);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switch0,INPUT);
pinMode(switch1,INPUT);
pinMode(switch2,INPUT);
pinMode(switch3,INPUT);
pinMode(opto1,INPUT);
// Set the spinning direction CW/CCW:
//digitalWrite(dirPin, HIGH);
digitalWrite(enablePin, LOW);
digitalWrite(ledRed, LOW);
digitalWrite(ledGreen, LOW);
}
void function0()
{
if(digitalRead(switch0)==LOW && digitalRead(switch1)==HIGH && digitalRead(switch2)==HIGH && digitalRead(switch3)==HIGH)
{
for(int i=0;i<20000;i++)
{
digitalWrite(enablePin, LOW);
}
//digitalWrite(enablePin, LOW);
}
}
void function1()
{
if(digitalRead(switch1)==LOW && digitalRead(switch2)==HIGH && digitalRead(switch3)==HIGH)
{
for(int i=0;i<60000;i++)
{
digitalWrite(dirPin, HIGH);
digitalWrite(enablePin, HIGH);
digitalWrite(stepPin, HIGH);
// These four lines result in 1 step:
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
digitalWrite(enablePin, LOW);
if(analogRead(opto1)>900)
{
digitalWrite(enablePin, LOW);
delay(100);
for(int i=0;i<60000;i++)
{
digitalWrite(dirPin, LOW);
digitalWrite(enablePin, HIGH);
digitalWrite(stepPin, HIGH);
// These four lines result in 1 step:
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
digitalWrite(enablePin, LOW);
if(analogRead(opto2)>900)
{
digitalWrite(enablePin, LOW);
//delay(20);
digitalWrite(dirPin, LOW);
for(int i=0;i<60000;i++)
{
if(analogRead(opto2)<900)
{
digitalWrite(enablePin, LOW);
}
else
{
digitalWrite(dirPin, LOW);
digitalWrite(enablePin, HIGH);
digitalWrite(stepPin, HIGH);

                      // These four lines result in 1 step:
                    delayMicroseconds(200);
                    digitalWrite(stepPin, LOW);
                    delayMicroseconds(200);
                    digitalWrite(enablePin, LOW);
                    }
                  }
                 digitalWrite(enablePin, HIGH);
            }
           }
  }
}
//digitalWrite(enablePin, LOW);

}
}
void function2(){
if(digitalRead(switch1)==HIGH && digitalRead(switch2)==LOW && digitalRead(switch3)==HIGH)
{
for(int i=0;i<20000;i++)
{
digitalWrite(enablePin, HIGH);
digitalWrite(stepPin, HIGH);
digitalWrite(dirPin, HIGH);
// These four lines result in 1 step:
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
digitalWrite(enablePin, LOW);
if(analogRead(opto2)>900)
{
digitalWrite(enablePin, LOW);
delay(2000);
digitalWrite(dirPin, LOW);
}
//digitalWrite(enablePin, LOW);
}
}
}
void function3()
{
if(digitalRead(switch1)==HIGH && digitalRead(switch2)==HIGH && digitalRead(switch3)==LOW)
{
for(int i=0;i<20000;i++)
{
digitalWrite(enablePin, HIGH);
digitalWrite(stepPin, HIGH);
digitalWrite(dirPin, LOW);
// These four lines result in 1 step:
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
digitalWrite(enablePin, LOW);
}
//digitalWrite(enablePin, LOW);
}
}
void loop()
{
function0();
function1();
function2();
function3();
}

Is it wired correctly with the correct parts? If not post a schematic, not a frizzy drawing with all interconnections shown and links to each piece of hardware. In the code get rid of the delays, they cause all types of problems.

How are the switches wired? Do they have external pullup or pulldown resistors?

You may be better off writing the code with a state machine. Here is a tutorial on state machines that even includes an example with an elevator.

Thanks for support. When i keep success decision i will upload schematic and project here.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.