[SOLVED] If statement inside of for loop

See bottom for actual question

So I am attempting to make a time-sensitive action in a project I am working on. This shortened debugging version of the code shows the general idea.

#include <Wire.h> //An included arduino library that enables I2C communications on pins A4 and A5
#include <DS3231.h> //A downloaded library that enables communication with the RTC

DS3231 RTC;

bool h12;
bool PM;
bool EndPeriodCheck = false;

#define PeriodAmount 9 // how many periods will you have
int PeriodEnd [PeriodAmount] [3] =
{ 8, 51, 45,  //starting hour, 1 minute past ending time, 5 min before ending time
  9, 51, 45,
  10, 21, 15,
  10, 51, 45,
  12, 21, 15,
  13, 21, 15,
  14, 21, 15,
  15, 21, 15,
  23, 56, 40
};


void setup() {
  Serial.begin(9600); //start serial comms, mostly for debuggig or checking set times, etc

  Wire.begin(); //initializes I2C comms

}
void loop() {
  
  for (int i=0; i<9; i++) {
    if (RTC.getMinute() == PeriodEnd [i] [3]){
     EndPeriodCheck = true; 
    }
  }
}

  Serial.println (EndPeriodCheck);
  delay(1000);

The program is supposed to check the “PeriodEnd” array for times and if the time matches, execute a command in this case, simply changing EndPeriodCheck to true (I’ve been using the 8th line of the array as test times). The problem is, it seems the if statement never seems to be fulfilled. Is there an issue with placing an if statement inside of a for loop? I can’t seem to find anything online that answers this. There are many posts about the converse, placing a for loop in an if statement, but not this. It seems possible in other C based languages like python, but I can’t seem to find an answer that is Arduino specific.

Additionally, if I change my code around so the time is not in an array and simply checks one integer, it works. Like this:

#include <Wire.h> //An included arduino library that enables I2C communications on pins A4 and A5
#include <DS3231.h> //A downloaded library that enables communication with the RTC

DS3231 RTC;

bool h12;
bool PM;
bool EndPeriodCheck = false;
int PeriodEnd = 40;


void setup() {
  Serial.begin(9600); //start serial comms, mostly for debuggig or checking set times, etc

  Wire.begin(); //initializes I2C comms

}
void loop() {
  
    if (RTC.getMinute() == PeriodEnd){
     EndPeriodCheck = true; 
    }
}

  Serial.println (EndPeriodCheck);
  delay(1000);

Obviously, though, writing each individual number in that array as well as an if statement for each of those variables would be a waste of space.

So my question: is placing an if statement inside of a for loop possible or does something about it not function properly? If it is possible could someone point me in the right direction as I am clearly doing something wrong? If it is not possible, could someone suggest some alternatives?

Thanks for helping!

if (RTC.getMinute() == PeriodEnd [i] [3])

There is no element at index 3. With size 3, the index is ranged from 0 - 2

I... I can't believe I didn't catch that...

An if statement inside or outside of a for loop is just fine.

However...

    if (RTC.getMinute() == PeriodEnd [i] [3]){

This is NOT fine. Arrays in C have a zero-based index, so the 3 should be no larger than 2. Furthermore, a quick look at the program suggests that, when comparing minutes, this index should be 1.

If the data is properly formatted, PeriodAmount should be unnecessary.

Sorry, I don't see a very important comand: thw way in witch thw variable return 0. Witchout that line rhe program continue to show 1.
After please change the type of the array form 'int' to 'const byte', because 1) in Arduino you have not much memory for variables, and so you have to optimize the space
2) the 'const' word means that Arduino forbid to kodify that value, and forget an = in an if is common, so I transform a controll (==) in a assignement (=).

At least (I'm very sorry, because I know you made your best in this)I ask you what is it for? I dln't have to use an Arduino for a so simple timing problem. I can check the clock

arduino_new:
There is no element at index 3. With size 3, the index is ranged from 0 - 2

Yeah, I can’t believe I didn’t think about that. This is almost definitely the issue. Will update the post if I get it up and running.

vaj4088:
This is NOT fine. Arrays in C have a zero-based index, so the 3 should be no larger than 2. Furthermore, a quick look at the program suggests that, when comparing minutes, this index should be 1.

If the data is properly formatted, PeriodAmount should be unnecessary.

Like I said above, the three is definitely my issue, thanks! There are actually two slots for minutes as that array of data will be checked once to start the action, then again to end it. Both the 1 column and the 2 column are minute markers used for different things.

PeriodAmount is only there because in the full code it is used more than once so having it defined saves some time.

Silente:
Sorry, I don’t see a very important comand: thw way in witch thw variable return 0. Witchout that line rhe program continue to show 1.
After please change the type of the array form ‘int’ to ‘const byte’, because 1) in Arduino you have not much memory for variables, and so you have to optimize the space
2) the ‘const’ word means that Arduino forbid to kodify that value, and forget an = in an if is common, so I transform a controll (==) in a assignement (=).

At least (I’m very sorry, because I know you made your best in this)I ask you what is it for? I dln’t have to use an Arduino for a so simple timing problem. I can check the clock

Thanks for the response. You are correct, this bit of code is incredibly useless, but I was using it more as a proof of concept. This was a very small portion stripped from a larger code. This was just the specific piece that wasn’t working properly. I didn’t include any unnecessary bit of code so that my issue was easier to read through.

"There are actually two slots for minutes as that array of data will be checked once to start the action, then again to end it. Both the 1 column and the 2 column are minute markers used for different things."

Then perhaps an array of struct is more appropriate?
It would certainly reduce the opportunity of using the wrong index.

Also,

const unsigned byte PeriodAmount = sizeof(PeriodEnd)/sizeof(PeriodEnd[0]/3 ;

CORRECTION (left out a parenthesis):

const unsigned byte PeriodAmount = sizeof(PeriodEnd)/sizeof(PeriodEnd[0])/3 ;