SWITCH DIRECTION STEP MOTOR ON RESTART

hi guys, its been a couple of weeks im trying to figure out how to control a step motor through a IR sensor. basically what im trying to do is open and close a cabinet door using a IR sensor controling a step motor. one touch to open, one touch to close, so the step motor needs to invert rotation at each restart. i have done all the system to fit the motor but i have no idea about the code. im using the controller L298N dual bridge, the step motor is the DF15RSMG and the HOBBY UNO, very similar to arduino uno board. if anyone can give me an idea about how to make this work would be much appreciate. thanks

The key to the solution is to detect if the door is open or closed. There are many solutions for this. One option is two use two contact switches. One switch will detect when the door is fully open, the other when it is fully closed. This will also allow you to detect when the motion is finished so you can stop your motor.

i thought to use two switches as well, but then how can i program the step motor to do, for example, X number of spins then switch off (from close to open door), then on the other switch the motor would again spin X times then switch off. would be ok to use two switches but i dont know how to program this... tks

Move the motor a single step at a time. After each step, check the limit switch you're moving towards to tell you whether you're there yet. No need to step the motor a particular number of times.