1

我的问题是,当温度高于预设值时,我会发出警报。然而,Arduino 正在通过 if 语句而不是停在门口。我尝试查找不同的语法,但是它一直通过 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: 
4

1 回答 1

0

您打印到串行的内容与您maxtemp在 if 语句中比较的内容不同:

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

if(analogRead(temp/2.05) > maxtemp)

前者有/2.05analogRead召,后者有内召。假设前者正在报告您期望看到的内容,请将 if 语句更改为:

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

应该解决问题。

于 2014-07-01T02:16:12.933 回答