2014-06-11 97 views
0

我試圖用Simple-H高壓電機罩和H電橋連接控制單臺直流電機。我需要使用串行監視器來控制速度和方向(例如「f 220」意味着以220的速度前進)。以下是我的代碼:使用Arduino Mega和Simple-H高壓電機罩控制直流電機

M1使電動機前進,M2使電動機後退。

以下是錯誤:沒有匹配函數調用「HardwareSerial ::打印()」

我不知道這是否是即使編碼本的正確途徑。我只是想幫助編程,因爲我在Arduino編碼方面很新穎。

const int PWM1 = 2; //PA - PWM A (M1) 
const int PWM2 = 3; //PB - PWM B (M2) 
const int MotorEnable = 4; //EA - Enable A 
const int TestLED = 13; //LED that tests whether going forward or back. LED lights up = forward, //non-lit = reverse 

void setup() { 
    pinMode(PWM1, OUTPUT); 
    pinMode(PWM2, OUTPUT); 

    Serial.begin(9600); 
} 
void loop() { 

    int VariableMove = Serial.print(); // *** ERROR is here! *** 
    int speed = Serial.parseInt(); 

    if (Serial.available() > 0) { 
     if (VariableMove = "f" && speed >= 0 && speed <= 255) { 
     digitalWrite(MotorEnable, HIGH); 
     digitalWrite(PWM1, HIGH); 
     digitalWrite(PWM2, LOW); 
     Serial.println("Going Forward"); 
     analogWrite(PWM1, speed); 
     } 
    if (VariableMove = "r" && speed >= 0 && speed <= 255) { 
     digitalWrite(MotorEnable, HIGH); 
     digitalWrite(PWM1, LOW); 
     digitalWrite(PWM2, HIGH); 
     Serial.println("Coming Back"); 
     analogWrite(PWM2, speed); 
     } 
     } 
    } 
+0

問題尋求幫助調試(「爲什麼不是這個代碼的工作?」)必須包括所期望的行爲,一個特定的問題或錯誤的和必要的重現最短代碼:

從串行LIB文件

它在問題本身。沒有明確問題陳述的問題對其他讀者無益。請參閱:[**如何創建一個最小,完整和可驗證的示例**](http://stackoverflow.com/help/mcve)。 –

+0

我試着爲串口監視器的輸入設置一個變量。我試圖存儲監視器中所說的內容,以便電機可以繼續運行,直到發出另一個命令。 – user2487812

+0

_'Here是錯誤:沒有匹配函數調用'HardwareSerial :: print()''_你顯示的代碼與這個錯誤消息沒有任何關係(除非你顯示什麼是'Serial',你實際上調用'HardwareSerial :: print()')! –

回答

-2

更新如下。

Serial.print()是一個「返回寫入的字節數」的函數,但是您沒有該函數的輸入,因此存在編譯時錯誤。

const int rightCW = 6, leftCW = 11, rightCCW = 5, leftCCW = 10; 

void setup() 
{ 
    Serial.begin(9600); 
    initMotors(); 

} 

void loop() 
{ 
    delay(10); 
    analogWrite(rightCW,255);  
} 

void initMotors() 
{ 
    //initialize motors 
    pinMode(rightCW, OUTPUT); 
    pinMode(leftCW, OUTPUT); 
    pinMode(rightCCW, OUTPUT); 
    pinMode(leftCCW, OUTPUT); 
} 

假設你已經正確連接電機,您可以編輯的權利,順時針和逆時針的(順時針和逆時針)引腳留給無論你控制引腳。該程序將簡單測試您是否可以使用一臺電機。

藉此爲H橋圖,在殼體:http://9m.no /쵉쀨 角去到電源軌,在每一側內部的兩個去理由,1A,2A進入控制上的Arduino,1Y,2Y銷去到電機的兩根電線。這同樣適用於您可能擁有的任何其他電機。

UPDATE(見下面的註釋):

你會想是

String input = ""; 
String result = ""; 
while (Serial.available() > 0) 
{ 
    char temp = Serial.read(); 
    if (temp == '\n') 
    { 
    result = input; 
    input = ""; 
    break; 
    } 
    else 
    { 
    input += temp; 
    } 
} 

Serial.read()是在同一時間在一個字節讀取功能。

+0

除了你的介紹性句子,所有這些東西與問題中提到的錯誤有什麼關係? –

+0

哦。我的印象是你想要電機的一般幫助。我指出了你所要求的錯誤,並建議了一個可以適應你的需求的替代程序,並接受諸如「f 220」之類的輸入。 – EchoAce

+0

不好意思,可能想把所有analogWrite的東西放在void loop()中。 – EchoAce

4

你說該生產線是引發錯誤:

int VariableMove = Serial.print(); // *** ERROR is here! *** 

目前還不清楚你期望什麼線做的,但有兩件事情不妥:

  1. Serial.print()需要參數:要打印的數據。從調用該方法,無需自參數,作爲錯誤信息的錯誤結果指出:no matching function for call to 'HardwareSerial::print()'

  2. 不到使用錯誤的編碼錯誤:Serial.print()在您的代碼返回表示寫入的字節的數目的long再稍後測試它是否爲字符串「f」等。因此,您可能需要Serial.read()或用於讀取傳入數據的其他方法之一。

最後這條線if (VariableMove = "f"

=分配爲了測試等價您使用兩個==爲:

if (VariableMove == "f"這是一個常見的錯誤作出。

Returns

size_t (long): print() returns the number of bytes written, though reading that number is optional

+0

'VariableMove ==「f」'這也是明顯的錯誤,應該儘可能'最可能'VariableMove =='f'' OP固定他的代碼從用戶輸入中讀取單個字符。 –

相關問題