为什么红灯不亮?

Why is the red light not lighting up?

所以我对这个 IOT 东西很陌生,我想在这里创建的有点像交通违规,我的想法是当红灯亮起时,如果 PIR 传感器检测到 buzzer/led 走高。

图片如下:

代码如下所示

int pir = 2;
int rojo = 12; 
int amarillo = 11;
int verde = 10;
int led = 7;

void setup()
{
  pinMode(pir, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  pinMode(verde, OUTPUT); //It declares the green pin as output 
  pinMode(amarillo, OUTPUT);//It declares the yellow pin as output 
  pinMode(rojo, OUTPUT);
}
    
void loop()
{
  digitalWrite(verde, HIGH); //It turns on the green led 
  delay(15000); //wait 15 seconds 
  digitalWrite(verde, LOW); //It turns off the green led 
  delay(250); //wait 0.25 seconds
  
  digitalWrite(amarillo, HIGH); //It turns on the yellow led 
  delay(3000); //wait 3 seconds 
  digitalWrite(amarillo, LOW); //It turns off the yellow led 
  delay(250); //wait 0.25 seconds
  int val = digitalRead(pir);
  Serial.println(val);
    
  digitalWrite(rojo, HIGH); //It turns on the red led 
  delay(15000); //wait 15 seconds 
  digitalWrite(rojo, LOW);
  if (rojo == HIGH) {
    if (val == HIGH) {
      digitalWrite(led, HIGH);
    } else {
      digitalWrite(led, LOW); 
    }
    delay(10);
  }
}

您需要更改 if

的位置
int pir=2;
int rojo=12; 
int amarillo=11;
int verde=10;
int led=7;

void setup()
{
    pinMode(pir,INPUT);
    pinMode(led,OUTPUT);
    Serial.begin(9600);
    pinMode(verde,OUTPUT); //It declares the green pin as output 
    pinMode(amarillo,OUTPUT);//It declares the yellow pin as output 
    pinMode(rojo,OUTPUT);
}
    
void loop()
{
    digitalWrite(verde,HIGH); //It turns on the green led 
    delay(15000); //wait 15 seconds 
    digitalWrite(verde,LOW); //It turns off the green led 
    delay(250); //wait 0.25 seconds
    
    digitalWrite(amarillo,HIGH); //It turns on the yellow led 
    delay(3000); //wait 3 seconds 
    digitalWrite(amarillo,LOW); //It turns off the yellow led 
    delay(250); //wait 0.25 seconds
    int val = digitalRead(pir);
    Serial.println(val);
    
    digitalWrite(rojo,HIGH); //It turns the red led 
    if(val==HIGH){             //---> //I dont know if this value can be compared with HIGH, on my programm whe costum use numbers!
        digitalWrite(led,HIGH);
    }
    else{
        digitalWrite(led,LOW); 
    }
    delay(15000); //wait 15 seconds 
    digitalWrite(rojo,LOW);
    delay(10);
}

问题出在这里:

  digitalWrite(rojo, HIGH); //It turns on the red led 
  delay(15000); //wait 15 seconds 
  digitalWrite(rojo, LOW);
  if (rojo == HIGH) {
    if (val == HIGH) {
      digitalWrite(led, HIGH);
    } else {
      digitalWrite(led, LOW); 
    }
    delay(10);
  }

首先,rojo是一个pin号,不是你想在这个比较中使用的值。

其次,在您的延迟 delay(15000) 期间,代码停止 运行,因此在此期间未检测到移动。

检测 15 秒延迟的唯一方法是使用 millis() 进行计时和延迟(或使用中断)。

您可以尝试这样的操作(未经测试):

digitalWrite(rojo, HIGH); //It turns on the red led 
unsigned long int redStartTime = millis();
while (millis() - redStartTime <= 15000) {
  delay(100);
  int val = digitalRead(pir);
  if (val == HIGH) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW); 
  }
}
digitalWrite(rojo, LOW);

我没有对此进行测试,但我希望你能理解。

请注意,当有物体移动时,我不知道运动检测器是 returns HIGH 还是 LOW;您可能需要更改那里的代码。