Hi !
i working on a project but i'm running in to a problem, why not working this setup ?
my problem is : if i use one button and 3 relay at one time its working normaly the coil voltage 4.6
but im using this settings my relay not working properly the relay coil voltage only 2.3 v
not really matter the relay power becaouse if im write four relay active to one button its working.
only this way not working so the problem in the code.
int PUMP1 = 2;
int PUMP2 = 3;
int ALARM = 4;
int SensorButton1 = 9;
int SensorButton2 = 8;
int SensorButton3 = 10;
void setup()
{
pinMode(SensorButton1, INPUT_PULLUP);
pinMode(SensorButton2, INPUT_PULLUP);
pinMode(SensorButton3, INPUT_PULLUP);
pinMode(PUMP1, OUTPUT);
pinMode(PUMP2, OUTPUT);
pinMode(ALARM, OUTPUT);
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
void loop()
{
///////////////////////////////////////// STAGE 1
if (digitalRead(SensorButton1) == LOW)
{
digitalWrite(PUMP1, LOW);
digitalWrite(PUMP2, LOW);
digitalWrite(ALARM, LOW);
}
else
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
///////////////////////////////////////// STAGE 2
if (digitalRead(SensorButton2) == LOW)
{
digitalWrite(PUMP1, LOW);
digitalWrite(PUMP2, LOW);
digitalWrite(ALARM, LOW);
}
else
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
///////////////////////////////////////// STAGE 3
if (digitalRead(SensorButton3) == LOW)
{
digitalWrite(PUMP1, LOW);
digitalWrite(PUMP2, LOW);
digitalWrite(ALARM, LOW);
}
else
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
/////////////////////////////////////////
}
The setup is an arduino mega and a four relay module board
The relay board is an opto switched so in arduino ide is needed to use revert so "on= LOW"
"off = HIGH"
Relay board link : https://potentiallabs.com/cart/image/cache/catalog/Arduino/4-channel%20relay%20Module-800x800.jpg
Have you considered how a reader could even have a clue as to what "not working" means? You have not even given a schematic showing how you wire the set-up or are powering the setup.
But a question needs to be asked as to why you read the button before then second testing, even though an "else" would be appropriate for the logic. A button is off if it is not on, so another read accomplishes nothing. Just writing to change the output pins will not take long enough to allow the pin to change value.
Paul
No matter which button is pressed, the code for the other buttons sets all the outputs high, so all the relays turn off.
Paul_KD7HB:
Have you considered how a reader could even have a clue as to what "not working" means? You have not even given a schematic showing how you wire the set-up or are powering the setup.
But a question needs to be asked as to why you read the button before then second testing, even though an "else" would be appropriate for the logic. A button is off if it is not on, so another read accomplishes nothing. Just writing to change the output pins will not take long enough to allow the pin to change value.
Paul
Sorry for my very minimal writeing im alredy completed.
Im therefore write "HIGH" the output pin becaouse this is an opto relay, working "on " on LOW state
So ony need some delay into the button after ?
evanmars:
No matter which button is pressed, the code for the other buttons sets all the outputs high, so all the relays turn off.
So the second "else" is cause the trouble ?
Im want to switch the relay if the button pressed the relays is on if im not pressed the button the relays is off.
How about get rid of all the elses.
Make another if statement:
if (digitalRead(SensorButton1) && digitalRead(SensorButton2) && digitalRead(SensorButton3) )
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
Edit: deleted extra ( and )
Next question is what happens if 2 or 3 buttons are pressed at the same time.
evanmars:
Next question is what happens if 2 or 3 buttons are pressed at the same time.
hm thanks for youre code but it not working im running into error but im understand what you thinking
So the origintal schematic is : if im push button 1 the relay 1 (PUMP1) is on if im unpressed the button 1 the relay 1 is off
Secound state:
if im push button 1 again the relay 2 (PUMP2) is on if im unpressed the button 1 the relay 2 is off
so this is a state machine if im not wrong.
the third state is :
if im push button 2 the relay 1- 2 (PUMP1 AND PUMP2) but need a delay after pump 1 an if upress button 2 relays is off
a last state:
if im push button 3 the relay 3 (alarm) is on if upress button 3 relay is off
this is want to working
evanmars:
How about get rid of all the elses.
Make another if statement:
if (digitalRead(SensorButton1) && digitalRead(SensorButton2) && digitalRead(SensorButton3) )
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
Edit: deleted extra ( and )
this is working thanks
void loop()
{
if (digitalRead(SensorButton1) && digitalRead(SensorButton2) && digitalRead(SensorButton3))
{
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, HIGH);
digitalWrite(ALARM, HIGH);
}
else
{
digitalWrite(PUMP1, LOW);
digitalWrite(PUMP2, LOW);
digitalWrite(ALARM, LOW);
}
}