Ping-Pong with a DC motor & a pot

but I'm having a hard time getting my head around the code.
Can anyone give me some suggestions?

The code does something. You want it to do something.

What you want it to do is clear. What it actually does is not.

We can't really help you until we know what the problem is.