我有這個Arduino代碼我想運行,它偵聽通過從我寫的一些C++代碼串行輸入進來。這些信息正在寫入連接,但我在Arduino方面看到了一些非常奇怪的行爲。Arduino項目需要打印語句工作
如果我不把打印語句放在(特別是非printRGB()語句)中,我看不到任何輸出到串行控制檯。只要我加入他們,他們開始再次出現。這是怎麼回事?
#define LED_PIN 13
#define R_PIN 3
#define G_PIN 5
#define B_PIN 6
#define frameBegin ((char) 255)
char colors[] = {
0,0,0};
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(R_PIN, OUTPUT);
pinMode(G_PIN, OUTPUT);
pinMode(B_PIN, OUTPUT);
// establishContact();
Serial.println("blah");
}
void loop() {
if (Serial.available() > 0) {
char inByte = Serial.read();
toggleLED();
// Got a new frame
//Serial.println('S');
if (inByte == frameBegin) {
//Serial.println('B');
//write bass
boolean allThree = true;
for (int i=0; i < 3; i++) {
char rgb = Serial.read();
if (rgb == frameBegin) {
//Serial.println('F');
allThree = false;
break;
}
else {
colors[i] = rgb;
}
}
if (allThree) {
//printRGB();
analogWrite(R_PIN, colors[0]);
analogWrite(G_PIN, colors[1]);
analogWrite(B_PIN, colors[2]);
}
}
}
}
void printRGB() {
Serial.print("rgb: ");
Serial.print((uint8_t) colors[0]);
Serial.print(" ");
Serial.print((uint8_t) colors[1]);
Serial.print(" ");
Serial.println((uint8_t) colors[2]);
}
如果你不打印序列號,你期望讀什麼? 你自己的命令不會出現,因爲它們不是由arduino寫的,串口監視器不會給你一個你寫的東西的日誌。 – Lesto
請參閱printRGB()。在我得到一個特定的字節表示一個「幀」的開始並且我得到我期望的值的數量之後調用它。 – amruthv
如果你刪除它,你將看不到任何東西,因爲它永遠不會被調用。我不明白什麼是錯的。爲什麼您期望在SerialConsole上讀取一些內容,刪除在串行上寫入該值的代碼? – Lesto