Ultrasonic Sensor & PIR

Hi,

I am working on a project where when the PIR detects movement and the ultrasonic sensor reads below a given value it triggers a relay and a media player off a digital output pin.

If during the playing of the video the PIR sensor detects no movement it stops the video and releases the relay

Currently I have this in a loop by which if the value of the PIR is equal to 1023 and the ultrasonic sensor give the correct value it will start the video and trigger the relay.

The issue i have is that if the PIR gives a false reading or the user stops moving the video will stop immediately, i need to implement something so that it doesn't stop on the first reading. How would you recommend i could achieve this?

int pirPin = A5; //digital 5
int Screen = 7;
int Play = 2;
int Stop = 3;
int ez1Analog = 0;

void setup(){
 Serial.begin(9600); 
 pinMode(pirPin, INPUT);
 pinMode(Screen,OUTPUT);
pinMode(ez1Analog,INPUT);
pinMode(Play, OUTPUT);
pinMode(Stop, OUTPUT);

}

void loop(){
  int pirVal = analogRead(pirPin);
  int val = analogRead(ez1Analog);

  if(pirVal > 0){ //was motion detected
    Serial.println(pirVal); 
    

if (pirVal = 1023);
      if (val < 40) //Value set from Sensor Test File
        {Serial.print(val); 
        digitalWrite(Play, HIGH);
        delay(100);
        digitalWrite(Play, LOW);  
        {digitalWrite(Screen, HIGH);
        delay(2000);
      
        } }
else{   delay (2000);
        if (pirVal < 5); //pir value set to a minimum of 5 based on readings from PIR Test file
        {digitalWrite(Stop, HIGH);
        delay(200);
        digitalWrite(Stop, LOW);  
        digitalWrite(Screen, LOW);
  }

}}}

Many Thanks

if (pirVal = 1023); <<<<<<<< the ; should not be there as that effectively closes the IF statement,.

some remarks:

  • Please use CTRL-T before posting code, this reformats your code a bit for readability
    Keeping your code readable will give you more feedback as it makes
  • use [ code ] tags => # button above the smileys (you can modify your exiting post)

Many thanks for that.

So that will ensure the if statement works. Would it be best to add in a count loop for the pir sensor?

Matt

if (pirVal = 1023);

Not only should the semicolon not be there, the "=" should have a twin.

sorry - very new to this. What do you mean by a twin?

Thanks

Matt

An identical twin.

im not sure i understand where the twin would sit as it only needs to read the value once?

if (pirVal == 1023)

Identical twin.

if (pirVal = 1023)

Will assign 1023 to pirVal, and test the result, which will always be true