Skip to main content
+ method aliasing to uint64_t.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}

If you don't mind the array being backwards, you can also handle the whole array as a single 64-bit number, and just increment it like you would increment any integer. But then you have to print the array backwards, starting from the mast element:

union {
    uint64_t number;
    byte bytes[8];
} Hexa_Val = { .number = 0 };

void loop() {
    // Increment the array.
    Hexa_Val.number++;

    // Print out.
    for (int i = 7; i >= 0; i--) {
        if (Hexa_Val.bytes[i] < 0x10) Serial.write('0');
        Serial.print(Hexa_Val.bytes[i], HEX);
        Serial.write(' ');
    }
    Serial.println(); 

    delay(500);
}

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}

If you don't mind the array being backwards, you can also handle the whole array as a single 64-bit number, and just increment it like you would increment any integer. But then you have to print the array backwards, starting from the mast element:

union {
    uint64_t number;
    byte bytes[8];
} Hexa_Val = { .number = 0 };

void loop() {
    // Increment the array.
    Hexa_Val.number++;

    // Print out.
    for (int i = 7; i >= 0; i--) {
        if (Hexa_Val.bytes[i] < 0x10) Serial.write('0');
        Serial.print(Hexa_Val.bytes[i], HEX);
        Serial.write(' ');
    }
    Serial.println(); 

    delay(500);
}
+ solution with loop.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}

If you don't want to write 8 nested conditions (which is kind of cumbersome), you can instead loop over the array backwards, incrementing each element in turn, and break out of the loop as soon as an increment does not overflow:

for (int i = 7; i >= 0; i--) {
    if (++Hexa_Val[i] != 0) break;
}
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

There are multiple problems here:

if(Hexa_Val[7] = 0xFF)

First of all, = is the assignment operator. You are setting Hexa_Val[7] to 0xFF. If you want to compare for equality, you should use ==.

Second problem, The value 0xff will never be displayed, because you are replacing it by zero. This byte will then count 0xfd, 0xfe, 0x00, skipping 0xff. If you want to count 0xff, the condition should be Hexa_Val[7] == 0, i.e. increment Hexa_Val[6] only after Hexa_Val[7] has overflowed.

Third problem, your second if test comes too late. At this point Hexa_Val[7] has already been reset. You should test for Hexa_Val[6] overflowing only within the previous test.

With all that fixed:

Hexa_Val[7] = Hexa_Val[7] + 1;
if (Hexa_Val[7] == 0) {
  Hexa_Val[6] = Hexa_Val[6] + 1;
  if (Hexa_Val[6] == 0) {
    Hexa_Val[5] = Hexa_Val[5] + 1;
  }
}