简体   繁体   English

为什么红灯不亮?

[英]Why is the red light not lighting up?

So I am very new to this IOT stuff and what I am trying to create here is somewhat like traffic violation and my idea is when the red light is on and if the PIR sensor detects the moment the buzzer/led goes high.所以我对这个物联网的东西很陌生,我在这里尝试创建的东西有点像交通违规,我的想法是红灯亮时,如果 PIR 传感器检测到蜂鸣器/LED 变高的那一刻。

Here's the image:这是图片:

在此处输入图片说明

Here's what the code looks like这是代码的样子

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);
  }
}

The problem is here:问题在这里:

  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);
  }

First of all, rojo is a pin number, not a value you want to use in this compare.首先, rojo是一个引脚编号,而不是您要在此比较中使用的值。

Second, during your delay delay(15000) , the code stops running, so movement is not detected during this time.其次,在您的延迟delay(15000) ,代码停止运行,因此在此期间未检测到移动。

The only way to detect during the 15s delay is by using millis() for your timing and delay (or using an interrupt).在 15 秒延迟期间进行检测的唯一方法是使用millis()进行计时和延迟(或使用中断)。

You could try something like this (untested):你可以尝试这样的事情(未经测试):

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);

I didn't test this, but I hope you get the idea.我没有测试这个,但我希望你能明白。

Note that I don't know if the motion detector returns HIGH or LOW when something moves;请注意,当某物移动时,我不知道运动检测器是返回HIGH还是LOW you may need to change the code there.您可能需要更改那里的代码。

You need to change the position of your if你需要改变你的位置 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM