You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.8KB

  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 (Wire.available() >= 2) { // if two bytes were received
  36. reading = Wire.read(); // receive high byte (overwrites previous reading)
  37. reading = reading << 8; // shift high byte to be high 8 bits
  38. reading |= Wire.read(); // receive low byte as lower 8 bits
  39. Serial.println(reading); // print the reading
  40. }
  41. delay(250); // wait a bit since people have to read the output :)
  42. }
  43. /*
  44. // The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
  45. // usage: changeAddress(0x70, 0xE6);
  46. void changeAddress(byte oldAddress, byte newAddress)
  47. {
  48. Wire.beginTransmission(oldAddress);
  49. Wire.write(byte(0x00));
  50. Wire.write(byte(0xA0));
  51. Wire.endTransmission();
  52. Wire.beginTransmission(oldAddress);
  53. Wire.write(byte(0x00));
  54. Wire.write(byte(0xAA));
  55. Wire.endTransmission();
  56. Wire.beginTransmission(oldAddress);
  57. Wire.write(byte(0x00));
  58. Wire.write(byte(0xA5));
  59. Wire.endTransmission();
  60. Wire.beginTransmission(oldAddress);
  61. Wire.write(byte(0x00));
  62. Wire.write(newAddress);
  63. Wire.endTransmission();
  64. }
  65. */