2013-06-01 75 views
0

我被困在我的項目中,最後一部分是通過讀取xml feed來控制伺服器,並且取決於它應該轉動幾度的值。我正在使用yahoo的天氣xml提要,它顯示不同的風速屬性作爲速度,方向等。我正在做的是僅使用風向和速度以便用LED和箭頭指示方向。一切工作正常與LED的,但是當談到伺服,我不知道該怎麼做。它在Arduino掃描示例中工作正常,但我使用Firmata,我猜它的代碼有點不同。首先,我使用標準Firmata來控制LED,但對於伺服器來說它並不好,所以現在我使用2個arduino的標準Firmata作爲LED,另一個使用Servo Firmata作爲伺服器,noth分配在不同的COM端口上。不幸的是,當我嘗試對伺服進行編程時,它只會變得更快或者更慢,而不能控制它的角度以及何時停止。在Arduino中,它可以和他們的例子一起工作,但是在Firmata中,似乎我無法適應它,所以它在處理Firmata中的作用類似。我一直停留在這2天已經請任何幫助,將不勝感激,因爲我有在週一Firmata伺服控制

這裏完成它是代碼:

import processing.serial.*; 
import cc.arduino.*; 
Arduino arduino, arduino2; 



final String URL = "http://weather.yahooapis.com/forecastrss?w=27764362"; 
final String WORD = "yweather:wind"; 
final String TOKEN = "\\W+"; 

int ledPin = 13; 
int ledPin1 = 12; 
int ledPin2 = 8; 
int pos=0; 
void setup() { 

    arduino = new Arduino(this, Arduino.list()[0], 57600); 
    arduino2 = new Arduino(this, Arduino.list()[1], 57600); 
    arduino.pinMode(ledPin, Arduino.OUTPUT); 
    arduino.pinMode(ledPin1, Arduino.OUTPUT); 
    arduino.pinMode(ledPin2, Arduino.OUTPUT); 
    arduino2.pinMode (9, Arduino.OUTPUT); 

    final String[] xml = loadStrings(URL); 

    int idx = 0; 

    for (; idx != xml.length; ++idx) 
    if (xml[idx].contains(WORD)) break; 

    if (idx == xml.length) { 
    println("Not found"); 
    exit(); 
    } 

    println("Index: " + idx); 
    println(xml[idx]); 

    final int[] info = int(xml[idx].split(TOKEN)); 
    final int dir = info[6]; 
    final int spd = info[8]; 
    println("Speed: " + spd); 
    println("Direction: " + dir); 

    if (spd < 5) { 
    arduino.digitalWrite(ledPin, Arduino.LOW); 
    arduino.digitalWrite(ledPin1, Arduino.LOW); 
    arduino.digitalWrite(ledPin2, Arduino.LOW); 
    } 
    if (spd >= 5 && spd <10) { 
    arduino.digitalWrite(ledPin, Arduino.HIGH); 
    arduino.digitalWrite(ledPin1, Arduino.LOW); 
    arduino.digitalWrite(ledPin2, Arduino.LOW); 
    } 
    if (spd >= 10 && spd <= 15) { 
    arduino.digitalWrite(ledPin, Arduino.HIGH); 
    arduino.digitalWrite(ledPin1, Arduino.HIGH); 
    arduino.digitalWrite(ledPin2, Arduino.LOW); 
    } 
if (spd > 16) { 
    arduino.digitalWrite(ledPin, Arduino.HIGH); 
    arduino.digitalWrite(ledPin1, Arduino.HIGH); 
    arduino.digitalWrite(ledPin2, Arduino.HIGH); 
    } 
println("3"); 


    if (dir >= 10 && dir <= 25) { 
    println("4"); 

    arduino2.analogWrite(9, 90);    
    delay(1500);      

    } 
    else if (dir > 340 && dir <= 360) { 
    println("Low speed"); 


arduino2.analogWrite(9, 80); //in this case 80 is controlling the speed, although I wanted to control the angle 
    delay(1500);  
    arduino2.analogWrite(9, 120);  // same here  
    delay(1500);  
    println("5"); 
    } 

} 

所有這些prinLn(1-5)是顯然只是代碼檢查器,看看它是否有什麼問題,但我想沒有,只是我不能控制角度或任何其他特殊的控制,而不是速度,但它不會停止旋轉,直到我拔下插頭大多數情況下爲usb)

我想我需要插入一些arduino示例的這部分,以使其正確移動:

void loop() 
{ 
    for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees 
    {         // in steps of 1 degree 
    myservo.write(pos);    // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position 
    } 

但不是很確定,因爲我嘗試過,但我可能沒有正確編程。

謝謝!

+0

搜索「firmata」一些討論洙基本上總結一切可能有人告訴我一種方法來控制Firmata連續舵機角度 再次感謝 –

回答

0

setup()僅在草圖初始化時調用一次。 循環()在運行期間被重複調用。因此,您的大部分代碼都需要在循環中。

我建議從一些簡單的草圖開始,讓事情一步一步地運行。

ladyada有一些很好的教程後,即進行串行通信在http://www.ladyada.net/learn/arduino/lesson4.html
的firmata庫保持在https://github.com/firmata/arduino/tree/master/examples
除了一些很好的例子,A​​rduino的論壇揭示了在http://forum.arduino.cc/