2016-02-18 8 views
-8

我是編碼領域的初學者,特別是在Arduino中。我一直在做很多項目,但我遇到了一個問題。我似乎無法弄清楚如何製作一個有限循環。我正在尋找類似於while循環,四次後停止。這裏是它將被實施的地方,讓你更好地瞭解我正在尋找的東西。如何在Arduino中製作有限循環

#include <Servo.h> 

int thumbPin = 2; 
int ndxPin = 3; 
int midPin = 4; 
int rngPin = 5; 
int pnkyPin = 6; 

Servo thumb; 
Servo index; 
Servo middle; 
Servo ring; 
Servo pinky; 
void setup() { 
    Serial.begin(9600); 

    thumb.attach(thumbPin); 
    index.attach(ndxPin); 
    middle.attach(midPin); 
    ring.attach(rngPin); 
    pinky.attach(pnkyPin); 
} 
void loop() { 
    /* I want this code in the comment to be ran four times, then continued on to the code after 
    thumb.write(0); 
    delay(20); 
    thumb.write(0); 
    index.write(0); 
    middle.write(0); 
    ring.write(0); 
    pinky.write(0); 

    thumb.write(150); 
    index.write(150); 
    middle.write(150); 
    ring.write(150); 
    pinky.write(150); 
    */ 
    thumb.write(0); 
    index.write(0); 
    pinky.write(0); 

    middle.write(0); 
    thumb.write(150); 
    pinky.write(150); 
} 
+2

你用C,C++或C#編碼?請不要垃圾標籤,但只使用與您的問題相關的標籤。 – kaylum

+0

Arduino既不是給定的語言!不要爲無關語言添加標籤。 – Olaf

+0

它看起來很像所說的語言,我知道基本的語法(同時,如果...其他等)幾乎是相同的。 –

回答

7

你想要一個for循環:https://www.arduino.cc/en/Reference/For

for (int i = 0; i < 4; i++) { 
    thumb.write(0); 
    delay(20); 
    thumb.write(0); 
    index.write(0); 
    middle.write(0); 
    ring.write(0); 
    pinky.write(0); 

    thumb.write(150); 
    index.write(150); 
    middle.write(150); 
    ring.write(150); 
    pinky.write(150); 
}