if statement problem

in this code a led(bomba) is supposed to turn on or off in some cases but in one of these cases anything is supposed to happen. In other words, if the led is on before this case happens, it will keep on or if it was off it will keep off, and i thought "okay i will not write anything in this sentence and anything will happen" but when i tested it, when this case happens the led turn off.

can you please help me with it ?

the complete code:

#define bomba 9
int rel_max = 13;
int rel_min  = 12;
int rap_min = 11;

void setup() {
  pinMode(bomba, OUTPUT);
  pinMode(4, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);

}

void loop() {
  
  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == LOW && digitalRead(rel_max) == LOW)
  {
    digitalWrite(bomba, LOW);
  }
  
  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == LOW && digitalRead(rel_max) == HIGH)
  {
    digitalWrite(bomba, LOW); //se este caso acontecer um defeito ocorreu nos sensores
  }

  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == LOW)
  {
    digitalWrite(bomba, LOW);
  }
  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == HIGH)
  {
    digitalWrite(bomba, LOW);
  }
  
  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == LOW && digitalRead(rel_max) == LOW)
  {
    digitalWrite(bomba, HIGH);
  }
  
  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == LOW && digitalRead(rel_max) == HIGH)
  {
    digitalWrite(bomba, LOW);
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == LOW)
  {
  //anything is supposed to happen
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == HIGH)
  {
    digitalWrite(bomba, LOW);
  }
}

the part of the code where the problem is happening

if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == LOW)
  {
  //anything is supposed to happen
  }

How are the inputs wired ?

You have set the pinMode() of 3 pins to INPUT_PULLUP but then you never read the pins. Why ?

UKHeliBob:
How are the inputs wired ?

You have set the pinMode() of 3 pins to INPUT_PULLUP but then you never read the pins. Why ?

ohh, forgot to talk about that, it was supposed to be like that

  pinMode(bomba, OUTPUT);
  pinMode(rel_min, INPUT_PULLUP);
  pinMode(rel_max, INPUT_PULLUP);
  pinMode(rap_min, INPUT_PULLUP);

but i don't know why, when i put the code like that it doesn't work

well, i did this code to control a led(bomba) and i used "if" for it, but when i tested, the led turn on in all the "if" cases, witch was not supposed to happen. Did i write the "if" senteces wrong ?

#define rap 13
#define rel 12
#define bomba 9

void setup () {
  pinMode(rap, INPUT_PULLUP);
  pinMode(rel, INPUT_PULLUP);
  pinMode(bomba, OUTPUT);
}

void loop () {
if(digitalRead(rap)==LOW && digitalRead(rel)==LOW){
  digitalWrite(bomba,LOW);
  }

if(digitalRead(rap)==LOW && digitalRead(rel)==HIGH) {
  digitalWrite(bomba,LOW);
  }

if(digitalRead(rap)==HIGH && digitalRead(rel)==LOW) {
  digitalWrite(bomba,LOW);
  }

if(digitalRead(rap)==HIGH && digitalRead(rel)==HIGH) {
  digitalWrite(bomba,HIGH);
  }
}

First, verify your switch wiring. Does each button when tested alone with digitalRead() give a HIGH when not pressed, and a LOW when pressed. This is the expected state of affairs with INPUT_PULLUP mode.

If the switches are not wired properly, you can not get the logic correctly.

Why don't you add some Serial.print() lines to display the value of those pins (HIGH/LOW) so you can see if they are actually changing when you press them or if they are wired up wrong.

I think it would be a good idea to read all of the pins and store in variables at the top of loop and use those in all the if statements. As you have it written now you’ve left yourself open to the possibility that the state of one of those pins changes between one of statement and the next making it appear as though one ran out of turn.

cattledog:
First, verify your switch wiring. Does each button when tested alone with digitalRead() give a HIGH when not pressed, and a LOW when pressed. This is the expected state of affairs with INPUT_PULLUP mode.

If the switches are not wired properly, you can not get the logic correctly.

i tested and when it is not pressed gives 0 and when it's pressed it gives 1, so should i use INPUT instead of INPUT_PULLUP ?

i tested and when it is not pressed gives 0 and when it's pressed it gives 1, so should i use INPUT instead of INPUT_PULLUP ?

No. INPUT_PULLUP is usually the best way to wire and read the switches.

Your wiring does not seem correct.

What kind of switches are you using, and can you provide a simple hand drawn wiring diagram?

If you are using the square, 4 pin, momentary buttons, then they should be wired diagonally across the button with one wire to ground, and the other wire to the input pin.

Delta_G:
I think it would be a good idea to read all of the pins and store in variables at the top of loop and use those in all the if statements. As you have it written now you’ve left yourself open to the possibility that the state of one of those pins changes between one of statement and the next making it appear as though one ran out of turn.

like that ??

#define bomba 9 // será LED_BUILTIN somente no teste 
int rel_max = 13;
int rel_min  = 12;
int rap_min = 11;

//estados dos reservatorios
#define a 0
#define b 1
#define c 2
#define d 3
#define e 4
#define f 5
#define g 6
#define h 7
#define medio 8

unsigned char novo(unsigned char novo_estado);
boolean transicao = true;
unsigned int reservatorios = novo(a);


unsigned char houve_transicao() {
  if (transicao) {
    transicao = false;
    return true;
  }
  return false;
}

unsigned char novo(unsigned char novo_estado) {
  transicao = true;
  return novo_estado;
}

void setup() {
  pinMode(bomba, OUTPUT);
  pinMode(4, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == LOW && digitalRead(rel_max) == LOW)
  {
    reservatorios = novo(a);
  }

  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == LOW && digitalRead(rel_max) == HIGH)
  {
    reservatorios = novo(b);
  }

  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == LOW)
  {
    reservatorios = novo(c);
  }
  if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == HIGH)
  {
    reservatorios = novo(d);
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == LOW && digitalRead(rel_max) == LOW)
  {
    reservatorios = novo(e);
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == LOW && digitalRead(rel_max) == HIGH)
  {
    reservatorios = novo(f);
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == LOW)
  {
    reservatorios = novo(g);
        Serial.println(digitalRead(rap_min));
  }

  if (digitalRead(rap_min) == HIGH && digitalRead(rel_min) == HIGH && digitalRead(rel_max) == HIGH)
  {
    reservatorios = novo(h);
  }
  switch (reservatorios) {
    case a:
      digitalWrite(bomba, LOW);
      break;

    case b:
      digitalWrite(bomba, LOW);
      break;

    case c:
      digitalWrite(bomba, LOW);
      break;

    case d:
      digitalWrite(bomba, LOW);
      break;

    case e:
      digitalWrite(bomba, HIGH);
      break;

    case f:
      digitalWrite(bomba, LOW);
      break;

    case g:
      //anything happens
      break;

    case h:
      digitalWrite(bomba, LOW);
      break;
  }
}

So the switches connect to Vcc when pressed, so you should use INPUT. If you get a stable 0 when they are not pressed, that implies that you must have an external pulldown resistor on the switch pins already? A pin connected to an external button needs a pullup (if the button connects to ground when pressed) or pulldown (if it connects to Vcc when pressed), otherwise the pin is floating (connected to nothing other than the input pin), and will give random readings as it picks up noise from the environment. (Since the arduino has internal pullups, most people wire switches to connect to ground when pressed so they don't need external pulldown resistors)

cattledog:
No. INPUT_PULLUP is usually the best way to wire and read the switches.

Your wiring does not seem correct.

What kind of switches are you using, and can you provide a simple hand drawn wiring diagram?

If you are using the square, 4 pin, momentary buttons, then they should be wired diagonally across the button with one wire to ground, and the other wire to the input pin.

Imgur

blh64:
Why don't you add some Serial.print() lines to display the value of those pins (HIGH/LOW) so you can see if they are actually changing when you press them or if they are wired up wrong.

well, i tried it and in all the cases the value is 1

I do not see the external pull downs that Dr Azzy refers to.

You can add them, or you can change the wiring to work with INPUT_PULLUP. Remove the 5V connection and replace it with a connection to ground.

If you use INPUT_PULLUP the switches will read HIGH when not pressed, and LOW when pressed, so the logic of your sketch will need to be adjusted.

cattledog:
I do not see the external pull downs that Dr Azzy refers to.

You can add them, or you can change the wiring to work with INPUT_PULLUP. Remove the 5V connection and replace it with a connection to ground.

If you use INPUT_PULLUP the switches will read HIGH when not pressed, and LOW when pressed, so the logic of your sketch will need to be adjusted.

how can i add external pull downs

how can i add external pull downs

Add a 10Kohm resistor between the input pin side of the switch and ground.

https://playground.arduino.cc/CommonTopics/PullUpDownResistor/

cattledog:
Add a 10Kohm resistor between the input pin side of the switch and ground.

Arduino Playground - PullUpDownResistor

thanks, i did it and looks good

but my main problem still persists :

in this code a led(bomba) is supposed to turn on or off in some cases but in one of these cases anything is supposed to happen. In other words, if the led is on before this case happens, it will keep on or if it was off it will keep off, and i thought "okay i will not write anything in this sentence and anything will happen" but when i tested it, when this case happens the led turn off

if (digitalRead(rap_min) == LOW && digitalRead(rel_min) == LOW && digitalRead(rel_max) == LOW)
  {
    digitalWrite(bomba, LOW);
  }

Are your switches toggle switches with fixed on and off positions, or are they momentary push button type switches?

If momentary switches, you are likely to enter the LOW/LOW/LOW situation between presses and the led will be OFF before any of your "do nothing" combinations.

Well i would start with changing

if(digitalRead(rap)==LOW && digitalRead(rel)==LOW){
  digitalWrite(bomba,LOW);
  }

to

if(digitalRead(rap)==LOW || digitalRead(rel)==LOW){
  digitalWrite(bomba,LOW);
  }

and remove the redundant other 2 if statements that turn the led low. And did you actually put a Serial.print here ?

if(digitalRead(rap)==LOW || digitalRead(rel)==LOW){
  digitalWrite(bomba,LOW);
  Serial.println("LOW !");  // here 
  }

gabrieldamakampos:
well, i tried it and in all the cases the value is 1

If the code is reading a 1 when the button is pressed and also when it is not pressed then you have a wiring issue.