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:
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
delay(3000);
Is this the correct way?
if(pastime+timer*60000 <= curtime)
long curtime,pastime;
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!
LarryD
May 25, 2019, 9:17pm
11
This is wrong.
long curtime,pastime;
DomekRomek:
what's wrong with it?
Are you expecting to measure negative times ?
LarryD
May 25, 2019, 9:32pm
15
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?
LarryD
May 25, 2019, 9:48pm
17
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.
Hmm... the code seeems to be working , but when i start the code the pump turns on until the digitalWrite(3,LOW) is executed
LarryD
May 25, 2019, 10:01pm
20
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)