Whats wrong with my if statement?

When '*' is pressed we will go to setting up a date for a password to valid, and the date will be store in dateA. The password is invalid when it is not the time.
DateA[3] store the year, dateA[5] store the month, dateA[7] store the date.

Can you explain what this all is without any code references? Like what is it, a safe? How does it work? Not the code but how would I operate it once you're finished. We wasted a ton of time last night fighting this and it would really help to know what exactly I'm building so that rather than try to fix this crap code I can maybe help you write something sensible.

Im doing a project on a security door. First, we need to set a date for a password to work. We are not setting the time for the rtc right here, just to store a date that later will be compared to rtc. The set date will then generate a password itself. The password is only correct when the current time is equal to the set date. Outside of the date, the password should be incorrect. Thats how it should be.

My problem is the password is always correct eventhough the date is not equal to the time its suppose to correct.

So the password is a date and it only works if you use it at exactly the right time?

No, I mean tell me how I would operate it, not how it works internally. If I read the owners manual for this thing what would it tell me to do?

Delta_G:
So the password is a date and it only works if you use it at exactly the right time?

yesy it is

Delta_G:
If I read the owners manual for this thing what would it tell me to do?

This kind of like security door for a homestay, it is to ease the owner to not have come to the homestay to set a password. For the first phase this is the manual.

To set a date for the password to work, pressed '*', then key in the date. After finished key in the date, press '#' which act as enter. Then when the LED ask to enter a password, simple just key in the password and press '#' which act as enter. The user should get the password from the owner everyday because it will change once its the new day.

So the date isn't the password? The password is different from the date but is only good for the one time that was entered?

ire.requestFrom(0x68, 7); // send 7 bytes
  byte secs = Wire.read();
  byte mins = Wire.read();
  byte hrs = Wire.read();
  byte day = Wire.read();
  byte date = Wire.read();
  byte month = Wire.read();
  byte year = Wire.read();

You're still trying to pull the values off the RTC yourself instead of using the libraries you are including. Why? Didn't we talk about that on your other thread?

The easiest thing to do would be to set up the sync provider from the Time library so that it can get time from the RTC and let it handle all the time stuff. You just ask it for the minute, hour, second, etc.

Delta_G:
So the date isn't the password? The password is different from the date but is only good for the one time that was entered?

The password comes from manipulating the date. So the password is not the date.The password is only valid the whole day that the date has been set up by the owner when he press '*'.

Delta_G:

ire.requestFrom(0x68, 7); // send 7 bytes

byte secs = Wire.read();
  byte mins = Wire.read();
  byte hrs = Wire.read();
  byte day = Wire.read();
  byte date = Wire.read();
  byte month = Wire.read();
  byte year = Wire.read();




You're still trying to pull the values off the RTC yourself instead of using the libraries you are including. Why? Didn't we talk about that on your other thread?

The easiest thing to do would be to set up the sync provider from the Time library so that it can get time from the RTC and let it handle all the time stuff. You just ask it for the minute, hour, second, etc.

I change it to used the RTC library. I redeclare the year and minus it to 2000 because I want it to be only 2 digit as i am comparing to 2 digit which come from dateA[3]. Still same result, the password will be accepted even its not the time. It also accept when the right time.

  int YEAR= now.year()-2000;
  Serial.print(YEAR);
 if(!((YEAR==(dateA[3]-'0'))&&(now.month()==(dateA[5]-'0'))&&(now.day()==(dateA[7]-'0'))))

I also try to randomly compare it to a random number. and it still accept the password which it should not

if(!((YEAR==(7))&&(now.month()==(7))&&(now.day()==(7))))

OK, let's try putting some print statements right before that if statement and print out all the things you are comparing and see if they have the values that you think they should.

Why are you checking in your if statement for it to NOT be true?

Delta_G:
Why are you checking in your if statement for it to NOT be true?

I try to check it with true but it never go into that statement. Now I know why

I checked the dateA at the end of change() and in the beginning of loop() just before the if statement and the result is not the same.

Serial.println();
  Serial.print("rtc :");
  Serial.print(YEAR);
  Serial.print("/");
  Serial.print(now.month());
  Serial.print("/");
  Serial.print(now.day());

  Serial.println();
  Serial.print("dateA[]:");
  Serial.print(dateA[3]-'0');
  Serial.print("/");
  Serial.print(dateA[5]-'0');
  Serial.print("/");
  Serial.print(dateA[7]-'0');
  Serial.println();

