Pages: [1]   Go Down
Author Topic: SRF08 sensor not working  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So yeah, I can't figure out why, but i hooked it up according to this:
http://arduino.cc/playground/Main/SonarSrf08
and this is the code :
Code:
   

    #include <Wire.h>

     

    #define srfAddress 0x70                           // Address of the SRF08

    #define cmdByte 0x00                              // Command byte

    #define lightByte 0x01                            // Byte to read light sensor

    #define rangeByte 0x02                            // Byte for start of ranging data

     

    void setup(){

      Wire.begin();                                   // Initialize the I2C bus

      Serial.begin(115200);                           // Initialize the serial connection

      delay(100);                                     // Waits to make sure everything is powered up before sending or receiving data

    }

     

    void loop(){

      //int rangeData = getSoft();                     // Calls a function to get software version

      int rangeData = getRange();                      // Calls a function to get the range data

      int lightData = getLight();                      // Calls a function to get the light data

     

      Serial.print("Range: ");

      Serial.print(rangeData);

      Serial.println("cm");

     

      Serial.print("Light: ");

      Serial.print(lightData);

      Serial.println("");

     

      delay(100);                                      // Wait before looping

    }

     

    int getRange(){                                   // This function gets a ranging from the SRF08

      int range = 0;

     

      Wire.beginTransmission(srfAddress);             // Start communicating with SRF08

      Wire.write((byte)cmdByte);                             // Send Command Byte

      Wire.write(0x51);                                // Send 0x51 to start a ranging in cm

      Wire.endTransmission();

     

      delay(100);                                     // Wait for ranging to be complete

     

      Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule

      Wire.write(rangeByte);                           // Call the register for start of ranging data

      Wire.endTransmission();

     

      Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module

      while(Wire.available() < 2);                    // Wait for data to arrive

      byte highByte = Wire.read();                 // Get high byte

      byte lowByte = Wire.read();                  // Get low byte

     

      range = (highByte << 8) + lowByte;              // Put them together

     

      return(range);                                  // Returns Range

    }

     

    int getLight() {                                  // Function to get light reading

      Wire.beginTransmission(srfAddress);

      Wire.write(lightByte);                           // Call register to get light reading

      Wire.endTransmission();

     

      Wire.requestFrom(srfAddress, 1);                // Request 1 byte

      while(Wire.available() < 0);                    // While byte available

      int lightRead = Wire.read();                 // Get light reading

     

      return(lightRead);                              // Returns lightRead

     

    }

     

    int getSoft(){                                    // Function to get software revision

     

      Wire.beginTransmission(srfAddress);             // Begin communication with the SRF module

      Wire.write((byte)cmdByte);                             // Sends the command bit, when this bit is read it returns the software revision

      Wire.endTransmission();

     

      Wire.requestFrom(srfAddress, 1);                // Request 1 byte

      while(Wire.available() < 0);                    // While byte available

      int software = Wire.read();                  // Get byte

     

      return(software);                               

     

    }

It blinks on start up then nothing if I enter the Serial monitor.
Any ideas?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while(Wire.available() < 0);                    // While byte available

I think you mean '< 1'  or possibly '== 0'.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote

I think you mean '< 1'  or possibly '== 0'.

OK I tired both but I'm not even using that function, all I want is the distance not the light value.
What else should I try?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you should use the library or at least look at how the example uses the library to see if you missed any steps.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: