2017-04-30 117 views
-2

這個程序調節電動機告訴它3秒前輸出120伏,3秒後輸出255伏..不知道爲什麼這不是編譯。我的arduino代碼有什麼問題?我得到一個失敗,編譯錯誤,不知道爲什麼

int motorPin = 9; 

void setup() { 
    // put your setup code here, to run once: 
    pinMode(9,OUTPUT); 
} 

void loop() { 
    // put your main code here, to run repeatedly: 
    #include <time.h> 

    if (int tm_sec<int tm_3sec); 
    analogWrite(9,120); 

    else (int tm_sec>int tm_3sec); 
    analogWrite(9,255); 
+0

您應該將'include'語句移出'loop()'函數。 'if's後面還要去掉分號。 Aslo,聲明'tm_sec' ....很多問題。 –

+0

#include在循環()中不合適。 – JimmyB

+2

請確實包含您的編譯器輸出 –

回答

0

有很多問題。首先,include指令用整個文件替換該行代碼,通常放置在源代碼文件的開頭。另外,這是Arduino。改爲使用millis()

const int motorPin = 9; 

void setup() { 
    pinMode(motorPin, OUTPUT); 
} 

void loop() { 
    // millis() resets to 0 when the Arduino resets 
    if (millis() < 3000) { 
     analogWrite(motorPin, 120); 
    } 
    else { 
     analogWrite(motorPin, 255); 
    } 
} 

但是,這不會輸出120V和255V。 Arduino無法做到這一點。 analogWrite根據您發送的值寫入PWM信號。您應該閱讀:https://www.arduino.cc/en/Reference/analogWrite

相關問題