trial&error is the only way to go
No there is a better solution, thinking about what is going on.
Get you thoughts confirmed by using print statements or flapping pins / LEDs up and down.
Trial & error is the infinite number of monkeys approach to getting it right.