Code for autonomous line follower and sonar (wall sensor)

Hey everybody,

My lab group and I are building an Arduino line follower with a sonar sensor for a school project and we are having trouble optimizing our code.

Our codes for line following (Source: http://www.parallax.com/product/28108) and sonar (Source: http://learn.parallax.com/BoeShield/RoamingPingShieldBot) worked well separately, however upon combining them our robot’s movements became shaky and it sometimes runs into walls instead of avoiding them.

Any comments will be greatly appreciated.

Below I have attached our combined code as well as our individual sonar code (which works well).

Combined Code.pdf (272 KB)

Why is it so very,very hard to SIMPLY POST CODE?