"if" statements using servo and sonar (HC-SR04)

We were recently given an assignment in class which we have no idea how to do. We are attempting to build a machine which sorts different types of marbles. Where we are struggling is finding a way to use a sonar to detect when all the marbles have been sorted. We will create a hopper which lines all the marbles along a track, waiting to be let into the sorter. A servo will control the flow of the marbles, moving a gate up and down to allow one marble through at a time. We must use a sensor for something and our only idea is to have a sonar detect when the line of marbles has been depleted. However, we have no idea how to use sonars as were never taught and I can't find good examples online. I want to create an if statement that basically says "if the sonar reads that there are no marbles, set the servo to the closed position." I have no idea what to put in the if statement, whether it be echoPin or duration or what.

PLEASE HELP

Thanks

mrjonessucks:
I can't find good examples online.

I find that incredibly hard to believe.

A quick google search for "Arduino sonar" turns up 561,000 hits. The whole first two pages look very informative. Probably not one that does your assignment for you, but definitely enough there to figure out how it works.

You are likely to find that the sonar is ineffective in detecting whether there are any marbles sitting on a track. I would suggest using a limit switch.