Put your code in a separate Function.
When you click a button, call the Function. When you want to run the same code without the user pressing the button, call the same Function.
When you click a button, call the Function. When you want to run the same code without the user pressing the button, call the same Function.