I seem to have created an endless loop, not sure what I did wrong

Thank you in advance for your help. I swear this was working before. I loop through each line of the EEPROM and then pull out each of every 4 lines. After it loops through the entire eeprom, it just starts over from the beginning.

int LookForUidInEEPROM(){
Serial.println("inloop");
  byte compare[4];
  int count;
  for (int i = 0; EEPROM.length(); i = i + 4){
  compare[0] = EEPROM.read(i);
  compare[1] = EEPROM.read(i+1);
  compare[2] = EEPROM.read(i+2);
  compare[3] = EEPROM.read(i+3);
  
    
//    if(compare == GivenKey){
//      return i;
//    }else if(i >= EEPROM.length()){
//      return -1;
//    }
Serial.println(count);
count = count +1;
  }

  Serial.println("end of for loop");
  delay(5000);
  Serial.println("end of for loop");
}

I never get out of the loop.

Hello,

Yes, this line is wrong and creates an infinite loop:

for (int i = 0; EEPROM.length(); i = i + 4){

Can you spot the mistake?

Hint: the second argument of the for loop must be a conditional statement :slight_smile:

guix:
Hello,

Yes, this line is wrong and creates an infinite loop:

for (int i = 0; EEPROM.length(); i = i + 4){

Can you spot the mitake?

Hint: the second argument of the for loop must be a conditional statement :slight_smile:

my first instinct is to do

for (int i = 0; i == EEPROM.length(); i = i + 4){

but then it won’t run the loop even a single time.

for (int i = 0; i < EEPROM.length(); i = i + 4){

i swear it worked the other way before. i was using this function.

guix:
Hello,

Yes, this line is wrong and creates an infinite loop:

for (int i = 0; EEPROM.length(); i = i + 4){

Can you spot the mistake?

Hint: the second argument of the for loop must be a conditional statement :slight_smile:

ok, i see what I did wrong and why it “worked” and why i got confused.

int LookForUidInEEPROM(){
  byte compare[4];
  for (int i = 0; i < EEPROM.length()+1; i = i + 4){
  compare[0] = EEPROM.read(i);
  compare[1] = EEPROM.read(i+1);
  compare[2] = EEPROM.read(i+2);
  compare[3] = EEPROM.read(i+3);  
    
    if(compare[0] == nuidPICC[0] || compare[1] == nuidPICC[1] || compare[2] == nuidPICC[2] || compare[3] == nuidPICC[3]){
      return i;
    }else if(i >= EEPROM.length()){
      return -1;
    }
  }
}

my if simply returned me out of the loop

    if(compare[0] == nuidPICC[0] || compare[1] == nuidPICC[1] || compare[2] == nuidPICC[2] || compare[3] == nuidPICC[3]){
      return i;

If one of the bytes match, get out of loop. Is that what you need?

sterretje:

    if(compare[0] == nuidPICC[0] || compare[1] == nuidPICC[1] || compare[2] == nuidPICC[2] || compare[3] == nuidPICC[3]){

return i;



If one of the bytes match, get out of loop. Is that what you need?

if all of them match i return "i". if any of them don't match, i return "-1"

clearly i do not remember operators...

Hi,
I think you need to use the Reference tab provided at the top of this page.

AND ALSO

google arduino boolean operators

Tom... :slight_smile:

I'm having trouble finding "and also operator"

birddseedd:
I'm having trouble finding "and also operator"

?

&& is and. || is or. I dont know what and or is

Hi,

if event1==1 AND event2==1 AND event3==1 AND event4==1

no need for an ALSO...

Tom... :slight_smile:

Hi,

Please write out what you want the statement to say in plain language.

Thanks.. Tom.. :slight_smile:

Hi,

if (event1==1 AND event2==1) OR (event3==1 AND event4==1)

Tom... :)

Okay I miss read what you were saying. "And" is what I needed.

Although I'm still doing something weird with the data. It does work in a function that I made but in my main code I can't get it to work. And it's writing out something weird for the data

Post your complete code of you need help.

Ah, I see you continued in your earlier thread