Push button controlled relay

Hi all,

I'm pretty new at all this stuff and I am trying to make an aquarium controller with an Arduino mega.

I have most of the basic functions working like heating, cooling, light control done and working but I am hitting a roadblock on the feed function I am trying to setup.

Here is the code I have

const int returnpump = 22;
const int powerhead = 24;
const int skimmer = 26;
const int ato = 28;
const int button = 14;
const int floatswitch1 = 12;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long powerheadofftime = 1000;
unsigned long returnpumpofftime = 2000;
unsigned long skimmerofftime = 3000;



void setup() {
  // put your setup code here, to run once:
  pinMode(returnpump, OUTPUT);
  pinMode(powerhead, OUTPUT);
  pinMode(skimmer, OUTPUT);
  pinMode(ato, OUTPUT);
  pinMode(button, INPUT);
  pinMode(floatswitch1, INPUT_PULLUP);
  digitalWrite(returnpump, HIGH);
  digitalWrite(powerhead, HIGH);
  digitalWrite(skimmer, HIGH);
  digitalWrite(ato, HIGH);
}



void loop() {
  // put your main code here, to run repeatedly:

  
   
   
int state = digitalRead(button);
  if (state == HIGH){
    feedmode();
    }

    
  
}

void feedmode(){
   
    currentMillis = millis();
    
    digitalWrite(returnpump, LOW);
    digitalWrite(powerhead, LOW);
    digitalWrite(skimmer, LOW);
    if (currentMillis - previousMillis >= powerheadofftime){
      digitalWrite(powerhead, HIGH);}
    if (currentMillis - previousMillis >= returnpumpofftime){
      digitalWrite(returnpump, HIGH);}  
    if (currentMillis - previousMillis >= skimmerofftime){
      digitalWrite(skimmer, HIGH);}
   previousMillis = currentMillis; 
}

When I push the button the relays come on but as soon as I release them they go off immediately.

If i use delay like this code

#include <LiquidCrystal.h>

#include <Wire.h>

const int returnpump = 22;
const int powerhead = 24;
const int skimmer = 26;
const int ato = 28;
const int button = 14;
const int floatswitch1 = 12;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long powerheadofftime = 1000;
unsigned long returnpumpofftime = 2000;
unsigned long skimmerofftime = 3000;



void setup() {
  // put your setup code here, to run once:
  pinMode(returnpump, OUTPUT);
  pinMode(powerhead, OUTPUT);
  pinMode(skimmer, OUTPUT);
  pinMode(ato, OUTPUT);
  pinMode(button, INPUT);
  pinMode(floatswitch1, INPUT_PULLUP);
  digitalWrite(returnpump, HIGH);
  digitalWrite(powerhead, HIGH);
  digitalWrite(skimmer, HIGH);
  digitalWrite(ato, HIGH);
}



void loop() {
  // put your main code here, to run repeatedly:

  
   
   
int state = digitalRead(button);
  if (state == HIGH){
    feedmode();
    }

    
  
}

void feedmode(){
   
    currentMillis = millis();
    
    digitalWrite(returnpump, LOW);
    digitalWrite(powerhead, LOW);
    digitalWrite(skimmer, LOW);
   delay(1000);
      digitalWrite(powerhead, HIGH);
    delay(1000);
      digitalWrite(returnpump, HIGH);  
    delay(1000);
      digitalWrite(skimmer, HIGH);}

Everything works fine. The relays all come on and then turn off in sequence. The problem with that is that in reality the delays need to be 7, 8 and 10 minutes not a few seconds.

I think it is somehow going to a LOW state before running the whole feedmode function but I am not sure.

Thanks for the help!

unsigned long powerheadofftime = 1000;
unsigned long returnpumpofftime = 2000;
unsigned long skimmerofftime = 3000;

You do know these values are 1,2 and 3 seconds?

Yes. For testing purposes I want the relays to cycle faster so that I can observe them. Once it is working I will replace those values with the proper ones.

You may want to rethink where lines like this should go.

if (currentMillis - previousMillis >= powerheadofftime)

You should look at the 'change in state' example.

.