PIR(Module SB0061) AND ULTRASONIC (HC-SR04) SENSOR COMBINATION

Hi friends,
I have been doing a simple DIY project with these two sensors separately with my INFIDUINO UNO(Arduino uno R3_full_kit)

1.Using my pir sensor(INPUT) , LCD 1602,Leds and Buzzer(OUTPUT) have found it working 100% perfect.
(and what I wanted is 100% done.)

2.Using my ultrasonic distance sensor(INPUT) , LEDs and a buzzer(OUTPUT) have found this part too 100% perfect.
(and what I wanted is 100% done.)

3.Last part of this project is combining 1st and 2nd part to be run by my Arduino simultaneously, the general circuit and codes are without any error BUT when I upload it to the Arduino IT'S ONLY part one which functions leaving 2nd part totally down(function-less at all)

I don't know if there is any problem with compatibility OR can't the arduino run two sensors performing two different tasks simultaneously?? According to my Arduino knowledge though am a NEWBIE I know that the Arduino boards are multi-functional , what are the key points to consider to make it ??

the 1st 2nd 3rd codes are somehow long, if needed will post them here for better understanding and easier problem solving.

Thank you!

..post code!

Knut_ny Sir , here are the codes,,, sorry for a late reply, was out!!

// 1st PART CODE, timer

#include <LiquidCrystal.h> // library

//initializing the library with the numbers of the interface Pins_LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // THE 1602_16_PINS LCD

...........................................................................
...........................................................................
............................................................................

pinMode(A3,OUTPUT); //***buzzer
}
void loop() {
// measure distance : send "ping trigPin will always be changing HIGH to LOW
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

//Measure distance: listen for "ping" echoPin will always be HIGH
pinMode(echoPin, INPUT);
pulseDuration=pulseIn(echoPin, HIGH);

// divide by two (back/forth for a single trip), divided by speed of sound(cm)
pulseDuration = pulseDuration/2;
distance = int(pulseDuration/29); // sound travels at 1cm/29 microseconds

//****light up RED_LED: inverted linear of 0-25 to 0-255. off to max_brightness
if(distance > 0 && distance < 60){ // from 60cm->0 RED LED will light dimming towards 0
int RedValue = (60-distance)*2; //10.2; 2 is great // ????
analogWrite(RedLedPin, RedValue); //

} else {
analogWrite(RedLedPin, 0);
}

//////BUZZER CODE WORKS FINE BUT NOISE OMG
if (distance > 0 && distance < 30){
digitalWrite(A3, HIGH);
} else {
digitalWrite(A3, LOW);
}
////////IF TOO CLOSE

// ***light up GREEN_LED: 60-100cm ^=255-0 on GreenLedPin



...................................................
count=30; //MAX INITIAL FOR COUNTDOWN =30 seconds FREELY changeable
delay(2000);
lcd.clear();
}

/// IMPORTANT_REGION_CODES_FOR_COUNTDOWN
void countdown() //
{
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // checks if the sensor is HIGH
{
digitalWrite(led, HIGH); //turn LED ON_SHOWing THAT PIR IS ACITVE
digitalWrite(8, LOW); // MAKE IT LOW TO AVOID NOISE PCDZ_buzzer
delay(0);

for(int i = 0; i < count; i++) // from 30->0 secs
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print( count - i ); //REMAINING SECONDS_lcd_1602_DISPLAY
lcd.print(" S........"); //

lcd.setCursor(0, 1);
lcd.print("ST!~");
digitalWrite(10, 30); //let the small RED_LED blink PER SEC 30_times
delay(500); // 1 second led_blinking+timer
digitalWrite(10, 0); //stop LED
d; // 1 second led_blinking+timer
}
}
} if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}else {
digitalWrite(led, LOW); // turn LED OFF
delay(5); // delay 200 milliseconds

if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

/////////LOOP CODES FOR ACTION////////////////////
.......................................................................
..........................................................................
.......................................................................
.......................................................................

The problem is the delays. You have to get rid of them!
There is an example "blink without delay" (found under 02.digital).

The idea is to monitor the "millis()" (current 'time')
Store 'time' in a long variable. Next printout will have to wait til that stored time is passed with a predifined number of milliseconds.

If there are several things that shall occur at different time intervals, you can use several variables to store time.

Store 'time' in a long variable.

unsigned long, that that is what millis() and micros() return. Keeps the math consistent.

Simple example:

if (digitalRead(startButton) == LOW){
startTime = millis();
}

currentTime = millis();
elapsedTime = currentTime - startTime;
if (elapsedTime >= duration){
// enough time has passed, do something
}

ooh Thanks a lot Knut_ny and CrossRoads Sir,
am going to try those alternatives, and will feedback you here!

Thanks

I have understood the concept of blinking a single, two ,three and even more LEDs without DELAYS(), and
Have tried to apply that concept(unsigned long millis()) to my project with PIR especially in 1st part first before combination, as it's the only part having if not all then most of DELAYS BUT seems to be so complicated and time consuming, disturbing the general flow of what I wanted to be displayed on my LCD 1602.. as am trying to interface those delays with the LCD SCREEN DISPLAY and NOT Leds,

am afraid I will need to buy another Arduino uno R3, to use two USB ports of the PC only to make sure they run at the same time....
sounds awkward though to use two boards for simple wiring I have made so far, I have no way as HAVE TRIED IT MUCH..and will keep trying too. I guess I need more time to work on it

I will appreciate IF any coding help by (unsigned long , millis()) will be given, instead of delays esp in 1st part code to enable it functioning in the same flow

Thanks.

Hi Friends i have doubt with my code can you please help me out with these,
i am interfacing pir sensor and ultrasonic sensor with 2 servomotor, for my project.
these is my code

#include <Servo.h>
#define trigPin 9
#define echoPin 10
#define pir 2
Servo servo;
Servo servo1;
void setup()

{

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(pir, INPUT);

servo.attach(7);

servo1.attach(8);

}

void loop()

{

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

if(distance <= 400)
{

servo.write(180);

}

else

{

servo.write(0);

}
if (digitalRead(pir)==HIGH)

{
Serial.println("Motion Detected");

servo1.write(120);

delay(2000);

}

else

{
Serial.println("Motion not Detected");

servo1.write(0);

delay(2000);
}

if (distance > 400 || distance <= 0)

{
Serial.println("The distance is more than 400");

}

else

{

Serial.print(distance);

Serial.println(" cm");

}

delay(1000);

}