Can someone look over this code

this is some code i wrote but im having a strange issue with the pcf8574, it appears to randomly set some output states high. i thought maybe this was interference but now i think maybe something is wrong with my code. i have tried to find any bugs in this timer sequence but i failed.

the sequence starts out waiting for float sensors to become high state indicating empty tank. then the waterSolenoid and stirringPump are turned out until both float sensors are low indicating full tank. then pumps 1 - 5 run individually for their timers. then timerstate should become 0 and wait for the next cycle ,

What happened there?

sorry it would make sense to post it all.

functions.h,

void(* resetFunc) (void) = 0;

void drawLcd() {
  // if (millis() - lcdMillis >= 1000) {
  if (!menu) {
    lcd.clear();
    if (timerState != 0) {
      lcd.print("Working");
      lcd.setCursor(0, 1);
      lcd.print("WaterPump ON");
      if (tank1WaterSolenoidstate == true) {
        lcd.setCursor(0, 2);
        lcd.print("Water Solenoid ON");
      } else {
        lcd.setCursor(0, 2);
        lcd.print("Water Solenoid OFF");
      }
      if (tank1P1state == true) {
        lcd.setCursor(0, 3);
        lcd.print("P1 ON");
      }
      if (tank1P2state == true) {
        lcd.setCursor(0, 3);
        lcd.print("P2 ON");
      }
      if (tank1P3state == true) {
        lcd.setCursor(0, 3);
        lcd.print("P3 ON");
      }
      if (tank1P4state == true) {
        lcd.setCursor(0, 3);
        lcd.print("P4 ON");
      }
      if (tank1P5state == true) {
        lcd.setCursor(0, 3);
        lcd.print("P5 ON");
      }
    } else {
      lcd.print("Idle");
      if (!tank1PCFstate) {
        lcd.setCursor(13, 0);
        lcd.print("!T1");
      }
      if (!tank2PCFstate) {
        lcd.setCursor(17, 0);
        lcd.print("!T2");
      }
      // waterTimerTime = waterTimer
      lcd.setCursor(0, 2);
      lcd.print(waterTimerTime);
      lcd.setCursor(0, 1);
      lcd.print("T1:" );
      lcd.print( _EEPROM.t1Cycles );
      lcd.print(" ");
      lcd.print("T2:");
      lcd.print( _EEPROM.t2Cycles );
     // DateTime now = rtc.now();
      lcd.setCursor(0, 3);
    //  lcd.print(now.hour());
      lcd.print(':');
     // lcd.print(now.minute());
      lcd.print(':');
     // lcd.print(now.second());
      lcd.print("   ");
      lcd.setCursor(11, 3);
     // lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
      lcd.print(",");
    //  lcd.print(now.day());
      lcd.print('/');
    //  lcd.print(now.month());
    }

    lcdMillis = millis();
  }
}

I must say i almost forgot about tank2TimerCode.h, i have not assmblled it yet and am only running tank1. but i forgot its still in the loop!

i thought maybe this could be a code problem. at first my pcf8574 state was randomly changing high, the only way to the watersolenoid to turn off is if its called in the code and thats always followed by settings its state to false. however if shuts off without setting state to false. But incase i overlooked something i thought i could get a opinion of the problem

From what i can tell if i just set all the states low manually, they stay low, but when the timer is running even when there are no commands being sent to the pcf8574 it will go high

I first noticed the problem when the waterSolenoid pin became high before both float sensors became low

Perhaps the hackiest thing I've seen on this forum ... and that's saying a lot.

void(* resetFunc) (void) = 0;

Arduino software reset. Last resort I guess.

It is a means of reseting via software with the buttons. I then setup the watchdog timer incase i2c hangs

i don't understand why your so frustrated. why not have a reset function? if for whatever reason i need to quickly set all the pins of my pcf8574 high and reset the device then i need software reset. why solder wire to hw reset if i dont need to

Because that's not a reset function.

why is it not a reset function?

Because it is simply a call to the reset vector.
If you want a reset, use the watchdog - that's what it is for

1 Like

But is it not a function? None the less, a function that resets the software?

idk im not trying to argue with you im just confused lol

im not saying theres not a better way to do it either.

Does the code in tank1TimerCode.h look okay? is there any reason in the code you see that may cause the tank1SolenoidPin to become high without setting tank1WaterSolenoidstate to false?

i Found 1 of my problems with the stirrer randomly.. shutting off,

  if (tank1StirrerOn) {
    if (!tank1StirState) {
      tank1PCF.write(tank1Stirrer, 0);
      tank1StirState = true;
      tank1StirrerMillis = millis();
    }
    if (millis() - tank1StirrerMillis >= 360000) {
      tank1StirrerOn = false;
      tank1PCF.write(tank1Stirrer, 1);
    }
  }

i have removed that code but i still randomly have a problem with the solenoid pin.
I realize my code needs some improvements.

Sometimes when i come to check on it. the lcd says the waterpump and water solenoid are on but they are off. neither are suppose to shutoff untill the upper float sensor shows low input for 5 seconds to the arduino and that cant happen unless the tanks full. but ill come check on it and it says they are both on and the tank is not full and the solenoid pin state is not low as it should be.

it will sit like that until i improvise. if i manually move the float sensor with my hand it will continue the cycle like normal. but why is the solenoid shutting off before i designed it to? or did i?

btw the water pump is actually wire to the solenoid output so they both come on and off together.

perhaps you can do it this way

void resetSystem() {
  wdt_enable( WDTO_15MS );
  while (1);
}
1 Like

I'm guessing nothing really stands out in the timercode that is causing my waterSolenoid to become in the high state before both of tank1 float sensors report low state for 5 seconds

Thing is, most well-designed programs don't need to do it at all. OP's code is triggering the reset based on a combination of digitalRead() statements. Likely because they painted themselves into a corner with blocking code and need to get out. Better programming would fix that.

I'm so glad all these response are so relevant to the questions I asked. All this helpful information is really solving my problem. I don't know what I would do without your kind attitude.