Pages: [1]   Go Down
Author Topic: SRF02 Library für Arduino 1.0  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 6
Posts: 705
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
ich verwende folgenden Ultraschallsensor und Library:
http://torfnase.lima-city.de/22500098ab109010a/05a8a19f5b0ab7f02/index.html

Leider kommt die Library nicht mit der neuen Arduino 1.0 Version klar, allerdings habe ich meinen Code jetzt komplett für 1.0 programmiert.
Was ich schon geändert habe:
Aus Wire.send wird Wire.write
und aus Wire.receive wird Wire.read

Hier der COde:
Code:
#include <Wire.h>
#define srfAddress 0x70                           // Address of the SRF08
#define cmdByte 0x00                              // Command byte
#define rangeByte 0x02                            // Byte for start of ranging data
byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging
int range = 0;
int rangeData;

void setup(){
    Serial.begin(9600);
    Wire.begin();                               
    }

void loop(){
 
  rangeData=getRange(); 
  Serial.print("Entfernung: ");
  Serial.println(rangeData, DEC);   
  delay(100);                                      // Wait before looping
}

int getRange(){                                   // This function gets a ranging from the SRF08
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.write(cmdByte);                             // Send Command Byte
  Wire.write(0x51);                                // Send 0x51 to start a ranging
  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
  highByte = Wire.read();                      // Get high byte
  lowByte = Wire.read();                       // Get low byte
  range = (highByte << 8) + lowByte;              // Put them together
  return(range);                                  // Returns Range
}


Mit folgender Meldung komme ich allerdings nicht klar:
"Call of overloaded write(int) is ambigious"

sketch_Ultraschallsensor.cpp: In function 'int getRange()':
sketch_Ultraschallsensor.pde:-1: error: call of overloaded 'write(int)' is ambiguous
D:\Aqua-Projekt\Arduino\Software\arduino-1.0\libraries\Wire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)
D:\Aqua-Projekt\Arduino\Software\arduino-1.0\hardware\arduino\cores\arduino/Print.h:49: note:                 size_t Print::write(const char*)

Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Füge mal folgenden cast ein:

Code:
Wire.write((byte)(_irgendwas_));

Die #defines werden standardmaessig als 'int' angesehen.
« Last Edit: January 09, 2012, 04:09:45 pm by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Offline Offline
Edison Member
*
Karma: 21
Posts: 1419
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Der Compiler findet ein Wire.write() mit einem Parameter, den er als "int" identifiziert. Ich würde spontan mal auf die per "#define" definierten Werte tippen, die sind nämlich zunächst erstmal typenlos. Da es keine Funktion für int als Parameter gibt, meckert er rum ,weil es zwei mögliche Lösungen gibt die Wire.write(uint8_t) und die Print::write(const char*). Versuch mal die Werte auf einen Typ zu casten.
Also
Code:
int getRange(){                                   // This function gets a ranging from the SRF08
  ...
  Wire.write((uint8_t)cmdByte);                             // Send Command Byte
  ...
  Wire.write((uint8_t)0x51);                                // Send 0x51 to start a ranging
  ...
  Wire.write((uint8_t)rangeByte);                           // Call the register for start of ranging data
  ...
}
Damit sagst Du dem Compiler welchen Typ der Wert hat und er weiss, welche Funktion er nehmen muss.
Alternativ könntest Du auch Variablen vom Typ "uint8_t cmdByte= 0x00;" definieren. Anhand des Typs der Variablen weiss der Compiler dann auch was er nehmen muss. Allerdings "verbrennt" das dann auch wieder 3 Byte :-)

Mario.
Logged

Offline Offline
God Member
*****
Karma: 6
Posts: 705
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi und danke fürs Feedback,
werde das morgen mal ausprobieren, für heute reichts, habe mich grad noch mit meinem Encoder rumgequält.
Gruß
Logged

Offline Offline
God Member
*****
Karma: 6
Posts: 705
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hallo,
vielen Dank, klappt wunderbar.
Hier nochmal der finale Code für die Arduino 1.0 Umgebung:
Code:
#include <Wire.h>
#define srfAddress 0x70                           // Address of the SRF08
#define cmdByte 0x00                              // Command byte
#define rangeByte 0x02                            // Byte for start of ranging data
byte highByte = 0x00;                             // Stores high byte from ranging
byte lowByte = 0x00;                              // Stored low byte from ranging
int range = 0;
int rangeData;

void setup(){
   Serial.begin(9600);
    Wire.begin();                               
    }

void loop(){
 
  rangeData=getRange(); 
  Serial.print("Entfernung: ");
  Serial.println(rangeData, DEC);   
  delay(5000);                                      // Wait before looping
}

int getRange(){                                   // This function gets a ranging from the SRF08
  Wire.beginTransmission(srfAddress);             // Start communticating with SRF08
  Wire.write((uint8_t)cmdByte);                             // Send Command Byte
  Wire.write((uint8_t)0x51);                                // Send 0x51 to start a ranging
  Wire.endTransmission(); 
  delay(100);                                     // Wait for ranging to be complete
  Wire.beginTransmission(srfAddress);             // start communicating with SRFmodule
  Wire.write((uint8_t)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
  highByte = Wire.read();                      // Get high byte
  lowByte = Wire.read();                       // Get low byte
  range = (highByte << 8) + lowByte;              // Put them together
  return(range);                                  // Returns Range
}

Gruß
Thorsten
Logged

Pages: [1]   Go Up
Jump to: