1

So I am very new to this IoT stuff and what I am trying to create here is somewhat like traffic violation detection.

My idea is: when the red light is on and if the PIR sensor detects movement, the buzzer/LED turns on.

Here's the image:

enter image description here

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);
  }
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • Please embed the image into your post (will generate a stackoverflow imgur link, and the community can embed it for you even if you don't have enough reputation yet) https://stackoverflow.com/posts/64971238/edit – ti7 Nov 23 '20 at 15:27
  • 1
    `rojo` is a *number* of an output pin (12), but you compare it to `HIGH` (`1` I presume) as if it was some input signal. This will obviously be always false. – Eugene Sh. Nov 23 '20 at 15:28
  • @EugeneSh. is right; the best way to handle this is to name variables more clearly, such as changing `rojo` to `rojoPinNumber`. – Steve Friedl Nov 23 '20 at 15:34
  • @Anonymous 286 What you need to do is create a variable wich contains the statment of rojo, and you will never get the rojo on, because you turn it off, before your if so you need to do something like I posted above! – Gonçalo Bastos Nov 23 '20 at 15:37
  • 1
    @ti7 sry I did not know this,will keep in mind – Anonymous 286 Nov 23 '20 at 15:58
  • @EugeneSh. So what should I do to achieve my desired output,can you guide me? – Anonymous 286 Nov 23 '20 at 15:59
  • @GonçaloBastos I don't know why but the code you gave doesn't work for me – Anonymous 286 Nov 23 '20 at 16:02
  • @SteveFriedl will keep that in mind – Anonymous 286 Nov 23 '20 at 16:02
  • @Anonymous286 Maybe because your val dont return HIGH or LOW, maybe its 0 or 1023! I made a program simmilar but I dont look at your entire code, I just figured out that you need to check the value of val in the moment that led is red, so if value is 1 turn light, if its 0 do not turn it on! PS- 0 and 1 its just exempleficatives numbers – Gonçalo Bastos Nov 23 '20 at 16:05
  • @GonçaloBastos thanks for helping – Anonymous 286 Nov 23 '20 at 16:51

2 Answers2

3

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.

Second, during your delay delay(15000), the code stops running, so movement is not detected during this time.

The only way to detect during the 15s delay is by using millis() for your timing and delay (or using an interrupt).

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 you get the idea.

Note that I don't know if the motion detector returns HIGH or LOW when something moves; you may need to change the code there.

ocrdu
  • 2,172
  • 6
  • 15
  • 22
0

You need to change the position of your 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);
}
Gonçalo Bastos
  • 376
  • 3
  • 16
  • Won't work; the idea is to light up led if at any time within the 15 seconds the RED led is on movement is detected; not just at the time the red LED is switched on. – ocrdu Nov 23 '20 at 16:05
  • @ocrdu yeah that's exactly what I want to do can you help me out with that? – Anonymous 286 Nov 23 '20 at 16:11
  • @Anonymous-286: See answer. It may need some work, haven't tested it. – ocrdu Nov 23 '20 at 16:18