hc-sr04

It is an unreal expectation that you will find code that exactly matches what you want to do. You can find information on how to connect the HCSR04 and code to get ranges from it. I linked to a tutorial on how to read a potentiometer. Once you can read the rangefinder and pot separately read up on the if statement and digitalWrite() in the language reference. Start with the basics and build a part at a time. When you understand each part then you can more easily add them together.