This is my result

rtc : 17/5/22
dateA in change() : 17/5/22
dateA in loop() :5/2/8

I wonder what makes it change drastically because I did not do any code to change it :o

Post the code that generated that output. The whole thing

if (dateA[2]==('1'))
      {
        dateA[3]=dateA[3]+10;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('2'))
      {
        dateA[3]=dateA[3]+20;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('3'))
      {
        dateA[3]=dateA[3]+30;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('4'))
      {
        dateA[3]=dateA[3]+40;
        EEPROM.write(3+10,dateA[3]);
      }

     if (dateA[2]==('5'))
      {
        dateA[3]=dateA[3]+50;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('6'))
      {
        dateA[3]=dateA[3]+60;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('7'))
      {
        dateA[3]=dateA[3]+70;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('8'))
      {
        dateA[3]=dateA[3]+80;
        EEPROM.write(3+10,dateA[3]);
      }
      if (dateA[2]==('9'))
      {
        dateA[3]=dateA[3]+90;
        EEPROM.write(3+10,dateA[3]);
      }

Why? Just why?

How about:

dateA[3] = dateA[3] + (10 * (dateA[2] - '0'));

Now let's clean this up a lot. Make changes like the one I just mentioned. And get rid of code like the direct reads from the rtc that we aren't using anymore. This code is starting to get too convoluted to try to follow.

Please format it too so I can see where the block all are. Indenting is important. If you just press Control-T in the IDE it will do it for you.

OK. Let's make one more important change. This will simplify things a LOT but might take you a while to do. Let's stop storing things in the EEPROM as ASCII. Just put the numbers in there themselves. This has 2 advantages, first it only takes one slot in EEPROM to store a number like 22 instead of 2 slots. And second it makes it easier to keep up with things without all the conversions to and from ASCII. When you get ASCII from the keypad, convert it to a real number BEFORE you put it in EEPROM or try to do anything else with it.

qiste,
From my perspective, you will be struggling quite a bit on this project.
The reason being is we are seeing that you going down some strange implementation paths of re-inventing the world, and we can’t seem to get even a high level description what of the project is trying to do.

Delta_G:
The easiest thing to do would be to set up the sync provider from the Time library so that it can get time from the RTC and let it handle all the time stuff. You just ask it for the minute, hour, second, etc.

In this case, given the OP wants to work with local time components, the Time library may not be helpful.
The RTClib library includes API method functions to calculate time offsets using local time components which would make the comparisons relatively easy.

qiste,
I will say this again this time even STRONGER:
GO READ AND STUDY THE INFORMATION ABOUT YOUR LIBRARIES AND USE THE API METHODS/FUNCTIONS AVAILABLE IN THEM.

The RTClib includes many method functions that will assist in what you are wanting to do. There is no need to re-invent the wheel.
The library includes examples that demonstrate the API method functions available.

In the mean time, it would be helpful if you could provide a description of what you are trying to accomplish so we could offer better advice.

i.e. we need to understand the high level project goals/requirements before we start to dig down into the dirt of implementation details.

— bill

      dateA[7] = dateA[7] + (10 * dateA[6]);
      dateA[5] = dateA[5] + (10 * dateA[4]);
      dateA[3] = dateA[3] + (10 * dateA[2]);

      EEPROM.write(7 + 10, dateA[7]);
      EEPROM.write(5 + 10, dateA[5]);
      EEPROM.write(3 + 10, dateA[3]);

I havent done the -'0' part yet because Im doing it one by one and not trying to confuse myself.
But it seem that replacing the one code into this one give me a weird output

I even checked the value of dateA before and after that code.
Result

change() A before code : 17/5/22
change() A after code : -15/-27/-10

bperrybap:
qiste,
I will say this again this time even STRONGER:
GO READ AND STUDY THE INFORMATION ABOUT YOUR LIBRARIES AND USE THE API METHODS/FUNCTIONS AVAILABLE IN THEM.

The RTClib includes many method functions that will assist in what you are wanting to do. There is no need to re-invent the wheel.
The library includes examples that demonstrate the API method functions available.

In the mean time, it would be helpful if you could provide a description of what you are trying to accomplish so we could offer better advice.

