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