-2

This is my code. If Relay 1 is on then don't turn off Relay 2 and if Relay 2 is on then don't turn off Relay 1. I can only open one relay at a time. I want it so that if relay 1 is on then don't close relay 2 if it is on and vice versa.

int D0=1; //Data pins 0 of DTMF Decoder int D1=2; //Data pins 1 of
DTMF Decoder int D2=3; //Data pins 2 of DTMF Decoder int D3=4; //Data
pins 3 of DTMF Decoder int Relay1  =  13; int Relay2 = 12;

void setup() {
  pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);
  pinMode(Relay2, OUTPUT);
  digitalWrite(Relay2, HIGH);
  }

void loop()  {
  // When 0 pressed : 0 0 0 0 (Turning all the relays to off state
  // When 1 pressed : 0 0 0 1
  if (digitalRead(D3)==0 && digitalRead(D2)==0 && digitalRead(D1)==0 && digitalRead(D0)==1 ) {
    digitalWrite(Relay1, HIGH); // Turning the Relay1 ON state
    delay(200);
  }
  if (digitalRead(D3)==0 && digitalRead(D2)==0 && digitalRead(D1)==1 && digitalRead(D0)==0) {
    digitalWrite(Relay1, LOW);
    delay(200);
  }
  if (digitalRead(D3)==0 && digitalRead(D2)==1 && digitalRead(D1)==0 && digitalRead(D0)==0) {
    digitalWrite(Relay2, HIGH); // Turning the Relay2 ON state
    delay(200);
  }
  if (digitalRead(D3)==0 && digitalRead(D2)==1 && digitalRead(D1)==0 && digitalRead(D0)==1) {
    digitalWrite(Relay2, LOW);
    delay(200);
  }
}
dda
  • 6,030
  • 2
  • 25
  • 34
Frankruss
  • 33
  • 8
  • So, what is your question and problem? – svtag Sep 12 '17 at 13:25
  • Brother this code is working fine.just i cant have both the relay on at the same time.When i switch to relay 1 then relay gets off and same with relay 1.I want if i open relay 1 then relay 2 should not shutdown.What condition should i put here. – Frankruss Sep 12 '17 at 13:32
  • You have to add flags, which keep trace of relay 1 and relay 2. Set these flags when a certain relay is switched on, and check that when turning other relay off. – svtag Sep 12 '17 at 13:35
  • Sorry buddy a little new on this.Can u give me an example.or any reference link.Thanks – Frankruss Sep 12 '17 at 13:38
  • In you previous comment there are 2 contradictory conditions `just i cant have both the relay on at the same time`. and `I want if i open relay 1 then relay 2 should not shutdown`. Is it like when Relay1 is switched `ON` then Relay2 can't be switched `OFF` but it can be switched `ON`? or is it when Either of 1 relay is `ON` then other should not change state? – svtag Sep 12 '17 at 13:46
  • i want both of them to act as seperate. like i press 4 relay 2 is on ok.now when i press 1 relay 1 should be on.but instead it closes the relay 2 and on the relay 1.and same with relay 1 when i press 1 relay 1 is on but simultaneously if i want relay 2 to open then relay 1 will be off. – Frankruss Sep 12 '17 at 13:56
  • Your logic seems correct. Just define `D0`, `D1`, `D2`, `D3` as `INPUT` pins (like `pinMode(D0, INPUT);`) and try again. It should work – svtag Sep 12 '17 at 14:07
  • tried buddy.Still same result. – Frankruss Sep 12 '17 at 14:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154247/discussion-between-sma-and-frankruss). – svtag Sep 12 '17 at 15:54

1 Answers1

0

Replaced your logic to read continuously and check to read once and check. This works in the test setup i created (just 4 push buttons and 2 LEDs).

void setup() {
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(D0, INPUT);
  pinMode(D1, INPUT);
  pinMode(D2, INPUT);
  pinMode(D3, INPUT);

  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
}

void loop() {
  byte DTMF_data = 0x00;
  DTMF_data = (digitalRead(D3) << 3) |  (digitalRead(D2) << 2) | (digitalRead(D1) << 1) | (digitalRead(D0) << 0);

  switch(DTMF_data) // Cases based on previous `if` conditions
  {
    case 1: // 0 0 0 1
      digitalWrite(Relay1, HIGH);
      delay(200);
      break;
    case 2: // 0 0 1 0
      digitalWrite(Relay1, LOW);
      delay(200);
      break;
    case 4: // 0 1 0 0
      digitalWrite(Relay2, HIGH);
      delay(200);
      break;
    case 5: // 0 1 0 1
      digitalWrite(Relay2, LOW);
      delay(200);
      break;
  }
}
svtag
  • 184
  • 4
  • 12
  • Thanku so much buddy it worked.Can u explain this byte DTMF_data = 0x00; DTMF_data = (digitalRead(D3) << 3) | (digitalRead(D2) << 2) | (digitalRead(D1) << 1) | (digitalRead(D0) << 0); – Frankruss Sep 12 '17 at 15:02
  • `byte DTMF_data = 0x00;` means that i create a `byte` type variable `DTMF_data` (size of 1 byte) and assign it to 0. Second line is reading all 4 digital pin and appending them such that they make 1 single digit. You can rad more about it here: https://stackoverflow.com/a/141873/6594779 – svtag Sep 12 '17 at 15:08