SFRRanger_reader.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
  2. // by Nicholas Zambetti <http://www.zambetti.com>
  3. // and James Tichenor <http://www.jamestichenor.net>
  4. // Demonstrates use of the Wire library reading data from the
  5. // Devantech Utrasonic Rangers SFR08 and SFR10
  6. // Created 29 April 2006
  7. // This example code is in the public domain.
  8. #include <Wire.h>
  9. void setup()
  10. {
  11. Wire.begin(); // join i2c bus (address optional for master)
  12. Serial.begin(9600); // start serial communication at 9600bps
  13. }
  14. int reading = 0;
  15. void loop()
  16. {
  17. // step 1: instruct sensor to read echoes
  18. Wire.beginTransmission(112); // transmit to device #112 (0x70)
  19. // the address specified in the datasheet is 224 (0xE0)
  20. // but i2c adressing uses the high 7 bits so it's 112
  21. Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
  22. Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50)
  23. // use 0x51 for centimeters
  24. // use 0x52 for ping microseconds
  25. Wire.endTransmission(); // stop transmitting
  26. // step 2: wait for readings to happen
  27. delay(70); // datasheet suggests at least 65 milliseconds
  28. // step 3: instruct sensor to return a particular echo reading
  29. Wire.beginTransmission(112); // transmit to device #112
  30. Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
  31. Wire.endTransmission(); // stop transmitting
  32. // step 4: request reading from sensor
  33. Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
  34. // step 5: receive reading from sensor
  35. if (2 <= Wire.available()) // if two bytes were received
  36. {
  37. reading = Wire.read(); // receive high byte (overwrites previous reading)
  38. reading = reading << 8; // shift high byte to be high 8 bits
  39. reading |= Wire.read(); // receive low byte as lower 8 bits
  40. Serial.println(reading); // print the reading
  41. }
  42. delay(250); // wait a bit since people have to read the output :)
  43. }
  44. /*
  45. // The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
  46. // usage: changeAddress(0x70, 0xE6);
  47. void changeAddress(byte oldAddress, byte newAddress)
  48. {
  49. Wire.beginTransmission(oldAddress);
  50. Wire.write(byte(0x00));
  51. Wire.write(byte(0xA0));
  52. Wire.endTransmission();
  53. Wire.beginTransmission(oldAddress);
  54. Wire.write(byte(0x00));
  55. Wire.write(byte(0xAA));
  56. Wire.endTransmission();
  57. Wire.beginTransmission(oldAddress);
  58. Wire.write(byte(0x00));
  59. Wire.write(byte(0xA5));
  60. Wire.endTransmission();
  61. Wire.beginTransmission(oldAddress);
  62. Wire.write(byte(0x00));
  63. Wire.write(newAddress);
  64. Wire.endTransmission();
  65. }
  66. */