Guest User

Untitled

a guest
Feb 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. // Licensed under the GNU General Public License v3.0
  2.  
  3. #include <Wire.h>
  4.  
  5. // Address of SRF02 range finder on I2C
  6. #define i2cAddress 0x70
  7.  
  8. void setup()
  9. {
  10. // Connection to the connected Laptop
  11. Serial.begin(9600);
  12.  
  13. Wire.begin();
  14. }
  15.  
  16. void loop()
  17. {
  18. //Allocate Memory
  19. byte byteHigh, byteLow;
  20. int distance;
  21.  
  22. Wire.beginTransmission(i2cAddress); // begin communication with SFR02
  23. Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
  24. Wire.write(byte(0x51)); // command sensor to measure in centimeters
  25. Wire.endTransmission(); // end transmission
  26.  
  27. // Wait for 70 milliseconds
  28. delay(70);
  29.  
  30. Wire.beginTransmission(i2cAddress); // begin communication with SFR02
  31. Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
  32. Wire.endTransmission(); // end transmission
  33.  
  34. Wire.requestFrom(i2cAddress, 2); // Request 2 bytes from SFR02
  35.  
  36. while(Wire.available() < 2); // Wait for the bytes to arrive
  37.  
  38. // Read the values
  39. byteHigh = Wire.read();
  40. byteLow = Wire.read();
  41.  
  42. // Calculate full bearing
  43. distance = ((byteHigh<<8) + byteLow);
  44.  
  45. // Print data to Serial Monitor window
  46. Serial.print("$Distance,");
  47. Serial.println(distance, DEC);
  48.  
  49. // In the demo wait half a second
  50. delay(500);
  51. }
Add Comment
Please, Sign In to add comment