How do I make a DC motor rotate clockwise for a random value, then rotate counter-clockwise for the same amount autonomously?
Build an odometer.
You'll need the slotted disc and opto-sensor arrangement that's used by old-style computer mice with rotating ball-and-roller sensors. This produces pulses as the wheel rotates.
That pulse train can be sent to a counter. Have the counter run &quot;up&quot; when going forwards and &quot;down&quot; when going backwards. You will need to chain them to get enough counts to measure enough distance accurately.
When going backwards, the point at which the counter flips over from 0000 to 1111 is the point at which it's travelled the same distance as it did when going forwards. At this point, stop.