簡體   English   中英

在 C 中出現錯誤,說明在“{”標記之前不允許在此處定義函數

[英]Getting an error in C saying that function definition is not allowed here before '{' token

我為 Arduino 板制作了一個小 C 程序,通過壓電蜂鳴器播放一些音調。 我調整了它嘗試從串行監視器獲取輸入,使用 for 循環遍歷輸入並使用 if/if else 播放相應的音調。

不幸的是,我遇到了一個我無法弄清楚的問題,因為我對 C 還是很陌生。如果你有時間,你介意指出我正確的方向來解決它嗎?

提前致謝。

我收到一個錯誤:

/Assignment_02_V2.ino:在函數“void loop()”中:/Assignment_02_V2.ino:38:16:錯誤:在“{”標記之前不允許有函數定義

int main() { ^ 退出狀態 1

這是代碼:

#define noisyPin 11    // set pin 11 to "noisyPin"

// define notes
#define NOTE_A4 440   // set frequency of A
#define NOTE_B4 494   // set frequency of B
#define NOTE_C4 262   // set frequency of C
#define NOTE_D4 294   // set frequency of D
#define NOTE_E4 330   // set frequency of E
#define NOTE_F4 349   // set frequency of F
#define NOTE_G4 392   // set frequency of G
#define NOTE_C5 523   // set frequency to high C

// set strings for serial monitor input
String msgToUsr = "Enter your note selection (A-G). A space indicates a pause of 0.5 seconds: ";
String usrMelody;

void setup() {
    pinMode(13, OUTPUT);  // set the onboard LED to indicate when a sound is playing
    pinMode(noisyPin, OUTPUT);  // directs noisyPin (pin 3) to output
    Serial.begin(9600);
}

void loop() {
    Serial.println(msgToUsr);
    while (Serial.available()==0) { 
    }
  
    usrMelody = Serial.readString();
    melodyLength = strlen(usrMelody);
  
    int main() { 
        int i=0; 
        for (i = 1; i < melodyLength; i++) {
            if (i == c || i == C) {
                buzzer(noisyPin, NOTE_C4, 500);   // activate buzzer to play NOTE_C4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == d || i == D) {
                buzzer(noisyPin, NOTE_D4, 500);   // activate buzzer to play NOTE_D4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == e || i == E) {
                buzzer(noisyPin, NOTE_E4, 500);   // activate buzzer to play NOTE_E4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == f || i == F) {
                buzzer(noisyPin, NOTE_F4, 500);   // activate buzzer to play NOTE_F4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == g || i == G) {
                buzzer(noisyPin, NOTE_G4, 500);   // activate buzzer to play NOTE_G4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == a || i == A) {
                buzzer(noisyPin, NOTE_A4, 500);   // activate buzzer to play NOTE_A4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else  if (i == b || i == B) {
                buzzer(noisyPin, NOTE_B4, 500);   // activate buzzer to play NOTE_B4 for 0.5 seconds
            }
            // delay(500);                       // set delay between HIGH to 0.5 seconds
            else if (i == " ") {
            delay(500)
            }
        } 
    return 0; 
    }
} 

void buzzer(int targetPin, long frequency, long length) {
    digitalWrite(13, HIGH); // turn the onboard LED on
    long delayValue = 1000000 / frequency / 2; // calculate delay value
    // 1 second (in microseconds) divided by frequency divided into 2 (two phases in every cycle)
    long cycleCount = frequency * length / 1000; // calculate the total number of cycles
    // frequency (cycles per second) multiply by the number of seconds divded by 1 second
    for (long t = 0; t < cycleCount; t++) { // for loop to play sound based on the calculations above
        digitalWrite(targetPin, HIGH); // set pin to HIGH to play sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
        digitalWrite(targetPin, LOW); // set pin to HIGH to stop sound
        delayMicroseconds(delayValue);  // delay sound based on calculated delay value above
    }
    digitalWrite(13, LOW); // turn the onboard LED off
}

在 C 中不能有嵌套函數。在你的代碼中,主函數寫在循環函數中。

查看C 中的嵌套函數以獲得一些額外的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM