Before you get into adding more functions, you might correct this code and test it.
Both setup() and loop() are functions. You want to add new function code to do something that is already being done in your code. Can you pick out what parts that might be?
Paul