If statement arduino跳过了if语句,只是连续运行整个cod,我不知道如何修复它

If statement arduino跳过了if语句,只是连续运行整个cod,我不知道如何修复它,if-statement,logic,logical-operators,arduino-ide,arduino-uno,If Statement,Logic,Logical Operators,Arduino Ide,Arduino Uno,我的问题是,当温度高于预设值时,我会发出警报。然而,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

我的问题是,当温度高于预设值时,我会发出警报。然而,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: 

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

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

if(analogRead(temp/2.05) > maxtemp)
前者在analogRead调用外部有/2.05,后者在内部有它。假设前者正在报告您期望看到的内容,则将if语句更改为:

if((analogRead(temp) / 2.05) > maxtemp)
应该可以解决这个问题