Need code help - buttons & logic states, control

Hi everyone. I have what may be a very simple C code problem to solve but my code experience is limited compared to my hardware experience. Let me describe my hardware -

I have a L298N - based H-bridge module driving a motor back and forth. I have two limit switches on a mechanism that the motor can reach- it starts at one limit and stops at the other. I also have a momentary button that is the activation switch.

What I need is a ‘smart’ bit of code that takes the input from the activation momentary button state and runs the motor forward until it gets to the end limit switch, and then stops. Then it needs to do drive the H bridge/motor the other way back to the start limit switch when the activation button is pressed again. It has to remember which position it last went to so it knows to go the opposite way when the button is pressed again. The motor speed can vary, so I cannot use time to determine where the motor is, just the limit switches.

So I have 3 input pins and 3 states - action1sw, limit1sw, limit2sw - and two outputs - motorforward and motorbackward.

I do not know how to write a set of statements (if/then?) that does what is described here. Does this require interrupts?

Thanks to anyone who can help.

Any quick points in the right direction of how to start

There is a very similar thread going on right now. See https://forum.sparkfun.com/viewtopic.ph … ne#p149558 for hints.