Debian Codegraph @

libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino

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