1

我的問題是,我正在發出警報,當溫度超過預設的量。然而Arduino正在通過if語句並且沒有停在門口。 我嘗試過查找不同的語法,但它繼續通過if語句,像它不在那裏,而它仍然序列打印。該arduino正在跳過if語句,只是連續運行整個鱈魚我無法弄清楚如何解決它

//naming the led's left to right defining the pin and the constant int 
//coded by luke 
// the breadboard has 6 leds lined up by order 1,2,3,4,5,6 on the corresponding pins 
//6.28.2014 
// second program written from scratch 
// !buzzer pin 8! 
// integers 
const int led1=(1); 
const int led2=(2); 
const int led3=(3); 
const int led4=(4); 
const int led5=(5); 
const int led6=(6); 
const int buzzer=(8); 
const int temp=(0); 
int maxtemp=(80); 


// place the buzzer at pin 8 for sound 

void setup() 
{ 
    pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 
    pinMode(led3, OUTPUT); 
    pinMode(led4, OUTPUT); 
    pinMode(led5, OUTPUT); 
    pinMode(led6, OUTPUT); 
    pinMode(temp, INPUT); 
    Serial.begin(9600); 
} 

void loop() 
{ 
    delay (1000); 
    analogRead(temp); 
    delay (100); 
    Serial.print(analogRead(temp)/2.05); 
    delay (10); 
    if(analogRead(temp/2.05) > maxtemp) 
    { 
     tone (buzzer, 440); 
     delay (100); // wait to turn on the second 
     digitalWrite(led2, HIGH); 
     delay (100); 
     digitalWrite(led1, LOW); 
     delay (100); 
     digitalWrite (led3, HIGH); 
     delay (100); 
     digitalWrite (led2, LOW); 
     delay (100); 
     tone (buzzer, 450); 
     digitalWrite (led4, HIGH); 
     delay (100); 
     digitalWrite (led3, LOW); 
     delay (100); 
     digitalWrite (led5, HIGH); 
     delay (100); 
     digitalWrite (led4, LOW); 
     delay (100); 
     digitalWrite (led6, HIGH); 
     delay (100); 
     digitalWrite (led5, LOW); 
     delay (100); 
     digitalWrite (led6, LOW); 
    } 
} 

// put your main code here, to run repeatedly: 

回答

0

什麼你打印到串口是不一樣的東西你比較maxtemp if語句:

Serial.print(analogRead(temp)/2.05); 

if(analogRead(temp/2.05) > maxtemp) 

前者/2.05analogRead外呼,後者它裏面。假設前者正在報告您期望看到的內容,將if語句更改爲:

if((analogRead(temp)/2.05) > maxtemp) 

應該解決問題。