The use of 4 PCS RCWL-0516 sensors at the same time!

Hello, I want to create a site around my caravan. I use 4 RCWL-0516 sensors and 4 channel relays for this. I want the lighting and siren to be activated by relays when the sensor detects motion. My problem is this; Code for 1 sensor is ready and running. However, I could not activate 4 sensors at the same time. I want to constantly control 4 sensors and activate the relays when any sensor or 2-3 sensors detect motion, if there is no motion detection in the sensors, the relays stop. What code is required to do these operations / transactions with the for loop? Your opinions are important to me. Thank you ...

I've used that sensor.

Post the code you have that works well for one sensor and relay.

Post any attempt you have made to use four sets.

Describe the physical setup: distances between the sensors and computer and a wiring diagram or proposal.

What means "I could not activate 4 sensors at the same time."?

a7

the code I use;

int Role1Pin = 9;
int Role2Pin = 10;
int Role3Pin = 11;
int Role4Pin = 12;
int SensorPin = 8;
int SensorDurum = LOW;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(Role1Pin, OUTPUT);
pinMode(Role2Pin, OUTPUT);
pinMode(Role3Pin, OUTPUT);
pinMode(Role4Pin, OUTPUT);
pinMode(SensorPin, INPUT);
}
void loop(){

val = digitalRead(SensorPin);
if (val == HIGH)
{
Serial.println(" HAREKET VAR ");
digitalWrite(Role1Pin, LOW);
digitalWrite(Role2Pin, LOW);
digitalWrite(Role3Pin, LOW);
digitalWrite(Role4Pin, LOW);
delay(4000);
if (SensorDurum == HIGH)
{
delay(4000);
SensorDurum = HIGH;
}
}
else
{
Serial.println(" HAREKET YOK ");
digitalWrite(Role1Pin, HIGH);
digitalWrite(Role2Pin, HIGH);
digitalWrite(Role3Pin, HIGH);
digitalWrite(Role4Pin, HIGH);

if (SensorDurum == LOW)
{
delay(4000);
SensorDurum = LOW;
}
}
}

The characters of the code I sent are missing !!

I cannot assign pins for 4 different pins (for example, pins 3, 4, 5 and 6) and operate the sensors at the same time ...

By the way, I joined from Turkey. I use google translate for my posts. I apologize for any possible inconveniences ...

Why not? And why not read them in exactly the same manner as you now read one?

Then you can use the 'or' operator with the four values.

Post what you have tried and describe how it is not doing the right thing.

Are you trying to close one really if the corresponding sensor is triggered?

Or all relays if any sensor is triggered?

a7

no. any, some or all of the sensors can be active. In this case, I want the relays to work.

Why not? What did you try? What happened?

a7

I had a try like this;

int relay1 = 7;
int relay = 8;
int relay = 9;
int relay = 10;
int sensors[ ]={3,4,5,6};

void setup() {
for (int i=0; i<4; i++)
{
pinMode(sensors[i],INPUT);
}
}
void loop()

{
for (int i=0;i<4;i++)
{
digitalWrite(sensors[i], HIGH);
if (i == HIGH)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
delay(5000);
}
}
}

Look at your original reading of one sensor

and compare it to how you are (not!) reading one from the array:

and here you are looking at the for loop index, not a sensor reading.

Just be a bit more careful!

Obvious error, here, too:

It makes no difference, but since you seem to have a grasp of the concept of arrays, one could be used for the relays in the same manner. But it makes no difference.

a7

void setup() {
for (int i=0; i<4; i++)
{
pinMode(sensors[i],INPUT);
}
}
void loop()

{
for (int i=0;i<4;i++)
{
digitalWrite(sensors[i], HIGH);
if (i == HIGH)
{

I fix other mistakes,
is there a mistake in this part?

If I make a link like this without dealing with coding, would it be a problem if I run the existing code?arduino

If I connect 4 or more sensors with diodes to a single pin

Fix the mistakes I pointed out, then

Post all your current code.

Auto-format your code in the IDE and use the IDE to “copy for forum” before you paste it into your next post, please.

There is no need to think about diodes and using one input pin. It may be possible, but at this point will only introduce more places for you to make mistakes.

Kolay gelsin

Edit: or just wait for someone to write you a nice program! :wink:

a7

const byte NumberOfSensors = 4;
const byte NumberOfRelays = 4;

int RelayPins[NumberOfRelays] = {7, 8, 9, 10};
int SensorPins[NumberOfSensors] = {3, 4, 5, 6};

void setup()
{
  Serial.begin(9600);

  for (int i = 0; i < NumberOfSensors; i++)
    pinMode(SensorPins[i], INPUT);

  for (int i = 0; i < NumberOfRelays; i++)
    pinMode(RelayPins[i], OUTPUT);
}

void loop()
{
  bool alarm = false;

  for (int i = 0; i < NumberOfSensors; i++)
  {
    if (digitalRead(SensorPins[i]) == HIGH)
      alarm = true;
  }

  if (alarm)
  {
    Serial.println(" HAREKET VAR ");
    for (int i = 0; i < NumberOfRelays; i++)
      digitalWrite(RelayPins[i], LOW);
  }
  else
  {
    Serial.println(" HAREKET YOK ");
    for (int i = 0; i < NumberOfRelays; i++)
      digitalWrite(RelayPins[i], HIGH);
  }

  delay(5000);
}

Hello, thank you for your interest. I'll try the code at the earliest opportunity and let you know.

Teşekkür ederim ...

I used the code you prepared. no problem. Thank you again for your attention and assistance. Believe me it was highly appreciated ...

Teşekkür ederim...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.