下面的代码工作正常,没有函数spinLeft()(我从goForward复制并修改)。当我尝试添加该功能(稍后使用)时,我得到了上面的错误。但我无法看到我做了什么没有做过的事情。如何添加另一个函数会产生此错误?
错误消息的其余部分显示:
Roomba4operants.ino: In function 'void spinLeft()':
Roomba4operants:57: error: call of overloaded 'write(int)' is ambiguous
/Users/royclymer/Desktop/Arduino.app/Contents/Resources/Java/libraries/SoftwareSerial/SoftwareSerial.h:92: note: candidates are: virtual size_t SoftwareSerial::write(uint8_t)
/Users/royclymer/Desktop/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:49: note: size_t Print::write(const char*)
代码:
#include <SoftwareSerial.h>
int rxPin = 3;
int txPin = 6;
int ledPin = 13;
SoftwareSerial Roomba(rxPin,txPin);
void setup() {
pinMode(ledPin, OUTPUT); // sets the pins as output
Serial.begin(115200);
Roomba.begin(115200);
digitalWrite(ledPin, HIGH); // say we're alive
Serial.println ("Sending start command...");
delay (1000);
// set up ROI to receive commands
Roomba.write(128); // START
delay(150);
Serial.println ("Sending Safe Mode command...");
delay (1000);
Roomba.write(131); // SAFE
delay(150);
digitalWrite(ledPin, LOW); // say we've finished setup
Serial.println ("Ready to go!");
delay (5000);
}
void loop() {
digitalWrite(ledPin, HIGH); // say we're starting loop
Serial.println ("Go Forward");
goForward();
delay (1000);
Serial.println ("Halt!");
halt();
Serial.println ("Go Backwards");
delay (200);
goBackward();
delay (1000);
Serial.println ("Halt!");
halt();
while(1) { } // Stop program
}
void goForward() {
Roomba.write(137); // DRIVE
Roomba.write((byte)0x00); // 0x00c8 == 200
Roomba.write(0xc8);
Roomba.write(0x80);
Roomba.write((byte)0x00);
}
void spinLeft() {
Roomba.write(137); // DRIVE
Roomba.write((byte)0x00); // 0x00c8 == 200
Roomba.write(0xc8);
Roomba.write(0x00); //0x0001==spin left
Roomba.write((byte)0x01);
}
void goBackward() {
Roomba.write(137); // DRIVE
Roomba.write(0xff); // 0xff38 == -200
Roomba.write(0x38);
Roomba.write(0x80);
Roomba.write((byte)0x00);
}
void halt(){
byte j = 0x00;
Roomba.write(137);
Roomba.write(j);
Roomba.write(j);
Roomba.write(j);
Roomba.write(j);
}
答案 0 :(得分:8)
0x00
可以解释为整数文字或指针文字。编译器不知道你的意思,因为你有两个重载。
所以将该行更改为:
Roomba.write((byte)0x00);
或稍微冗长的C ++风格的演员:
Roomba.write(static_cast<byte>(0x00));