
APPENDIX C 403
Chapter 8
Sharp GP2D12 IR ranger reader
Language: Wiring/Arduino
Reads the value from a Sharp GP2D12 IR ranger and sends
it out serially.
int sensorPin = 0; // Analog input pin
int sensorValue = 0; // value read from the pot
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin); // read the pot value
// the sensor actually gives results that aren't linear.
// this formula converts the results to a linear range.
if (sensorValue> 3) {
int range = (6787 / (sensorValue - 3)) - 4;
Serial.println(range, DEC); // print the sensor value
delay(10); // wait 10 milliseconds before the next loop
}
}
SRF02 sensor reader
Language: Wiring/Arduino
Reads data from a Devantech SRF02 ultrasonic sensor.
Should also work for the SRF08 and SRF10 sensors as well.
Sensor connections:
• SDA - Analog pin 4
• SCL - Analog pin 5
// include Wire library to read and write I2C commands:
#include <Wire.h>
// the commands needed for the SRF sensors:
#define sensorAddress 0x70
#define readInches 0x50
// use these as alternatives if you want centimeters or microseconds:
#define readCentimeters 0x51
#define readMicroseconds 0x52
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02
void setup()
{
Wire.begin(); // start the I2C bus.
// open the serial port:
Serial.beg