Hi, I am a fairly new user to using the Arduino and have a small project, seeking some guidance on how best to implement it.
Project Materials
1 x Arduino Uno R3
1 x Arduino Motor Shield Rev3
1 x Haydon Kerk 15mm Stepper Motor (Bipolar, 5V)
1 x Push Button Switch
2 x Omron EE-SX972 (NPN) Photo Interrupter
Method (Idea how to implement)
Arduino drives the stepper motor (which moves a platform up and down) and receives position input of the stepper motor via the photo interrupters, basic diagram as below:

The idea is that as the platform moves up or down, one of the photo interrupters gets triggered and then this stops the motor. In the above diagram, the platform is in the “HIGH” position and so when the push button is activated, it will sense that photo 1 is activated and photo 2 in not, so the motor direction will be to drive the platform to “LOW”. Once it moves down low enough, photo 2 will be activated and photo 1 will not be activated, so it will stop driving the stepper motor. When the button is activated again, it will move to the “HIGH” position.
How would it be best to code this, I have tried to use an “if” statement to run the modules (but looks like I'll have "if" statements within other "if" statement, which I guess is not ideal.) but was wondering if it would be better to use a “case” statement or some better was to implement? I’m new to using the Arduino so would be grateful for some ideas!
Here is the start of my code:
int delayLength = 30;
int led = 3;
int buttonPin = 5;
int photo1 = 4;
int photo2 = 6;
int valButton;
int valPhoto1;
int valPhoto2;
void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
val = digitalRead(buttonPin);
if(valButton == HIGH){
digitalWrite(led, HIGH);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Direction CH A
analogWrite(3, 255); //Moves CH A
delay(delayLength);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Direction CH B
analogWrite(11, 255); //Moves CH B
delay(delayLength);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Direction CH A
analogWrite(3, 255); //Moves CH A
delay(delayLength);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Direction CH B
analogWrite(11, 255); //Moves CH B
delay(delayLength);
}
else{
digitalWrite(led, LOW);
}
}


