2013-05-16 58 views
1

我試圖讓我的Arduino在莫爾斯電碼中閃爍「SOS」,作爲我的第一個真正的編程項目。優雅地循環Arduino LED閃爍的消息

我已經成功地這樣做了,但現在我想更有效地編寫代碼。這是我的版本。如何實現一個while循環來使S閃爍兩次?這似乎使跟蹤迭代與計數器複雜化。

int led = 13; 
//yj Integer representing dit (morse ' . ') 
int dit = 500; 
//yj Integer representing dah (morse ' - '>) 
//yj Testing a multiple of another constant integer to make the timing scalable. 
int dah = dit*3; 
int blankTime = 100; 

// the setup routine runs once when you press reset: 
void setup() {     
    // initialize the digital pin as an output. 
    pinMode(led, OUTPUT);  
} 

// the loop routine runs over and over again forever: 
void loop() { 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 

    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dah);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dah);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dah);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 

    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) 
    delay(dit);    // wait 
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW 
    delay(blankTime);    // wait 
} 

//TODO: 
//Loop counter implementation for multiples (SOS = 3 dit, 3 dah, 3 dit.) 

回答

0

我會建議一個簡單的函數來生成莫爾斯碼而不是一次又一次地寫入interation。它是這樣的東西。

GenerateMorseCode(char MorseArray [],int len);

您可以使用dit和dah的一些標識符填充MorseArray:可以使用點(。)和hipen( - )。所以對於3點,3點和3點。你的陣列將

...---... 

與數組的長度9.現在GenerateMorseCode函數內部所有你需要做的解析MorseArray。您可以使用簡單的開關情況下是這樣的

GenerateMorseCode(char MorseArray[], int len) 
{ 
    for(int i =0i<len;i++) 
{ 
switch(MorseArray[i]) 
{ 
    case '.': 
    //Your led logic in case of dot 
    break; 
    case '-': 
    //Your led logic in case of dah 
    break; 

} 

} 

} 

進一步你可以用它保持通話GenerateMorseCode功能按規定的循環另一個函數內部此功能。要生成MorseArray,你可以創建一個帶莫爾斯碼的char數組地圖。您可以繼續添加新的枚舉並更新相應的Morsemap。像這樣的東西

#define MAX_ELEM 12 
enum MorseCode 
{ 
    A_M = 0, 
    B_M, 
    C_M, 
    SOS_M 
}; 

typedef struct MorseMap 
{ 
    char array[MAX_ELEM]; 
}; 

MorseMap mm[3] = {{'.','-','\0'},{'-','.','.','.','\0'},{'-','.','-','.','\0'} }; 

也沒有必要現在的長度,它可以從char數組計算。訪問任何莫爾斯電碼使用毫米[A_M]或毫米[SOS_M]

GenerateMorseCode(char arr[]) 
{ 
    int len = strlen(arry); 
    //Above for loop and switch case case 
} 
+0

非常感謝,我稍後會看看它,因爲它看起來很複雜,而且我在我的手機上。 –

0

您希望的方法來表示「短劃線」,並表示「點」的方法。 下面是使用銷#13上的烏諾一個例子:http://www.devfactor.net/2014/09/22/arduino-made-easy-output-messages-in-morse-code/

/* 
Morse SOS 
*/ 

// the setup function runs once when you press reset or power the board 
void setup() { 
    // initialize digital pin 13 as an output. 
    pinMode(13, OUTPUT); 
} 

// the loop function runs over and over again forever 
void loop() { 

    for (int a = 0; a < 3; a++) { 
    dot(); 
    } 
    for (int b = 0; b < 3; b++) { 
    dash(); 
    } 
    for (int c = 0; c < 3; c++) { 
    dot(); 
    } 
} 

void dot() 
{ 
    digitalWrite(13, HIGH); 
    delay(250); 
    digitalWrite(13, LOW); 
    delay(250); 
} 

void dash() 
{ 
    digitalWrite(13, HIGH); 
    delay(1000); 
    digitalWrite(13, LOW); 
    delay(250); 
} 

注意,破折號()和點()方法使得重複更elegant-並且需要比硬編碼每一劃線或點更少的代碼。