Arduino MKR 1010 wifi

what is wrong with the code? I don't get any errors but the relay doesn't turn on.

#include <RTCZero.h>
#include <FastLED.h>
#define NUM_LEDS 300
#define DATA_PIN 8
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 50
#define VOLTS 5
#define MAX_AMPS 500
CRGB leds[NUM_LEDS];

int Relay = 4;

RTCZero rtc;

// Removed the const keyword so the variables can be updated
byte seconds = 00;
byte minutes = 59;
byte hours = 4;

byte day = 2;
byte month = 12;
byte year = 23;

int OnTime = 300;
int OnTime2 = 1020;
int OffTime1 = 480;
int OffTime2 = 1380;

int mOnTime = hours * 60 + minutes;

//5:30=330 min
//8=480 min
//17=1020 min
//23=1380 min

void setup() {
  Serial.begin(9600);

  rtc.begin();  // initialize RTC

  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);

  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();

  /*  if (OffTime1 < mOnTime) { // turn off next day?
    OffTime1 += 24*60;
}

    if (OffTime2 < mOnTime2) { // turn off next day?
    OffTime2 += 24*60;
}*/
}

void loop() {
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  print2digits(hours = rtc.getHours());  // We update the hours variable like this here
  Serial.print(":");
  print2digits(minutes = rtc.getMinutes());  // We update the minutes variable like this here
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  updateRelay();  // we update the relay here

  delay(1000);
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");  // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

// moved the relay code to it's own function
void updateRelay() {
  if (mOnTime >= OnTime && mOnTime <= OffTime1 || mOnTime >= OnTime2 && mOnTime <= OffTime2) {
    digitalWrite(Relay, HIGH);
    Serial.println("LIGHT ON");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::White;
      FastLED.show();
      delay(50);
    }
  } else {
    digitalWrite(Relay, LOW);
    Serial.println("LIGHT OFF");
  }
}

What makes you believe the problem is in the code?

the relay does not turn on when the time I set is reached.

Strip your code to the bare minimum to control the relay. Does that work? If not, draw your conclusions. If yes, add the timing; does it still work. And so on till you figure out where it fails.

I would advise to use some extra () there; personally I'm never sure of the priorities (&& vs ||).

​I'm not that experienced, I've copied the code, so it's hard for me to see where the error is. if I instead write the code as below, it works as long as I set the time >=10. it doesn't work if I write e.g. >=5. if I write a number <10 the relay doesn't turn on but I don't get an error code.

#include <RTCZero.h>
#include <FastLED.h>
#define NUM_LEDS 300
#define DATA_PIN 8
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 50
#define VOLTS 5
#define MAX_AMPS 500
CRGB leds[NUM_LEDS];

int Relay = 4;

RTCZero rtc;

// Removed the const keyword so the variables can be updated
byte seconds = 00;
byte minutes = 31;
byte hours = 10;

byte day = 2;
byte month = 12;
byte year = 23;

int OnTime = 632;
int OnTime2 = 1020;
int OffTime1 = 680;
int OffTime2 = 1380;

int mOnTime = hours * 60 + minutes;

//5:30=330 min
//8=480 min
//17=1020 min
//23=1380 min

void setup() {
  Serial.begin(9600);

  rtc.begin();  // initialize RTC

  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);

  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();

   /* if (OffTime1 < OnTime) { // turn off next day?
    OffTime1 += 24*60;
}

    if (OffTime2 < OnTime2) { // turn off next day?
    OffTime2 += 24*60;
}*/
}

void loop() {
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  print2digits(hours = rtc.getHours());  // We update the hours variable like this here
  Serial.print(":");
  print2digits(minutes = rtc.getMinutes());  // We update the minutes variable like this here
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  updateRelay();  // we update the relay here

  delay(1000);
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");  // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

// moved the relay code to it's own function
void updateRelay() {
  //if ((hours >= 10 && minutes >= 32) && (hours <= 10  && minutes <= 35) || (hours >= 16 && minutes >= 30) && (hours <= 23 && minutes <= 55)) {
  if ((mOnTime >= OnTime && mOnTime <= OffTime1) || (mOnTime >= OnTime2 && mOnTime <= OffTime2)) {
    digitalWrite(Relay, HIGH);
    Serial.println("LIGHT ON");
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::White;
      FastLED.show();
      delay(50);
    }
  } else {
    digitalWrite(Relay, LOW);
    Serial.println("LIGHT OFF");
  }
}

Upload the blink example sketch and set the led pin to the pin your relay is connected to. Does the relay work now?

yes it works the relay is on and off

Because this is a global variable, it will be set to hours * 60 + minutes when the Arduino starts up. At that moment, hours will be zero and minutes will be zero, because rtc.begin() has not been called yet (that does not get called until setup() runs, which will be after the global variable is assigned).

Your code never updates mOnTime again. The calculation hours*60+minutes will not get done each time your code uses mOnTime, because that is not how variables work.

I can't see any reason why mOnTime needs to be a global variable. It is only used inside updateRelay() I think. So you can just make mOnTime a local variable by moving it inside updateRelay().

yes it seems to work. I will test more but the relay turned on at the right time. I am very grateful for your help.