1-Wire, DS18B20... How do I search the bus?

I had the same looping problem with the search function. The looping seems to depend on the individual addresses for some reason, not on the number of sensors. Anyway, I rewrote the search algorithm.

I had my new version working successfully with up to 12 sensors, and I haven't seen it loop yet. This was with Arduino 11. For the record, I was using a 4K7 resistor.

Here is my code for the search routine:

//
// Perform a search. If this function returns a '1' then it has
// enumerated the next device and you may retrieve the ROM from the
// OneWire::address variable. If there are no devices, no further
// devices, or something horrible happens in the middle of the
// enumeration then a 0 is returned.  If a new device is found then
// its address is copied to newAddr.  Use OneWire::reset_search() to
// start over.
//
// searchJunction is the branch on the current address at which the
//    current search is to continue -
//    prior to this point the path follows the previous address
//    after this point, at each junction, 0 is selected
// lastJunction is the last unexplored junction on the present path.
//    the next search will continue from here
// 
uint8_t OneWire::search(uint8_t *newAddr)
{
  uint8_t i = 0;
  char lastJunction = -1;

  if ( searchExhausted) return 0;
  searchExhausted = 1; // the search is exhausted unless we find another junction

  if ( !reset()) return 0;
  write( 0xf0, 0);    // send search ROM

  for( i = 0; i < 64; i++) {
    uint8_t a = read_bit( );
    uint8_t nota = read_bit( );
    uint8_t ibyte = i/8;
    uint8_t ibit = 1<<(i&7);

    if ( a && nota) return 0;  // I don't think this should happen, this means nothing responded, but maybe if
    // something vanishes during the search it will come up.
    if ( !a && !nota) {                    // if at a junction, then
      if ( i < searchJunction) {           // if before search junction
        if ( address[ ibyte]&ibit)  a = 1; // follow previous address
        else a = 0;
      }
      else { 
        if ( i == searchJunction) a = 1;// at the search junction take the new branch.
        else a = 0;                     // past the search junction, on a new branch so select 0
      }
      if (!a) {             // if 0 is chosen
        lastJunction = i;   // set last junction
        searchExhausted = 0; // continue the search
      }
    }

    if (a) address[ibyte] |= ibit; // set address bit
    else address [ibyte] &= ~ibit;
    write_bit( a);
  } // end of address bit loop
  searchJunction = lastJunction;                    // set searchJunction for the next call to Search

  for ( i = 0; i < 8; i++) newAddr[i] = address[i]; // copy found address into output address
  return 1;  
}
#endif

And I put this in the header after Tom Pollard's note to clarify the version:

Modified to work with larger numbers of devices - avoids loop.
Tested in Arduino 11 alpha with 12 sensors.
26 Sept 2008 -- Robin James

If you are not sure how to update your library, do it this way:
first make a copy of your existing library, and put it in a safe place - i.e. not in "libraries". This is insurance!
Then in OneWire.cpp, delete the search routine and replace it with the one above. Also add the new header comment.
Filnally, delete the file OneWire.o. This will be regenerated when you restart Arduino.

I would have posted the full files here, but the size limitation does not permit this. A would have updated the library too, but I'm a newbie, and I'm clueless about how to go about it.

A couple of final comments:
As others have said, it is best to hard code the addresses instead of using the search routine every time. This is not as difficult as you may think.
Secondly, it is better and more robust to use more than one bus. This way, if something goes wrong you don't lose all of your sensors at once.
Would someone care to post some examples?