milis?

int potV,PWM = 155,timer = 0.5;
long curtime,pastime;


void setup() {
  Serial.begin(9600);
  //attachInterrupt(digitalPinToInterrupt(2), Interrupt, FALLING);
}

void loop() {
  //potV = analogRead(0);
  curtime = millis();
  if(pastime+timer*60000 <= curtime)
  {
    pastime = curtime;
    Serial.println("WATER COMMENCE!");
    Water;
    
    }
  else{
    Serial.println(curtime);
  }
  
}

void Water() {
  Serial.println("Watering...");
  analogWrite(3,PWM);
  delay(3000);
  digitalWrite(3,LOW);
  Serial.println("Done!");
}



void Interrupt(){
  //potV = analogRead(0);
  //timer = map(potV,0,1023,0,600);
  Serial.print(timer);
  Serial.println(" minutes");
}

I'm trying to make an automatic watering thingy and idk what's wrong with my code but instead of counting up and printing the current time in miliseconds it starts the pump

A few things are wrong but before you continue with debugging, read this:

delay() stops regular code execution for the delay interval.

You can replace delay() with a technique called Blink Without Delay, BWD.

When using BWD, your sketch can run other code during the delay time.

Read Robin2’s discussion:

https://forum.arduino.cc/index.php?topic=223286.0

Do you now fully understand what’s wrong?

If not what are you having trouble understanding?

Also read:

    Water;

?

A few things are wrong but before you continue with debugging, read this:

delay() stops regular code execution for the delay interval.

You can replace delay() with a technique called Blink Without Delay, BWD.

When using BWD, your sketch can run other code during the delay time.

Read Robin2's discussion:

Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Do you now fully understand what's wrong?

If not what are you having trouble understanding?

Also read:
Gammon Forum : Electronics : Microprocessors : Arduino programming traps, tips and style guide

but i didn't use delay() though

DomekRomek:
but i didn't use delay() though

  delay(3000);

What about this one ?

delay(3000); :wink:

Is this the correct way?
if(pastime+timer*60000 <= curtime) :wink:

long curtime,pastime; :wink:

:wink:

void Interrupt(){
  //potV = analogRead(0);
  //timer = map(potV,0,1023,0,600);
  Serial.print(timer);
  Serial.println(" minutes");
}

You didn’t do the readings did you?

UKHeliBob:

  delay(3000);

What about this one ?

i exclude this one because it isn't supposed to interfear with the other parts of the code and i set pastime=curtime

i'm even more confused now than i was beforehand.

int potV,PWM = 155,timer = 0.5;
long curtime,pastime;


void setup() {
  Serial.begin(9600);
  //attachInterrupt(digitalPinToInterrupt(2), Interrupt, FALLING);
}

void loop() {
  //potV = analogRead(0);
  curtime = millis();
  if(pastime+timer*60000 <= curtime)
  {
    
    Serial.println("WATER COMMENCE!");
    water();
    pastime = curtime;
    }
  else{
    Serial.println(curtime);
  }
  
}

void water() {
  Serial.println("Watering...");
  analogWrite(3,PWM);
  delay(3000);
  digitalWrite(3,LOW);
  Serial.println("Done!");
}



void Interrupt(){
  //potV = analogRead(0);
  //timer = map(potV,0,1023,0,600);
  Serial.print(timer);
  Serial.println(" minutes");
}

my code tweaked a bit, but i still don't understand what's wrong with using delay(3000)?

Did you read and understand the discussion?

int potV,PWM = 155,timer = 0.5,thedelay=3000;
long curtime,pastime;


void setup() {
  Serial.begin(9600);
  //attachInterrupt(digitalPinToInterrupt(2), Interrupt, FALLING);
}

void loop() {
  //potV = analogRead(0);
  curtime = millis();
  if(pastime+timer*60000 <= curtime)
  {
    
    Serial.println("WATER COMMENCE!");
    water();
    
    }
  else{
    Serial.println(curtime);
  }
  
}

void water() {
  Serial.println("Watering...");
  analogWrite(3,PWM);
  if(pastime + timer*60000 + thedelay<= curtime){
  digitalWrite(3,LOW);
  Serial.println("Done!");
  pastime = curtime;
  }
}



void Interrupt(){
  //potV = analogRead(0);
  //timer = map(potV,0,1023,0,600);
  Serial.print(timer);
  Serial.println(" minutes");
}

yeah so i replaced the delay with milis comparison thing and it helped 50%, because it kept repeating the

digitalWrite(3,LOW);
  Serial.println("Done!");
  pastime = curtime;

part

and the serial monitor looks like this:

Done!
WATER COMMENCE!
Watering...
Done!
WATER COMMENCE!
Watering...
Done!
WATER COMMENCE!
Watering...
Done!
WATER COMMENCE!
Watering...
Done!

This is wrong.
long curtime,pastime;

larryd:
This is wrong.
long curtime,pastime;

what's wrong with it?

DomekRomek:
what's wrong with it?

Are you expecting to measure negative times ?

UKHeliBob:
Are you expecting to measure negative times ?

no

Reading the url is important to fully understand the technique.
long curtime,pastime;

should be:
unsigned long curtime,pastime;

This is wrong:
int timer = 0.5;

larryd:
Reading the url is important to fully understand the technique.
long curtime,pastime;

should be:
unsigned long curtime,pastime;

This is wrong:
int timer = 0.5;

how is it supposed to be then?

long curtime,pastime;
should be:
unsigned long curtime,pastime;

This is wrong:
int timer = 0.5;
An integer (int) cannot be a decimal value i.e. it is a whole number only.
An int on the Arduino can be in the a range of -32,768 to 32,767

This is wrong:
if(pastime+timer*60000 <= curtime)

could you list out all the other wrong things and how they should be? thanks in advance. :slight_smile:

Hmm... the code seeems to be working , but when i start the code the pump turns on until the digitalWrite(3,LOW) is executed

A programmer has to have a good understanding of the basics.

Please read this discussion by Nick Gammon.

https://gammon.com.au/forum/?id=12127

After you read the above, tell us what your thought process was when you wrote:
if(pastime+timer*60000 <= curtime)