Adding two events from one output via Hall Effect sensor

Hi All
I'm looking for help on a little project I've been working on. Now I have to say that I'm whilst I've been trying to do this alone but now I cant see the wood for the trees. :rofl:

Ok I'll try and explain.
I have an Hall effect sensor and a Nano. The sensor is acting has a switch to control an electromagnet via an LN298N dual H bridge.Now I've cut and pasted my way through this section with various tutorials and I'm happy thus far.

My next stage is where I'm stuck!! Using the same Hall effect sensor switch and the H bridge's second input/output (Input 3 and 4) I want to control an alternating polarity motor and here's how foor example- When the magnet makes its first pass of the sensor (pin 3 goes high and pin 4 goes low), but on its next pass of the sensor it alternates so (pin 3 goes low and pin 4 goes high) thus driving the outputs to my motor.

Like I said I've looked at so many tutorials I've got brain freeze :rofl:

Any advice would be much appreciated :pray:

Thanks in advance.

Please see the code for the first set of my requirements, controlling an electromagnet via the LN298N and the Hall effect sensor. This part works fine although I'm no coder and more than likely it could be better.