You need to look into the concept of a "state machine". When the Arduino switches on, it initially doesn't know where the piston is. But it has limit switches to know if it is at one end or the other. If one switch is active then the position is obvious. If neither then it could drive one way or the other until a switch is touched.