Ive got some issues, and I don't get why it doesn't work

Hello everyone,

I've been working on a little script that is supposed to turn my home server on and off after a while. I tested it a couple of times with timeframes of 30 and 60 minutes, and all worked fine. But now that I have bigger numbers, for a 24hour schedule, it doesn't work anymore.

Can you guys help me please?

Thanks in advance,

Dorus

PS: Sorry for my English

This is what it looks like:

int LedPin = 13;
int SensorPin = 9;
int RelayPin = 8;
int time = 0;
int val;

void setup() {
  pinMode(SensorPin, INPUT);
  pinMode(LedPin, OUTPUT);
  pinMode(RelayPin, OUTPUT);
  digitalWrite(LedPin, HIGH);
  
}
void loop(){


while(time < 960){
   
  digitalWrite(RelayPin, LOW);
  delay(60000);
time ++;
}
  
if (time == 960  ) {
  val = digitalRead(SensorPin);
  if (val == HIGH){
    delay(60000);
    time ++;
  }
  else{
    digitalWrite(RelayPin, HIGH); //This gives a high signal to the relay, which switches ON the server
  delay(1000);
   digitalWrite(RelayPin, LOW);
   delay(59000);
    time ++;
  }
    
  }
 
   
val = digitalRead(SensorPin);
 if (time >= 1440){
   if (val == HIGH){
     digitalWrite(RelayPin, HIGH); //This gives a high signal to the relay, which switches OFF the server
  delay(1000);
   digitalWrite(RelayPin, LOW);
   
   time = 0;
   }
   else{
     time = 0;
   }
   }
    
 
   else{
     time ++;
     delay(60000);
   }
 
 

  } // end loop

Moderator edit: Code tags added and duplicate post deleted.

The usual problem is with sign extension from 16 bit variables.
Remember to qualify constants that need to be used in long calculations with a "UL" suffix.
e.g.

#define MILLIS_PER_SECOND 1000UL
#define SECONDS_PER_MINUTE 60UL
#define MINUTES_PER_HOUR 60UL

and so on.

Do not cross post your questions on different sections.
People do not like that round here. It waste time answering things in one thread that have been answered in another.

Topics merged.

@OP: this hardly qualifies as a suggestion for the Arduino project.

DO NOT CROSS POST.

It makes me CROSS ]:slight_smile:

If you cross post you do have issues. So I can understand the first part of your subject. :wink:

As to the second part :"why it doesn't work"
I would rewrite the whole thing. There are plenty op people who did something similar and published their code. Read it and learn.
As to your code
There are probably other issues but replacing all "int"s to "unsigned long"s should stretch your window a bit.
For instance: replace

int time = 0;

by

unsigned long time = 0;

Best regards
Jantje