How can i trigger my water pump every 30mins on and 20mins off

Hello I'm new in Arduino IDE how can i use my water pump for every hour my water run in 30 mins on and off in 20mins i dont know how to start a program i watch many videos but i failed to programming it... I hope you will help me

Items i used
Arduino Uno R3
Water pump 12v
Relay

Post the code of your best attempt.

Have you heard about millis(), did you look at File|Examples|02.Digital|BilinkWithoutDelay for an intro into using millis?

I just search on the youtube​:sweat_smile::sweat_smile:

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

1 Like

const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState =HIGH;// initial state of LED
long rememberTime=0;// this is used by the code

void setup()
{
pinMode(LEDpin,OUTPUT);// define LEDpin as output
digitalWrite(LEDpin,LEDState);// set initial state

}

void loop() {

if( LEDState ==HIGH )
{
if( (millis()- rememberTime) >= onDuration){
LEDState = LOW;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
else
{
if( (millis()- rememberTime) >= offDuration){
LEDState =HIGH;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}

}

what is the issue you are having?

edit your post and put the code in code tags, please.

First, please enclose the code inside "code" tag (edit your latest post, then select the code and press the "</>" button).

Second, you can't change the LED pin state this way, you need to call digitalWrite(LEDpin, LEDstate) each time you need to change it.

Third, you don't need a variable to keep LED state, you can read it using digitalRead(LEDpin).

It means you can start from here (you still need to go on expanding your code...):

const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
long rememberTime=0;// this is used by the code

void setup()
{
  pinMode(LEDpin,OUTPUT);// define LEDpin as output
  digitalWrite(LEDpin,HIGH);// set initial state
  // You also need this:
  rememberTime=millis();
}

void loop() {
  if( digitalRead(LEDpin)==HIGH )
  {
    if( ( millis()- rememberTime) >= onDuration ) {
      digitalWrite(LEDpin, LOW);// change the state of LED
      rememberTime=millis();// remember Current millis() time
    }
  }
  else
  {
    if( (millis()- rememberTime) >= offDuration ) {
      digitalWrite(LEDpin, HIGH);// change the state of LED
      rememberTime=millis();// remember Current millis() time
    }
  }
}
1 Like
const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState = HIGH; // initial state of LED
long rememberTime = 0; // this is used by the code

void setup()
{
  pinMode(LEDpin, OUTPUT); // define LEDpin as output
  digitalWrite(LEDpin, LEDState); // set initial state

}

void loop() {
  if ( LEDState == HIGH )
  {
    if ( (millis() - rememberTime) >= onDuration) {
      LEDState = LOW;// change the state of LED
      rememberTime = millis(); // remember Current millis() time
    }
  }
  else
  {
    if ( (millis() - rememberTime) >= offDuration) {
      LEDState = HIGH; // change the state of ALED
      rememberTime = millis(); // remember Current millis() time
    }
  }

}

code in code tags, give it a try.

1 Like

Thanks for the correction
I just want to know how can i apply it on relay
Sorry for so many questions im totally a beginner :sweat_smile:

not tested

const int LEDpin = 3;
unsigned long offDuration = 20000;// OFF time for LED
unsigned long onDuration = 30000;// ON time for LED
int LEDState = HIGH; // initial state of LED
unsigned long previousOnTime = millis();
unsigned long previousOffTime = millis();
bool Tick = True; //Tick==true led on
void setup()
{
  pinMode(LEDpin, OUTPUT); // define LEDpin as output
  digitalWrite(LEDpin, LEDState); // set initial state

}
////
void loop() 
{
  if ( Tick )
  {
    digitialWrite( LEDpin, HIGH );
    if ( (millis() - previousOnTime >= onDuration)
    {
      previousOFFTime  = millis(); 
      Tick = !Tick;
    }
  }
  else
  {
    digitialWrite( LEDpin, LOW );
    if ( (millis() - previousOFFTime) >= offDuration) {
      previousOnTime = millis();
      Tick = !Tick;
    }
  }

}
1 Like

Thanks i gonna try this to

Hello gridd

Check this small example using an array for the controlling of the timer.

const int PumpPin = 8;
constexpr unsigned long OffDuration = 20000;// OFF time for Pump
constexpr unsigned long OnDuration = 30000;// ON time for Pump
constexpr unsigned long PumpTimes[] {OffDuration,OnDuration};
void setup()
{
  pinMode(PumpPin, OUTPUT); // define LEDpin as output
  digitalWrite(PumpPin, HIGH); // set initial state
}
void loop() 
{
  static unsigned long timeStamp=millis();  
  if (millis()-timeStamp>=PumpTimes[digitalRead(PumpPin)]) 
  {
    timeStamp=millis(); 
    digitalWrite(PumpPin,digitalRead(PumpPin)^1); 
  }
}
1 Like

Thank you

In this statement
The bool part is not declared as said in Arduino

Its working but the accurate of time doesn't goes in it​:sweat_smile::sweat_smile::sweat_smile:

You have to modifiy these duration values to your needs.

constexpr unsigned long OffDuration = 20000;// OFF time for Pump
constexpr unsigned long OnDuration = 30000;// ON time for Pump

You can fix that issue, right?

I'll try😅

whilst you are trying to fix issues, please add code tags to the previous posts of yours. They make the forum look ugly...

thanks

You might need to spell true with a small 't'.

bool Tick = true; //Tick==true led on

Which would be clearer to us if @gridd had copy-pasted the entire error message.

Added - Yes:

sketch.ino:1:22: error: 'True' was not declared in this scope
bool caseSensitive = True;

a7