我正试图让我的 Arduino 在摩尔斯电码中闪烁“SOS”作为我的第一个真正的编程项目。
我已经成功地做到了,但是现在我想更有效地编写代码。这是我的版本。如何实现 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.)