i.e. we need to understand the high level project goals/requirements before we start to dig down into the dirt of implementation details.

--- bill

yes sir, i am now using the function available in RTClib

qistie:
The password comes from manipulating the date. So the password is not the date.The password is only valid the whole day that the date has been set up by the owner when he press '*'.

I change it to used the RTC library. I redeclare the year and minus it to 2000 because I want it to be only 2 digit as i am comparing to 2 digit which come from dateA[3]. Still same result, the password will be accepted even its not the time. It also accept when the right time.

  int YEAR= now.year()-2000;

Serial.print(YEAR);

I think I have an idea what you are likely to be trying to do in the final project but it still isn't clear.
It sounds like you are wanting to auto generate a password based on a date. While that can be made to work, it is highly insecure since it is not based on any knowledge factors. i.e. it is security based on obscurity using a hard coded algorithm based solely on a date. And as soon as anybody figures out the algorithm, the access is fully compromised and requires a code update to fix it since the algorithm is hard coded.

As far as the specific code you are currently writing, it appears to be some sort of testing or simulation code.
If you are trying to do some simulation testing (which is good) then you don't even need to use or involve an RTC at all yet. You can write and test your input and date to password generation and checking code without involving an RTC.

Also your comparisons seem strange - but mainly we still don't understand what you are trying to do.
You are comparing values that are numbers to a number that is created from a single character.
That seems very odd.

 iif (!((YEAR == (dateA[3] - '0')) && (now.month() == (dateA[5] - '0')) && (now.day() == (dateA[7] - '0'))))

For example YEAR is currently 17, in order to get a comparison match, dateA[3] would have be '0' + 17 which would be the character 'A'.
now.month() is currently 5 which means dateA[5] would have to be the character '5' which is ok but when you get to october the character would need to be a colon ':'

And for now.day() which today is 21, dateA[7] would need to be '0' + 17 or the character 'E'
Maybe that is what you wanted, but it sure seems screwy.
And the average person doesn’t have all the ASCII codes memorized so they would need have a table with them to know the magic characters to use.

Like we have been saying over and over again.
If you would describe what you really wanting to do, it would really help as, IMO, we are off chasing our tails with an X Y problem:
http://xyproblem.info/

--- bill

bperrybap:
I think I have an idea what you are likely to be trying to do in the final project but it still isn’t clear.
It sounds like you are wanting to auto generate a password based on a date.

Yes that is very correct. I did my best to try and explain my project. Sorry for my english.

bperrybap:
While that can be made to work, it is highly insecure since it is not based on any knowledge factors. i.e. it is security based on obscurity using a hard coded algorithm based solely on a date. And as soon as anybody figures out the algorithm, the access is fully compromised and requires a code update to fix it since the algorithm is hard coded.

That is correct too but i am asked to specifically requires to do the project based on algorithm.

bperrybap:
As far as the specific code you are currently writing, it appears to be some sort of testing or simulation code.
If you are trying to do some simulation testing (which is good) then you don’t even need to use or involve an RTC at all yet. You can write and test your input and date to password generation and checking code without involving an RTC.

I have a few different sketch to test and to do the simulation and I have also tested the code without involving an RTC.

bperrybap:
Also your comparisons seem strange - but mainly we still don’t understand what you are trying to do.
You are comparing values that are numbers to a number that is created from a single character.
That seems very odd.

 iif (!((YEAR == (dateA[3] - '0')) && (now.month() == (dateA[5] - '0')) && (now.day() == (dateA[7] - '0'))))

For example YEAR is currently 17, in order to get a comparison match, dateA[3] would have be ‘0’ + 17 which would be the character ‘A’.
now.month() is currently 5 which means dateA[5] would have to be the character ‘5’ which is ok but when you get to october the character would need to be a colon ‘:’

And for now.day() which today is 21, dateA[7] would need to be ‘0’ + 17 or the character ‘E’
Maybe that is what you wanted, but it sure seems screwy.
And the average person doesn’t have all the ASCII codes memorized so they would need have a table with them to know the magic characters to use.

— bill

That is why im comparing the year,month and date to minus ‘0’ so it compares to a decimal. I have manipulate the dateA in change().

I am now trying to understand why my dateA change value when if (key == ‘#’) in the loop.
It seems like the value of dateA is correct before button ‘#’ is pressed and after it been press it change drastically