Relay Timer

why it does not work what have I done wrong?

int pumppin1 = 9;
int pumppin2 = 8;
int pumppin3 = 5;
int pumppin4 = 4;
int knapp1 = 7;
int knapp2 = 6;
int knapp3 = 11;
int knapp4 = 10;
int knapp5 = 3;
int knapp6 = 2;
void setup(){
pinMode(pumppin1, OUTPUT);
pinMode(pumppin2,OUTPUT);
pinMode(pumppin3, OUTPUT);
pinMode(pumppin4, OUTPUT);
pinMode(knapp1,INPUT);
pinMode(knapp2,INPUT);
pinMode(knapp3,INPUT);
pinMode(knapp4,INPUT);

}

void loop() {
  if(knapp1,HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
  
  
  if(knapp2,HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
delay(10000);  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
  
if(knapp3,HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
if(knapp4,HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
delay(10000);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
  if(knapp5,HIGH);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
  
  if(knapp6,HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
  
delay(10000);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
}

For inputs you want to use

if(digitalRead(INPUT_PIN) == HIGH)

because you are testing it to see if it is equal to something. Take note that it is a double == which will return true or false, if it is true it will continue, if false it will not. A single = would set the INPUT_PIN to something, not what you want.

edited because I realized I had my syntax wrong brought to light by Arrch.

  if(knapp1,HIGH);

Whatever you think this is doing, it isn't.

  1. Comma operator doesn't do a comparison, things like ==, <=, !=, >=, etc., do.
  2. You don't compare pin numbers to HIGH/LOW, you compare pin states to HIGH/LOW. To convert a pin number to its state, you use digitalRead().
  3. You shouldn't have a semicolon at the end of it
  4. Any code that should run under that condition should be surrounded by parentheses.

Ditto for all of the other similarly looking lines.

SVeilleux9:
For inputs you want to use

if(INPUT_PIN == high);

No you don't. "high" isn't a defined variable or constant, and you shouldn't be comparing a pin number to a state.

was this what you meant? or how do I make the simplest timer that may be different relays to be at a certain time of a button?

int pumppin1 = 9;
int pumppin2 = 8;
int pumppin3 = 5;
int pumppin4 = 4;
int INPUT_PIN1 = 7;
int INPUT_PIN2 = 6;
int INPUT_PIN3 = 11;
int INPUT_PIN4 = 10;
int INPUT_PIN5 = 3;
int INPUT_PIN6 = 2;
void setup(){
pinMode(pumppin1, OUTPUT);
pinMode(pumppin2,OUTPUT);
pinMode(pumppin3, OUTPUT);
pinMode(pumppin4, OUTPUT);
pinMode(INPUT_PIN1,INPUT);
pinMode(INPUT_PIN2,INPUT);
pinMode(INPUT_PIN3,INPUT);
pinMode(INPUT_PIN4,INPUT);
pinMode(INPUT_PIN5,INPUT);
pinMode(INPUT_PIN6,INPUT);

}

void loop() {
 if(INPUT_PIN1 == HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
  
  
  if(INPUT_PIN2 == HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
delay(10000);  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
  
if(INPUT_PIN3 == HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
if(INPUT_PIN4 == HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
delay(10000);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  
  if(INPUT_PIN5 == HIGH);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
  
  if(INPUT_PIN6 == HIGH);
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
  
delay(10000);
  
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin4,HIGH);
  
delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin4,LOW);
}

Arrch is correct, sorry for the misunderstanding. I am used to using direct port manipulation and tried to make it to simple.

will build a smådelstvätt for engine parts so then I have 6 kvappar that will control four relays that should be on 30 mins and 1hr on another. if you know what I mean. what should I look for?

tobba__1990:
will build a smådelstvätt for engine parts so then I have 6 kvappar that will control four relays that should be on 30 mins and 1hr on another. if you know what I mean. what should I look for?

\

I don't know what a smådelstvätt or a kvappar are, so I couldn't tell you.

If you want to control 4 relays, you have to be more explicit about how you want them to be controlled. Statement such as "I want relay 1 to be on for 10 minutes, of for 5. Afterwards, I want Relay 2 to be on for 6 minutes, then off for 10. At the same time that Relay 2 is turned on, I want relay 3 to also be turned on for 2 minutes, then off for 6" are explicit and describe exactly how you want the relays to function. They also help structure the code.

parts washing. I have 6 buttons. and 4 relays. button 1, all relays to be switched on for 30 minutes. Number 2 all relays in an hour. button 3. 3 relays on 30 minutes. Button 4. 3relän 1 hour. 5 key 30 min at 2 relays key 6 2 relays 1 hour

and relay 1 will always start 1 minute before the second relay

Your IF statements should look like this

 if(INPUT_PIN1 == HIGH){
  digitalWrite(pumppin1,HIGH);
  delay(5000);
  digitalWrite(pumppin2,HIGH);
  digitalWrite(pumppin3,HIGH);
  digitalWrite(pumppin4,HIGH);
  delay(180000);
  digitalWrite(pumppin1,LOW);
  digitalWrite(pumppin2,LOW);
  digitalWrite(pumppin3,LOW);
  digitalWrite(pumppin4,LOW);
}

The the code that runs when the IF is true is enclosed in {}

Also you may want to consider debouncing switches if they are not hardware debounced.

Also, the use of delays is probably going to cause you problems. When the arduino is delayed, it can not do anything else so it will not look for other button presses. This may be ok for your application, I'm not sure. Look at the example "Blink without delay" for a better method.

Also, it's always good to add a "Stop all button".

tobba__1990:
will build a smådelstvätt for engine parts so then I have 6 kvappar that will control four relays that should be on 30 mins and 1hr on another. if you know what I mean. what should I look for?

You should study the BlinkWithoutDelay sketch sample included in your IDE.

basically you want to 'activate' a timed function with a button press. Something like this:

int pumppin1 = 9;
int pumppin2 = 8;
int pumppin3 = 5;
int pumppin4 = 4;
int INPUT_PIN1 = 7;
int INPUT_PIN2 = 6;
int INPUT_PIN3 = 11;
int INPUT_PIN4 = 10;
int INPUT_PIN5 = 3;
int INPUT_PIN6 = 2;
int pumpTime;
boolean pumpToggle;
unsigned long startTime;
//
void setup()
{
  pinMode(pumppin1, OUTPUT);
  pinMode(pumppin2,OUTPUT);
  pinMode(pumppin3, OUTPUT);
  pinMode(pumppin4, OUTPUT);
  pinMode(INPUT_PIN1,INPUT);
  pinMode(INPUT_PIN2,INPUT);
  pinMode(INPUT_PIN3,INPUT);
  pinMode(INPUT_PIN4,INPUT);
  pinMode(INPUT_PIN5,INPUT);
  pinMode(INPUT_PIN6,INPUT);
}

void loop() 
{
  if (pumpToggle);
  {
    runPump(pumpTime);
  }
  if (digitalRead(INPUT_PIN1) == HIGH)// you need to debounce this, FYI  if you hod it down, it will extend the time of the pump running
  {
    pumpToggle = true;
    pumpTime =180;
    startTime = millis();
  }
}
//
void runPump(unsigned long secs)
{
  unsigned long elapsedTime = millis() - startTime;
  if (elapsedTime < 5000)
  {
    digitalWrite(pumppin1,HIGH);
  }
  else if (elapsedTime < secs * 1000UL)
  {
    
    digitalWrite(pumppin2,HIGH);
    digitalWrite(pumppin3,HIGH);
    digitalWrite(pumppin4,HIGH);
  }
  else
  {
    digitalWrite(pumppin1,LOW);
    digitalWrite(pumppin2,LOW);
    digitalWrite(pumppin3,LOW);
    digitalWrite(pumppin4,LOW);
    pumpToggle = false;
  }
}

Chiumanfu:
Your IF statements should look like this

No it shouldn't.

Arrch:

Chiumanfu:
Your IF statements should look like this

No it shouldn't.

I'm pretty sure the syntax for IF is

if (someVariable == HIGH)
{
// do something here
}

Unless there is only one line of executable code.

Chiumanfu:

Arrch:

Chiumanfu:
Your IF statements should look like this

No it shouldn't.

I'm pretty sure the syntax for IF is

if (someVariable == HIGH)
{
// do something here
}

Unless there is only one line of executable code.

or unless it has a lot of blocking code (I think the comment was directed at that)

IP isn't going to get what they want with that kind of blocking...

Chiumanfu:

Arrch:

Chiumanfu:
Your IF statements should look like this

No it shouldn't.

I'm pretty sure the syntax for IF is

if (someVariable == HIGH)
{
// do something here
}

Unless there is only one line of executable code.

if(INPUT_PIN1 == HIGH){
  1. You don't compare pin numbers to HIGH/LOW, you compare pin states to HIGH/LOW. To convert a pin number to its state, you use digitalRead().

Arrch:

if(INPUT_PIN1 == HIGH){

good eye, fixed my example.

tobba__1990:
parts washing. I have 6 buttons. and 4 relays. button 1, all relays to be switched on for 30 minutes. Number 2 all relays in an hour. button 3. 3 relays on 30 minutes. Button 4. 3relän 1 hour. 5 key 30 min at 2 relays key 6 2 relays 1 hour

for this you would want something that looks like this, there may be errors but it should get you on the right track.

if(digitalRead(button_1_pin) == HIGH){
  digitalWrite(relay1, HIGH) ;  //assuming that a high output will open/close the relay depending on what you want. 
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  delay(1800000);  //I am not sure if delay can accept a number this large, but I believe it uses long so it should be fine
  digitalWrite(relay1, LOW) ; 
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
}

If you want to have there be an offset between when each relay opens/closes then add a delay in between the corresponding digitalWrite() statements. Also I included digitalWrite(relayx,LOW); because I am assuming you want each relay to open/close after the given time.

Arrch:
2) You don't compare pin numbers to HIGH/LOW, you compare pin states to HIGH/LOW. To convert a pin number to its state, you use digitalRead().

Yes of course. Sorry, should have looked at the example closer.