D:/DRISSI/arduino-0022/arduino-0022/libraries/uSonic/uSonic.cpp
Go to the documentation of this file.
00001 
00005 /* Get the value return by each ultrasonic sensor */
00006 
00007 #include "uSonic.h"
00008 
00009 
00010 byte HighByte = 0x00;                                                           // Stores high byte from ranging
00011 byte LowByte = 0x00;                                                            // Stored low byte from ranging
00012 
00013 
00014 // Get the value return by the first ultrasonic sensor
00015 int GetRangeUS1()                                                                               // This function gets a ranging from the SRF235
00016 { 
00017 #ifdef __DEBUG__US__
00018         Serial.println("Getting US sensor 1 value...");
00019 #endif
00020 
00021         int Range = 0; 
00022         Wire.beginTransmission(srfAddress);             // Start communticating with SRF235
00023         Wire.send(cmdByte);                             // Send Command Byte
00024         Wire.send(0x51);                                // Send 0x51 to start a ranging
00025         Wire.endTransmission();
00026   
00027         delay(100);                                     // Wait for ranging to be complete
00028   
00029         Wire.beginTransmission(srfAddress);             // Start communicating with SRFmodule
00030         Wire.send(rangeByte);                           // Call the register for start of ranging data
00031         Wire.endTransmission();
00032   
00033         Wire.requestFrom(srfAddress, 2);                // Request 2 bytes from SRF module
00034         if(Wire.available()== 2)                    // Wait for data to arrive
00035         {
00036                 HighByte = Wire.receive();                      // Get high byte
00037                 LowByte = Wire.receive();                       // Get low byte
00038                 Range = (HighByte << 8) + LowByte;              // Put them together
00039    
00040                 return(Range);                                                                  // Returns Range
00041         }
00042         return 0;
00043 }
00044 
00045 
00046 // Get the value return by the second ultrasonic sensor
00047 int GetRangeUS2()                                                                               // This function gets a ranging from the SRF235
00048 {  
00049 #ifdef __DEBUG__US__
00050         Serial.println("Getting US sensor 2 value...");
00051 #endif
00052         int Range = 0; 
00053         Wire.beginTransmission(srAddress);              // Start communticating with SRF235
00054         Wire.send(cmdByte);                             // Send Command Byte
00055         Wire.send(0x51);                                // Send 0x51 to start a ranging
00056         Wire.endTransmission();
00057   
00058         delay(100);                                     // Wait for ranging to be complete
00059   
00060         Wire.beginTransmission(srAddress);              // Start communicating with SRFmodule
00061         Wire.send(rangeByte);                           // Call the register for start of ranging data
00062         Wire.endTransmission();
00063   
00064         Wire.requestFrom(srAddress, 2);                                 // Request 2 bytes from SRF module
00065         while(Wire.available() < 2);                    // Wait for data to arrive
00066         HighByte = Wire.receive();                      // Get high byte
00067         LowByte = Wire.receive();                       // Get low byte
00068 
00069         Range = (HighByte << 8) + LowByte;              // Put them together
00070    
00071         return(Range);                                                                  // Returns Range
00072 }
00073 
00074 
00075 
00076 
00077