hello
i have having trouble with a SRF 02 Ultra Sound sensor.
I had a sketch running controlling a servo just fine, until I accidentally ripped apart my sensor. and broke the metal thingy inside. I have a replacement, which I wired identically, but it wont give me any love.
When I power it up with the old sketch uploaded onto the Arduino the SRF gives one Long flash and One Short flash on the LED and does nothing. I dont get any selrial Print or anything.
I know the Long and Short Flashes are an address indicator, but i dont know how to address the thing and change it. No ammount of googleing seems to explain it to me in a way I can understand.
If anyone can paste me a script i can run to address it so it runs with my current program I will give them cookies! thanks
Vahakn
I paste my program below:
/*
SRF02 sensor reader
language:Wiring/Arduino
*/
#include <Wire.h>
#include <Servo.h>
Servo servo1;
#define sensorAddress 0x70
#define readInches 0x50
// use this for cm or microsecods
#define readCentimeters 0x51
#define readMicroseconds 0x52
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02
//LED INITIALISE
/*
int ledOne = 8; // LED connected to digital pin 8
int ledTwo = 9; // LED connected to digital pin 9
int ledThree = 10; // LED connected to digital pin 10
int ledFour = 11; // LED connected to digital pin 11
{
pinMode(ledOne, OUTPUT); // sets the digital pin as output
pinMode(ledTwo, OUTPUT); // sets the digital pin as output
pinMode(ledThree, OUTPUT); // sets the digital pin as output
pinMode(ledFour, OUTPUT); // sets the digital pin as output
*/
void setup()
{
servo1.attach(9);
//servo1.setMaximumPulse(2200);
//start the I2C bus
Wire.begin();
//open serial port:
Serial.begin(9600);
}
void sendCommand (int address, int command) {
//start I2C transmission:
Wire.beginTransmission(address);
//send command:
Wire.send(0x00);
Wire.send(command);
//end I2C transmission:
Wire.endTransmission();
}
void setRegister(int address, int thisRegister) {
//start IC2 transmission:
Wire.beginTransmission(address);
//send address to read from:
Wire.send(thisRegister);
//end I2C transmission();
Wire.endTransmission();
}
/*
readData() returns a result from the SRF sensor
*/
int readData(int address, int numBytes) {
int result = 0; // the result is two bytes long
//send i2c request for data:
Wire.requestFrom(address, numBytes);
//wait for two bytes to return:
while (Wire.available() < 2 );{
//wait for result
}
// read thew two bytes, and combine them into one int:
result = Wire.receive() * 256;
result = result + Wire.receive();
//return the result:
return result;
}
void loop()
{
//send the command to read the result in CM:
sendCommand(sensorAddress, readCentimeters);
//wait at least 70ms for a result:
delay(20);
delay(20);
delay(20);
//set the reigster that you want to read the result from:
setRegister(sensorAddress, resultRegister);
//read the result:
int sensorReading = readData(sensorAddress, 2);
//print it
Serial.print("distance: ");
Serial.print(sensorReading);
Serial.println(" cm");
// wait before next reading:
delay(20);
delay(20);
delay(20);
int servopos;
// int servosend; //trying to chillout the servo
if (sensorReading < 30) //because sensor is innefective below 20cm, closer than 30cm sends 0 to the servo
{
sensorReading = 0;
}
/*
if (sensorReading > 180)
{
sensorReading = sensorreading;
}
*/
sensorReading = sensorReading /2;
servopos = sensorReading;
Serial.println (servopos);
servo1.write(servopos);
//LED DISTANCE MEASURE
/* if (sensorReading < 30)
{
digitalWrite(ledOne, HIGH); // sets the LED on
digitalWrite(ledTwo, LOW); // sets the LED on
digitalWrite(ledThree, LOW); // sets the LED on
digitalWrite(ledFour, LOW); // sets the LED on
}
if (sensorReading > 70)
{
digitalWrite(ledOne, LOW); // sets the LED on
digitalWrite(ledTwo, HIGH); // sets the LED on
digitalWrite(ledThree, LOW); // sets the LED on
digitalWrite(ledFour, LOW); // sets the LED on
}
if (sensorReading > 110)
{
digitalWrite(ledOne, LOW); // sets the LED on
digitalWrite(ledTwo, LOW); // sets the LED on
digitalWrite(ledThree, HIGH); // sets the LED on
digitalWrite(ledFour, LOW); // sets the LED on
}
if (sensorReading > 170)
{
digitalWrite(ledOne, LOW); // sets the LED on
digitalWrite(ledTwo, LOW); // sets the LED on
digitalWrite(ledThree, LOW); // sets the LED on
digitalWrite(ledFour, HIGH); // sets the LED on
}
*/
static int v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 's':
servo1.write(v);
v = 0;
break;
}
}
}
/*
SendCommend() sends command in the format that the SRF sensors expect
*/
/*
setRegister() tells the SRF sensor to change the address pointer position
*/
// Sweep
// by BARRAGAN http://barraganstudio.com