operating two relays simultaneously

Hi!
I have two 5-240 V relays connected to my arduino uno. they are both triggered by two ultrasonic sensors each. these very sensors trigger two various sounds - again two sensors for one sound. the sound and relay switch form two pairs that operate in sync within itself. now the problem is, neither the sound, nor the relay can be activated if the other sound and relay is already activated by the sensor.
my code is really basic and it this far only works for three sensors too… (which is another problem im facing… if i add one more it starts misbehaving triggering itself without an actual proximity object) either way, i saw posts around claiming it is possible to run several relays and activate several pins at the same time - just i couldnt really see how from these posts… could anyone here be of some assistance?

here s the code

int trigerPin1 = 8;
int echoPin1 = 9;
int trigerPin2 = 6;
int echoPin2 = 7;
int trigerPin3 = 4;
int echoPin3 = 5;
int trigerPin4 = 2;
int echoPin4 = 2;
int sound1 = 11;
int sound2 = 10;
int lamp1 = 13;
int lamp2 = 12;


void setup() {
  pinMode(trigerPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigerPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigerPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigerPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(lamp1, OUTPUT);
  pinMode(lamp2, OUTPUT);
  pinMode(sound1, OUTPUT);
  pinMode(sound2, OUTPUT);
}

void loop() {

  // BEGINING OF 1st FRAME

  long duration, distance;
  Serial.begin(9600);
  digitalWrite(trigerPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigerPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigerPin1, LOW);
  duration = pulseIn(echoPin1, HIGH);
  distance = duration / 58.2;
  Serial.println(distance);

  if (distance <= 5) {
    digitalWrite(lamp1, HIGH);
    if (digitalRead(sound1) == LOW) {
      digitalWrite(sound1, 1);
      delay(2000); //change this to set how long yor noise
    }
    if (digitalRead(sound1) == HIGH) {
      digitalWrite(sound1, 0);
      delay(2000); //change this to set how long yor noise
    }
  }
  else {
    digitalWrite(lamp1, LOW);
  }
  delay(20);

  Serial.begin(9600);
  digitalWrite(trigerPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigerPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigerPin2, LOW);
  duration = pulseIn(echoPin2, HIGH);
  distance = duration / 58.2;
  Serial.println(distance);

  if (distance <= 5) {
    digitalWrite(lamp1, HIGH);
    if (digitalRead(sound1) == LOW) {
      digitalWrite(sound1, 1);
      delay(2000); //change this to set how long yor noice
    }
    if (digitalRead(sound1) == HIGH) {
      digitalWrite(sound1, 0);
      delay(2000); //change this to set how long yor noice
    }
  }
  else {
    digitalWrite(lamp1, LOW);
  }
  delay(20);

  // END OF 1st FRAME

  // BEGINNING OF 2nd 

  Serial.begin(9600);
  digitalWrite(trigerPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigerPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigerPin3, LOW);
  duration = pulseIn(echoPin3, HIGH);
  distance = duration / 58.2;
  Serial.println(distance);

  if (distance <= 5) {
    digitalWrite(lamp2, HIGH);
    if (digitalRead(sound2) == LOW) {
      digitalWrite(sound2, 1);
      delay(2000); //change this to set how long yor noice
    }
    if (digitalRead(sound2) == HIGH) {
      digitalWrite(sound2, 0);
      delay(2000); //change this to set how long yor noice
    }
  }
  else {
    digitalWrite(lamp2, LOW);
  }
  delay(20);
}

the testing setup looks like this:

thanks a lot :slight_smile:

hofno:
now the problem is, neither the sound, nor the relay can be activated if the other sound and relay is already activated by the sensor.

That usually means that you are using the delay() function. It blocks the Arduino from doing other things.

Have a look at the demo Several Things at a Time which illustrates the use of millis() to manage timing without blocking.

...R

Couldn't make out from the picture how everything is powered. Note that the relay board is quite power hungry - will approach near 200mA with 2 relays energized.

At what voltage is Vin powered? (you'll get more current available if using 9V or better yet 7.5V)
Is the relay board separately powered and have you connected it to use it's opto-isolation feature?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

hofno:
could anyone here be of some assistance?

At first I’d remove every occurance of “Serial.begin(9600);” from the loop() function and put this ONCE ONLY into the setup() function.

At second I’d wait at least 50 milliseconds between triggering the ultrasonic pings, to avoid echos from a previous ping to disturb the next measurement.