2015-06-18 83 views
-1

我在編寫線跟蹤機器人(使用電機和使用Arduino Uno)並使用開關語句聲明電機的不同運動時遇到了麻煩。
到目前爲止,我有:未聲明的範圍(在Arduino中的開關語句)

void loop() { 
int sensorValueright = analogRead(A0); 
int sensorValuecentre = analogRead(A1); 
int sensorValueleft = analogRead(A2); 

switch (direction1) { 
    case "right": 
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A 
    digitalWrite(9, LOW); //Disengage the Brake for Channel A 
    analogWrite(3, 60); //Motor A at quarter speed 

    digitalWrite(13, HIGH); //Establishes forward direction of Channel B 
    digitalWrite(8, LOW); //Disengage the Brake for Channel B 
    analogWrite(11, 125); //Motor B at half speed 
    delay(1000); 
    break; 

    case "centre": 
    digitalWrite(12, HIGH); //Forward A 
    digitalWrite(9, LOW); //Disengage the Brake for Channel A 
    analogWrite(3, 100); //Motor A = Motor B speed 

    digitalWrite(13, HIGH); //Forward B 
    digitalWrite(8, LOW); //Disengage the Brake for Channel B 
    analogWrite(11, 100); //Motor A = Motor B speed 
    delay(500); 
    break; 

    case "left": 
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A 
    digitalWrite(9, LOW); //Disengage the Brake for Channel A 
    analogWrite(3, 125); //Motor A at Half Speed 

    digitalWrite(13, HIGH); //Establishes forward direction of Channel B 
    digitalWrite(8, LOW); //Disengage the Brake for Channel B 
    analogWrite(11, 60); //Motor B at Quarter Speed 
    delay(1000); 
    break; 

    } 

    if (sensorValuecentre < 1){ 
    direction1 == "centre" 
    } 

    else if (sensorValueright < 1){ 
    direction1 == "right" 
    } 

    else if (sensorValueleft < 1){ 
    direction1 == "left" 
    } 

    else{ 
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A 
    digitalWrite(9, LOW); //Disengage the Brake for Channel A 
    analogWrite(3, 50); //Motor A at low speed 

    digitalWrite(13, HIGH); //Establishes forward direction of Channel B 
    digitalWrite(8, LOW); //Disengage the Brake for Channel B 
    analogWrite(11, 50); //Motor B at low speed 
    delay(500); 
    } 

    delay(1); 
} 

但我得到以下錯誤而編譯:

line_tracker_test_switch.ino: In function 'void loop()': 
line_tracker_test_switch.ino:20:9: error: 'direction1' was not declared in this scope 
line_tracker_test_switch.ino:60:3: error: expected ';' before '}' token 
line_tracker_test_switch.ino:64:3: error: expected ';' before '}' token 
line_tracker_test_switch.ino:68:3: error: expected ';' before '}' token 
Error compiling. 

任何幫助將不勝感激!

+1

我看不到direction1的聲明。你做? – bmargulies

+2

閱讀錯誤 - 'direction1'沒有聲明,也沒有顯示它是如何/在哪裏宣佈! – John3136

+3

另外,'direction1 =='右邊''不會做你認爲它做的事。 –

回答

2

「direction1」在您粘貼的代碼中沒有任何聲明。 某處需要有一行 type direction1;例如char *direction1;int direction1; 以告訴編譯器什麼是direction1。

其他3個錯誤表明那些行末尾缺少分號。 direction1 == "left";這也不太可能是你想要的。 ==是平等運算符。 =是賦值運算符。這也是使用這些操作符的一種奇怪的方式,因爲字符串與基本類型(例如intfloat)的工作方式不同。您無法直接比較或分配它們。您將改爲訪問指針值。

使用帶開關語句中的字符串標準C.

創建整型常量,而不是 enum {LEFT, RIGHT, CENTRE};和替換是不允許的「左」與LEFT等。