oneWireSearch.ino 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // FILE: oneWireSearch.ino
  3. // AUTHOR: Rob Tillaart
  4. // VERSION: 0.1.02
  5. // PURPOSE: scan for 1-Wire devices + code snippet generator
  6. // DATE: 2015-june-30
  7. // URL: http://forum.arduino.cc/index.php?topic=333923
  8. //
  9. // inspired by http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
  10. //
  11. // Released to the public domain
  12. //
  13. // 0.1.00 initial version
  14. // 0.1.01 first published version
  15. // 0.1.02 small output changes
  16. #include <OneWire.h>
  17. void setup()
  18. {
  19. Serial.begin(115200);
  20. Serial.println("//\n// Start oneWireSearch.ino \n//");
  21. for (uint8_t pin = 2; pin < 13; pin++)
  22. {
  23. findDevices(pin);
  24. }
  25. Serial.println("\n//\n// End oneWireSearch.ino \n//");
  26. }
  27. void loop()
  28. {
  29. }
  30. uint8_t findDevices(int pin)
  31. {
  32. OneWire ow(pin);
  33. uint8_t address[8];
  34. uint8_t count = 0;
  35. if (ow.search(address))
  36. {
  37. Serial.print("\nuint8_t pin");
  38. Serial.print(pin, DEC);
  39. Serial.println("[][8] = {");
  40. do {
  41. count++;
  42. Serial.println(" {");
  43. for (uint8_t i = 0; i < 8; i++)
  44. {
  45. Serial.print("0x");
  46. if (address[i] < 0x10) Serial.print("0");
  47. Serial.print(address[i], HEX);
  48. if (i < 7) Serial.print(", ");
  49. }
  50. Serial.println(" },");
  51. } while (ow.search(address));
  52. Serial.println("};");
  53. Serial.print("// nr devices found: ");
  54. Serial.println(count);
  55. }
  56. return count;
  57. }