do..while

I have tried the following but it just sticks in an endless loop with the seconds advancing beyond 30

That's because your while clause is in the wrong place.

A do/while statement is fairly rare. A while statement is far more common. Unless you understand the difference, and have a good reason for using do/while, use while.