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
}
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 ?
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.
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
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
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.