Sorry for sounding like a complete noob about this but I am. I am currently doing a project using IR sensors and sonar, I am having difficulty programming a robot to go from one set of code to the other once it has reached a certain condition (i.e. I do not know how to join code together). I would like the robot to do the line follow until it reaches the end of the line, once has done this do a 90 degree turn then use the sonar code (i know that the sonar code is basically a tester but i can easily adapt it to drive the motors)
Again, sorry for being a noob and having very little understanding of arduino, but how would I implement the line follow into a while loop until the IR sensor show a specific value?
No, don't trap code in while loops. That is a noobie mistake. Let the loop function do the looping. What you need is something called a state machine. Look that up.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
I formatted your line following code for you. See how much easier that is to follow along with? We don't just do that to be pretty. It really does help to see the logic. You can use Control-t in the IDE and it will do it for you. That's how I fixed this so easy.
The good thing for you is that while the sonar code does have a delay at the end to slow down the output, neither of these codes is really blocking or taking a lot of time It would be easy to run both at the same time.
You need to take the parts of the code that do the work and put them into functions. You will want a function with the line following code and a function with the sonar code. If you don't know what I'm talking about with the functions then you need to get on Google and find some C++ tutorials and look at the functions section and spend a few hours learning a little about that.
Once you have those two functions it is trivial to set up a couple of if statements with whatever conditions you want to turn on each behavior and have it run the appropriate function on each pass of the loop.