Go Down

Topic: 4 channel relay with 4 buttons (Read 165 times) previous topic - next topic

r_dahiya

Apr 19, 2018, 07:27 pm Last Edit: Apr 19, 2018, 07:59 pm by r_dahiya
hi, I have a 4 channel relay and I want to use it for switching with arduino uno. I have written a code for it which is as under
Code: [Select]

#define reone 7 //relay no. 1
#define retwo 6
#define rethree 5
#define refour 4
#define sone A2 // switch no. 1
#define stwo A3
#define sthree A4
#define sfour A5
int val1=0;
int val2=0;
int val3=0;
int val4=0;
void setup()
{
pinMode (reone, OUTPUT);
pinMode (retwo, OUTPUT);
pinMode (rethree, OUTPUT);
pinMode (refour, OUTPUT);
pinMode (sone, INPUT);
pinMode (stwo, INPUT);
pinMode (sthree, INPUT);
pinMode (sfour, INPUT);
}
void loop(){
offline();
}
void offline(){
relay1();
relay2();
relay3();
relay4();
}
void relay1(){
val1 = digitalRead(sone); // read input value and store it
// check whether the input is HIGH (button pressed)
if (val1 == HIGH) {
digitalWrite(reone, HIGH); // turn relay ON
} else {
digitalWrite(reone, LOW);
}
}
void relay2(){
val2 = digitalRead(stwo); // read input value and store it
// check whether the input is HIGH (button pressed)
if (val2 == HIGH) {
digitalWrite(retwo, HIGH); // turn relay ON
} else {
digitalWrite(retwo, LOW);
}
}
void relay3(){
val3 = digitalRead(sthree); // read input value and store it
// check whether the input is HIGH (button pressed)
if (val3 == HIGH) {
digitalWrite(rethree, HIGH); // turn relay ON
} else {
digitalWrite(rethree, LOW);
}
}
void relay4(){
val4 = digitalRead(sfour); // read input value and store it
// check whether the input is HIGH (button pressed)
if (val4 == HIGH) {
digitalWrite(refour, HIGH); // turn relay ON
} else {
digitalWrite(refour, LOW);
}
}

the output of this code starts flickering, someone please can modify this code so that flickering may be removed and also is there any scope so that the length of code can be decreased...thanks

Robin2

To make it easy for people to help you please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum.

ALSO, please use the AutoFormat tool to indent your program code consistently for easier reading.


You say that the output starts flickering - does that mean that a relay goes on and off rapidly?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

r_dahiya

Yes, it goes on and off rapidly
To make it easy for people to help you please modify your post and use the code button </>
Code: [Select]
so your code looks like this
 and is easy to copy to a text editor. See How to use the Forum.

ALSO, please use the AutoFormat tool to indent your program code consistently for easier reading.


You say that the output starts flickering - does that mean that a relay goes on and off rapidly?

...R

Robin2

See how much easier it is to read your code when it is indented and has some whitespace between the functions. Use the AutoFormat tool.
Code: [Select]

#define reone 7 //relay no. 1
#define retwo 6
#define rethree 5
#define refour 4
#define sone A2 // switch no. 1
#define stwo A3
#define sthree A4
#define sfour A5
int val1=0;
int val2=0;
int val3=0;
int val4=0;

void setup()
{
    pinMode (reone, OUTPUT);
    pinMode (retwo, OUTPUT);
    pinMode (rethree, OUTPUT);
    pinMode (refour, OUTPUT);
    pinMode (sone, INPUT);
    pinMode (stwo, INPUT);
    pinMode (sthree, INPUT);
    pinMode (sfour, INPUT);
}

void loop(){
    offline();
}

void offline(){
    relay1();
    relay2();
    relay3();
    relay4();
}

void relay1(){
    val1 = digitalRead(sone); // read input value and store it
    // check whether the input is HIGH (button pressed)
    if (val1 == HIGH) {
        digitalWrite(reone, HIGH); // turn relay ON
    } else {
        digitalWrite(reone, LOW);
    }
}
void relay2(){
    val2 = digitalRead(stwo); // read input value and store it
    // check whether the input is HIGH (button pressed)
    if (val2 == HIGH) {
        digitalWrite(retwo, HIGH); // turn relay ON
    } else {
        digitalWrite(retwo, LOW);
    }
}
void relay3(){
    val3 = digitalRead(sthree); // read input value and store it
    // check whether the input is HIGH (button pressed)
    if (val3 == HIGH) {
        digitalWrite(rethree, HIGH); // turn relay ON
    } else {
        digitalWrite(rethree, LOW);
    }
}
void relay4(){
    val4 = digitalRead(sfour); // read input value and store it
    // check whether the input is HIGH (button pressed)
    if (val4 == HIGH) {
        digitalWrite(refour, HIGH); // turn relay ON
    } else {
        digitalWrite(refour, LOW);
    }
}


I suspect your problem is due to switch bounce. Do you have external pull-down (or pull-up) resistors on the switch pins. if not the easiest thing is probably to use pinMode(pin, INPUT-PULLUP); to activate the internal pull-up resistors. Then wire your switches so that they pull the pin to GND when closed. Also note that that will mean LOW signifies the switch is closed.

It would also be a good idea to have ashort interval between successive readings of the switches. Have a look at the demo Several Things at a Time


If you want the relay to stay in position until the switch is pressed a second time then you need different code. Have a look at the State Change example that comes with the Arduino IDE.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up