如何更改此arduino代码以使其工作?

时间:2015-03-24 20:16:11

标签: arduino

我一直致力于使用arduino Duemilanove和HMC6352指南针模块作为指南针进行伺服工作。当我尝试编程时,adrduino程序员说Wire.send已经重命名为Wire.write而Wire.receive已经重命名为Wire.read所以我已经改变了它们,但它告诉我错误填写。我不确定还能做些什么。如果你有任何答案,请帮忙。

这是代码:

#include <Wire.h>
#include <Servo.h>

// Pins
#define SDA 4 // analog
#define SCL 5 // analog
#define servoPointerPin 10

// Controllers
Servo servoPointer;

void setup()
{
  Serial.begin( 4800 );
  delay( 500 );
  setupCompass();
  setupServo();
  Serial.println( "Setup Complete" );
}

void setupCompass()
{
  Wire.begin();
}

void setupServo()
{
  servoPointer.attach( servoPointerPin );
  positionServo( &servoPointer, 50 );
}

void positionServo( Servo* pServo, int position )
{
  int currentPosition = pServo->read();
  if( currentPosition != position )
    pServo->write( position );
}

int lastHeading = -1;

bool getCurrentHeading( int& reading )
{
  int compassAddress = 0x42 >> 1;
  Wire.beginTransmission( compassAddress );
  Wire.write( 'A' );
  Wire.endTransmission();

  delay(10);  
  Wire.requestFrom( compassAddress, 2 );
  while( Wire.available() < 2 )
    delay(10);

  int newReading = Wire.read();
  newReading = newReading << 8;
  newReading += Wire.read();
  newReading /= 10;
  bool result = ( lastHeading == -1 ) ? true : ( abs( newReading - lastHeading ) > 2 );
  reading = newReading;
  if( result )
    lastHeading = reading;  
  return result;
}

void loop()
{
  int heading = 0;
  if( getCurrentHeading(heading) )
    PositionNeedle( heading );
}

void PositionNeedle( const int currentHeading )
{
  int bearing = -currentHeading;
  if( bearing < 0 )
    bearing += 360;

  Serial.print( "Bearing=" );
  Serial.print( bearing, DEC );
  float minSweep = 80.0f;
  float sweepRange = 145.0f - minSweep;
  int servoPosition = (int)(sweepRange - (bearing / 360.0f * sweepRange) + minSweep);
  Serial.print( " Servo=" );
  Serial.println( servoPosition, DEC );
  positionServo( &servoPointer, servoPosition );
}

1 个答案:

答案 0 :(得分:0)

这看起来像是因为你正在重新定义SDA和SCL。因此pins_arduino.h的第45和46行遇到了麻烦。

来自pins_arduino.h:

45   static const uint8_t SDA = 18;
46   static const uint8_t SCL = 19;

您正在使用#define语句更改这些行。 #define不像变量声明那样工作,它实际上用第二个参数预编译替换第一个参数的任何实例。所以编译器会在pins_arduino.h文件中找到这些行,现在看到:

45   static const uint8_t 4 = 18;
46   static const uint8_t 5 = 19;

由于您无法将'4'或'5'定义为变量,因此抛出错误的原因是什么。