relay - Arduino programming -
this code. if relay 1 on don't turn off relay 2 , if relay 2 on don't turn off relay 1. can open 1 relay @ time. want if relay 1 on don't close relay 2 if on , 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 relays 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 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 relay2 on state delay(200); } if (digitalread(d3)==0 && digitalread(d2)==1 && digitalread(d1)==0 && digitalread(d0)==1) { digitalwrite(relay2, low); delay(200); } }
replaced logic read continuously , check read once , check. works in test setup created (just 4 push buttons , 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; } }
Comments
Post a Comment