Help with if statements

can someone help me figure out how to get all of these if statements to work. the problem im having is if i have all three none of them work but if i just have one if statement then it works fine? :frowning:

void loop() {
  if (digitalRead(fanrelay) != LOW) {
  delay(1);
  Serial.println ("fan relay on");
  fancounter = fancounter + 1;
  Serial.println(fancounter); {
  while (digitalRead(fanrelay) != LOW) 
  delay(1);
  }
  }
  if (digitalRead(pumprelay) != LOW) {
  delay(1);
  Serial.println ("The pumps on");
  pumpcounter = pumpcounter + 1;
  Serial.println(pumpcounter); {
  while (digitalRead(pumprelay) != LOW) 
  delay(1);
  }
}
  if (digitalRead(drainrelay) != LOW) {
  delay(1);
  Serial.println ("The drain is open");
  draincounter = draincounter + 1;
  Serial.println(draincounter); {
  while (digitalRead(drainrelay) != LOW) 
  delay(1);
  }
}

Explain what the program is trying to do.

I don’t like using WHILE when its loop will take more than about 1 millisec to complete. But I have no idea what your physical project is.

I presume this is not the same project as in some of your other recent Threads.

…R

yes this is the same project. and i had to use the while statement because the counter wouldn't stop at 1 count. im trying to make it count how many times each relay turns on. eventually i would like to be able to convert ms on time into liters per minute. but for now i would be happy if i could just get this to work. thanks

will someone show me how to fix that code. it compiles but does not work. however it works if i just user 1 if statement instead of 3. is it the brackets????

If you explained what the code is supposed to do, posted a complete program that demonstrates the problem and explained how the inputs are wired more help could be provided.

  if (digitalRead(fanrelay) != LOW)
  {
    delay(1);
    Serial.println ("fan relay on");
    fancounter = fancounter + 1;
    Serial.println(fancounter);
    {
      while (digitalRead(fanrelay) != LOW)
        delay(1);
    }
  }

The code checks to see whether fanrelay (whatever that is) is HIGH. Then in a while loop it checks whether fanrelay is still HIGH. Nothing in the code you posted changes the state of fanrelay so why would the while loop ever exit ?

heres the code. the part i posted earlier is simply a button counter. it detects fanrelay,pump,etc pin status to see if its high or low. if its low then its on so add a counter and print serial.. if i just you 1 if statement it works fine. its not until i add the other 2 then it quits working. i dont know how to chain these statements together. maybe show me how would do it.

//READ ONLY BUTTON CONFIGURATION
const int pumprelay = A1; 
const int drainbutton = A1; 
const int fanrelay = A0; 
const int drainrelay = A4;

//INITAL PINSTATES
int draincounter = 0;
int pumpcounter = 0;
int fancounter = 0;
int drainrelayState = HIGH;  
int fanrelayState = HIGH;  
int pumprelayState = HIGH;  
int drainbuttonState = HIGH;

//FAN ON/OFF CYCLE
unsigned long currentMillis = millis();
unsigned long previousMillis = 0; //
unsigned long interval = 0; // 
unsigned long onInterval = 138000UL; //  FAN ON TIME 2.3 MINUTES
unsigned long offInterval = 1800000UL; // FAN OFF TIME 30 MINUTES

//PUMP ON/OFF CYCLE
unsigned long interval2 = 0;
unsigned long pumppreviousMillis = 0;
unsigned long pumponInterval = 36000UL; // spray on time 26 SECONDS
unsigned long pumpoffInterval = 6480000UL; // sprayer off time 1.8 HRS

//DRAIN RELAY ON TIME
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 2000; //DRAIN OPEN TIME
int previousdrainbuttonState = HIGH;
int currentdrainbuttonState = HIGH;


void setup() {
  interval = offInterval; 
  pinMode (drainbutton, INPUT_PULLUP); //DRAIN ACTIVATION PIN
  pinMode (fanrelay, OUTPUT); //FAN POWER RELAY
  pinMode (pumprelay, OUTPUT); //WATER PRESSURE PUMP
  pinMode (drainrelay, OUTPUT); //DRAIN SOLENOID POWER RELAY
  digitalWrite(pumprelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(fanrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainbutton, HIGH); //SET INITAL BUTTON STATE
  Serial.begin(9600);
  Serial.print("Ready");
}

void loop() {
  if (digitalRead(fanrelay) != LOW) {
  delay(1);
  Serial.println ("Fan relay on");
  fancounter = fancounter + 1;
  Serial.println(fancounter); {
  while (digitalRead(fanrelay) != LOW) 
  delay(1);
  }
  }
 if (digitalRead(pumprelay) != LOW) {
  delay(1);
  Serial.println ("The pumps on");
  pumpcounter = pumpcounter + 1;
  Serial.println(pumpcounter); {
  while (digitalRead(pumprelay) != LOW) 
  delay(1);
  }
}
  if (digitalRead(drainrelay) != LOW) {
  delay(1);
  Serial.println ("The drain is open");
  draincounter = draincounter + 1;
  Serial.println(draincounter); {
  while (digitalRead(drainrelay) != LOW) 
  delay(1);
  }
}

//FAN CYCLE
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (fanrelayState == HIGH) { //IF ITS OFF
    fanrelayState = LOW; // THEN TURN ON
    interval = onInterval; // ASK HOW LONG TO STAY ON
     }
    else {  
      fanrelayState = HIGH; // OTHERWISE OFF
      interval = offInterval; //ASK HOW LONG TO STAY OFF
    } 
    digitalWrite(fanrelay, fanrelayState); //UPDATE STATUS OF FAN 
 }
 
//PUMP CYCLE
  if(currentMillis - pumppreviousMillis > interval2) {
    pumppreviousMillis = currentMillis;   
    if (pumprelayState == HIGH) { //IF ITS OFF
      pumprelayState = LOW; // TURN IT ON
     
     interval2 = pumponInterval; //ASK HOW LONG TO TURN IT ON
    } 
    
    else {  
      pumprelayState = HIGH; //OTHERWISE TURN IT OFF
      interval2 = pumpoffInterval; //ASK HOW LONG TO TURN IT OFF
    }  
    digitalWrite(pumprelay, pumprelayState); //DEBOUNCE
    delay(50);
 }

 //DRAIN CYCLE
 currentTime = millis();
  previousdrainbuttonState = currentdrainbuttonState;
  currentdrainbuttonState = digitalRead(pumprelay); //ASK STATE OF DRAIN BUTTON
  
  if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
  {
    startTime = millis();
    digitalWrite(drainrelay, LOW);//TURN IT ON
    Serial.print("Drain is open!");
  }
  
  if (currentTime - startTime >= period) //ASK HOW LONG TO STAY ON
  {
    digitalWrite(drainrelay, HIGH); //DEBOUNCE
    delay(50);
  }
 }

okay total noob moment. != not equal to...

now i know what you were saying. okay that got me somewhere. but how can i prevent my counter from holding this while i'm holding the button? i would like it to just count only 1 time during button press??

Fan relay on
1
Fan relay on
2
Fan relay on
3
Fan relay on
4
Fan relay on
5
Fan relay on
6
Fan relay on
7
Fan relay on
8
Fan relay on
9
Fan relay on
10
Fan relay on
11
Fan relay on
12
Fan relay on
13
Fan relay on
14
Fan relay on
15
Fan relay on
16
Fan relay on
17
Fan relay on
18
Fan relay on
19
Fan relay on
20
Fan relay on
21
Fan relay on
22
Fan relay on
23
Fan relay on
24
Fan relay on
25

:slight_smile: :slight_smile:

I guess the state change detection example that comes with the IDE is what can get you on the way.

no it dont help me. my problem is the counter keeps counting up really fast. can someone atleast tell me how to fix this fix this ive been sitting here in front of this computer looking at this website for hours and im not really getting anywhere.

  if (digitalRead(fanrelay) != LOW)  //if fanrelay IS HIGH should be if fanrelay HAS BECOME HIGH
  {
    delay(1);
    Serial.println ("Fan relay on");
    fancounter = fancounter + 1;
    Serial.println(fancounter);
    {
      while (digitalRead(fanrelay) != LOW)
        delay(1);
    }
  }

See my comments above
StateChangeDetection is exactly what you need.

What are you trying to do? As far as I can see, your original program controls the fan relay, turning it on & off corresponding to your interval setting. What you've added, particularly the while loop, prevents that code (or any other) ever being called once the fan is off.

how can i make this code work. ive turned it into a mess. im having i need this thing working right now and i have no clue what to do

I suspect that it would relatively easy to help you except for one thing: I have no idea what you're trying to achieve. What is this button you're referring to? How does it relate to the fan relay?

drain button is just another input on the arduino. i have a manual button that pulls fan or pump relays low to force them on without the arduino. but in the event that either me or the arduino turns on the pump relay that forces drainbutton low. i need the drain relay to turn on right after the pump shuts off to drain the pressure in the line right away. i didnt even realize that the whole loop is cutoff and i cannot even identify the problem. heres the code i need to work maybe you can understand it better now if you have any more questions please ask thankyou.

pumprelay is on timer with millis
fanrelay is on time with millis
drainrelay is to turn on for 1 second as soon as pump relay changes from low to high.

//READ ONLY BUTTON CONFIGURATION
const int pumprelay = A1; 
const int drainbutton = A1; 
const int fanrelay = A0; 
const int drainrelay = A4;

//INITAL PINSTATES
int fanlastState = HIGH;
int draincounter = 0;
int pumpcounter = 0;
int fancounter = 0;
int drainrelayState = HIGH;  
int fanrelayState = HIGH;  
int pumprelayState = HIGH;  
int drainbuttonState = HIGH;

//FAN ON/OFF CYCLE
unsigned long currentMillis = millis();
unsigned long previousMillis = 0; //
unsigned long interval = 0; // 
unsigned long onInterval = 138000UL; //  FAN ON TIME 2.3 MINUTES
unsigned long offInterval = 1800000UL; // FAN OFF TIME 30 MINUTES

//PUMP ON/OFF CYCLE
unsigned long interval2 = 0;
unsigned long pumppreviousMillis = 0;
unsigned long pumponInterval = 36000UL; // spray on time 26 SECONDS
unsigned long pumpoffInterval = 6480000UL; // sprayer off time 1.8 HRS

//DRAIN RELAY ON TIME
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 2000; //DRAIN OPEN TIME
int previousdrainbuttonState = HIGH;
int currentdrainbuttonState = HIGH;


void setup() {
  interval = offInterval; 
  pinMode (drainbutton, INPUT_PULLUP); //DRAIN ACTIVATION PIN
  pinMode (fanrelay, OUTPUT); //FAN POWER RELAY
  pinMode (pumprelay, OUTPUT); //WATER PRESSURE PUMP
  pinMode (drainrelay, OUTPUT); //DRAIN SOLENOID POWER RELAY
  digitalWrite(pumprelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(fanrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainbutton, HIGH); //SET INITAL BUTTON STATE
  Serial.begin(9600);
  Serial.print("Ready");
}

void loop() {

//FAN CYCLE
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (fanrelayState == HIGH) { //IF ITS OFF
    fanrelayState = LOW; // THEN TURN ON
    interval = onInterval; // ASK HOW LONG TO STAY ON
     }
    else {  
      fanrelayState = HIGH; // OTHERWISE OFF
      interval = offInterval; //ASK HOW LONG TO STAY OFF
    } 
    digitalWrite(fanrelay, fanrelayState); //UPDATE STATUS OF FAN 
 }
 
//PUMP CYCLE
  if(currentMillis - pumppreviousMillis > interval2) {
    pumppreviousMillis = currentMillis;   
    if (pumprelayState == HIGH) { //IF ITS OFF
      pumprelayState = LOW; // TURN IT ON
     
     interval2 = pumponInterval; //ASK HOW LONG TO TURN IT ON
    } 
    
    else {  
      pumprelayState = HIGH; //OTHERWISE TURN IT OFF
      interval2 = pumpoffInterval; //ASK HOW LONG TO TURN IT OFF
    }  
    digitalWrite(pumprelay, pumprelayState); //DEBOUNCE
    delay(50);
 }

 //DRAIN CYCLE
 currentTime = millis();
  previousdrainbuttonState = currentdrainbuttonState;
  currentdrainbuttonState = digitalRead(pumprelay); //ASK STATE OF DRAIN BUTTON
  
  if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
  {
    startTime = millis();
    digitalWrite(drainrelay, LOW);//TURN IT ON
  }
  
  if (currentTime - startTime >= period) //ASK HOW LONG TO STAY ON
  {
    digitalWrite(drainrelay, HIGH); //DEBOUNCE
    delay(50);
  }
  }

Is this the problem?

  currentdrainbuttonState = digitalRead(pumprelay); //ASK STATE OF DRAIN BUTTON

Your comment says you're reading the button state, but it's actually looking at the pumpRelay.

yea i have the drain button tied to the pump relay. it follow the state of pump relay. when it detect that pump relay change from high to low it activates the drainrelay. can you check the part of this code PUMP CYCLE in the loop. so far i have the tiemr working for the drain relay and the fan relay. but when i turn on the restart the arduino it cycles the pump relay. i have pullup resistors in place so im thinking it has to be something to do with my code. after the pump cycles on startup everything works. somehow i need to stop this pump from starting when i plug it in for the first time. any ideas??

//READ ONLY BUTTON CONFIGURATION
const int pumprelay = A1;
const int drainbutton = A1;
const int fanrelay = A0;
const int drainrelay = A4;

//INITAL PINSTATES
int fanlastState = HIGH;
int draincounter = 0;
int pumpcounter = 0;
int fancounter = 0;
int drainrelayState = HIGH;
int fanrelayState = HIGH;
int pumprelayState = HIGH;
int drainbuttonState = HIGH;

//FAN ON/OFF CYCLE

unsigned long previousMillis = 0; //
unsigned long interval = 0; //
unsigned long onInterval = 138000UL; //  FAN ON TIME 2.3 MINUTES
unsigned long offInterval = 1800000UL; // FAN OFF TIME 30 MINUTES

//PUMP ON/OFF CYCLE
unsigned long interval2 = 0;
unsigned long pumppreviousMillis = 0;
unsigned long pumponInterval = 36000UL; // spray on time 26 SECONDS
unsigned long pumpoffInterval = 6480000UL; // sprayer off time 1.8 HRS

//DRAIN RELAY ON TIME
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 2000; //DRAIN OPEN TIME
int previousdrainbuttonState = HIGH;
int currentdrainbuttonState = HIGH;


void setup() {
  interval = offInterval;
  pinMode (drainbutton, INPUT_PULLUP); //DRAIN ACTIVATION PIN
  pinMode (fanrelay, OUTPUT); //FAN POWER RELAY
  pinMode (pumprelay, OUTPUT); //WATER PRESSURE PUMP
  pinMode (drainrelay, OUTPUT); //DRAIN SOLENOID POWER RELAY
  digitalWrite(pumprelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(fanrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainrelay, HIGH); //SET INITAL RELAY STATE
  digitalWrite(drainbutton, HIGH); //SET INITAL BUTTON STATE
  Serial.begin(9600);
  Serial.print("Ready");
}

void loop() {
  unsigned long currentMillis = millis();

  //FAN CYCLE
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (fanrelayState == HIGH) { //IF ITS OFF
      fanrelayState = LOW; // THEN TURN ON
      interval = onInterval; // ASK HOW LONG TO STAY ON
    }
    else {
      fanrelayState = HIGH; // OTHERWISE OFF
      interval = offInterval; //ASK HOW LONG TO STAY OFF
    }
    digitalWrite(fanrelay, fanrelayState); //UPDATE STATUS OF FAN
  }
//PUMP CYCLE
  
   if (currentMillis - previousMillis > interval2) {
    previousMillis = currentMillis;
    if (pumprelayState == HIGH) { //IF ITS OFF
      pumprelayState = LOW; // TURN IT ON

      interval2 = pumponInterval; //ASK HOW LONG TO TURN IT ON
    }

    else {
      pumprelayState = HIGH; //OTHERWISE TURN IT OFF
      interval2 = pumpoffInterval; //ASK HOW LONG TO TURN IT OFF
    }
    digitalWrite(pumprelay, pumprelayState); //DEBOUNCE
    delay(50);
  }
  
  //DRAIN CYCLE
  currentTime = millis();
  previousdrainbuttonState = currentdrainbuttonState;
  currentdrainbuttonState = digitalRead(pumprelay); //ASK STATE OF DRAIN BUTTON

  if (currentdrainbuttonState != previousdrainbuttonState && currentdrainbuttonState == HIGH)//ASK WHATS THE STATE OF THE BUTTON
  {
    startTime = millis();
    digitalWrite(drainrelay, LOW);//TURN IT ON
  }

  if (currentTime - startTime >= period) //ASK HOW LONG TO STAY ON
  {
    digitalWrite(drainrelay, HIGH); //DEBOUNCE
    delay(50);
  }


}
//PUMP CYCLE
  
   if (currentMillis - previousMillis > interval2) {
    previousMillis = currentMillis;
    if (pumprelayState == HIGH) { //IF ITS OFF
      pumprelayState = LOW; // TURN IT ON

      interval2 = pumponInterval; //ASK HOW LONG TO TURN IT ON
    }

    else {
      pumprelayState = HIGH; //OTHERWISE TURN IT OFF
      interval2 = pumpoffInterval; //ASK HOW LONG TO TURN IT OFF
    }
    digitalWrite(pumprelay, pumprelayState); //DEBOUNCE
    delay(50);
  }

does pin A1 have any relation to rx tx on the mega256. i notice when i restart my arduino the problem happens at the exact same time the rx light flashes?

I don't think your problem is the pump code. I put a serial print in that section and it never goes there at startup, which is as expected because of offInterval being so large.

Your issue appears to be hardware as Grumpy_Mike said. Your pullup, for whatever reason, is not doing the job.

okay so i added this to the void setup() " interval2 = pumpoffInterval;"
and that took care of it? i wish i knew exactly why.?!? now i guess its time to get my counter to work. i know each relay has its own interval. the fan is interval and the pump is interval2