Automatic fishtank water changer

Never posted online before and have only recently got into arduino so have only understood it at a basic level, but here we go, i am having some trubble with a basic programme to automatically change water for my fishtank, i have one input (a float switch which measures whether the water level is at the correct height) and two outputs (for two solenoidal valves, one to remove waste water and the other for clean replacement water).
In my programming i want a delay for 3 days before the waste valve opens to remove some water for a about 30 seconds.
then check whether water level has dropped with the float switch and open clean water valve until normal water level is reached.
to test i have been using LEDs to indicate when relays to the valves will be switched on or off.
I feel like this is an incredibly basic programme but for some reason its not been working. I’m not sure what i am doing wrong.

the most recent problems has been that its not recognising that there is an if statement before the else and so theres an error. (as seen in the screenshot below)
has anyone noticed my mistake

Add your code so far, please. My bad... the picture is a screenshot of your code... please copy and paste it using code tags so I don't have to type it out to modify...

here is the code

automatic_water_changer_2.ino (800 Bytes)

if (buttonState = HIGH);

= is the assignment command
== is the comparison command

The semicolon at the end of the if statement should not be there as it terminates the code to be executed if the test returns true

Also, do you have an external pull down on the float sensor?

in response to both of you..
UKHeliBob, that has helped with the error,

Perehama, i currently dont.

whats happening now is that after the waste water disposal period the clean water turns on and stays on.

sorry for slow replies, im limited to on message every 5 minutes apparantly

Zalig25:
Perehama, i currently dont.

Do you understand the necessity of pulling the input pin in the opposite direction that your sensor pulls the pin?

no i dont,
just learnt basics from the examples and google.
What is a pull up/down

Zalig25:
no i dont,
just learnt basics from the examples and google.
What is a pull up/down

The water mechanically operates the sensor, but the sensor, typically, opens or closes a single pair of contacts, tied to 5 volts, I assume. If it is open, that is to say there is enough water, the input is not pulled LOW without attaching a 10K resistor to ground. That resistor is like the spring or gravity in the float switch pulling against the water boyancy, but on the electrical side. Alternatively, there are “Double-Throw” or “Form C” sensors that close a contact that is common between two other contacts with one of them when the sensor is not active (normally closed) and open that circuit and close the common to the second contact when the sensor is active (normally open). In this case, you tie one to high and one to low and a pull-up resistor is not needed.

ok i get that.
but how would it differ from just directly attaching the switch to the input?

Zalig25:
ok i get that.
but how would it differ from just directly attaching the switch to the input?

Why don't you draw up what you have and I can confirm that will work or modify also. Yes, you need to attach the switch to the input, but if it is a single pole, single throw switch, you can only pull it high, never low, or only low, never high, so you must attach a resistor to the pin also to pull in the opposite direction.

not much has changed with the code,
do you want me to draw up the actual schematic?

automatic_water_changer_2.ino (805 Bytes)

Zalig25:
not much has changed with the code,
do you want me to draw up the actual schematic?

It doesn't have to be EagleCAD or anything official, but it would help to ask and answer questions that exist beyond your code. You can use easyeda.com if you have no other means.

i think me just drawing up a schematic will be easier instead of learning how to use that.

basic diagram.pdf (468 KB)

‘ALL’ DC inductive devices must have kickback diode protection.

Do not power a relay from an I/O pin directly.

Edit
Example:

Zalig25:
i think me just drawing up a schematic will be easier instead of learning how to use that.

You drew a pull down, so looks like your circuit checks out. I've modified your code.

const byte FloatSwitch = 2;
const byte WasteValve = 12;
const byte CleanValve = 8;
const unsigned long CleaningInterval = 259200000UL;// 3 days in milliseconds.
const unsigned long FlushInterval = 3000UL;// 3 seconds in milliseconds.
bool runCycle = true;// runs upon power up, then every CleaningInterval milliseconds.
bool flushingFlag = false;// true while waiting FlushInterval.
unsigned long cleaningTimestamp;
unsigned long flushTimestamp;

void setup() {
  pinMode(FloatSwitch , INPUT); // if(digitalRead(FloatSwitch) == LOW) water is full
  pinMode(WasteValve , OUTPUT);
  digitalWrite(WasteValve, LOW);// closed while LOW
  pinMode(CleanValve , OUTPUT);
  digitalWrite(CleanValve, LOW);// closed while LOW
}

void loop() {
  if (runCycle) {
    flushAndFill();
  }
  else if (millis() - cleaningTimestamp >= CleaningInterval) runCycle = true;
}

void flushAndFill() {
  if (flushingFlag) {
    if (millis() - flushTimestamp >= FlushInterval) {
      digitalWrite(WasteValve, LOW);// close valve
      if (digitalRead(FloatSwitch) == LOW) {
        digitalWrite(CleanValve, LOW);
        flushingFlag = runCycle = false;
        cleaningTimestamp = millis();
      }
      else digitalWrite(CleanValve, HIGH);
    }
  }
  else {
    digitalWrite(WasteValve, HIGH);// open valve
    flushTimestamp = millis();
    flushingFlag = true;
  }
}

larryd, i understand where you're getting at, i dont want to draw too much current from the pins. I think i heard somewhere that the max current i should draw through a pin is about 500mA but im not sure if thats true.

Perehama, i will try this out and tinker with it and the breadboard test, although i don't understand some of it as theres code i havent yet learnt about yet.

Thanks everyone for your help, i will inform you if the tests and final project works.

“max current i should draw through a pin is about 500mA”

Only 20mA.

wow thats a big difference :o

Zalig25:
Perehama, i will try this out and tinker with it and the breadboard test, although i don't understand some of it as theres code i havent yet learnt about yet.

If it doesn't run as is for you, don't discard it. Ask questions about the parts you don't understand. The only thing I've done is:

  1. use millis() to "blink without delay()" which should probably be one of your first lessons...
  2. break the code from the main body into a function. This should also be one of your first lessons....