1

I have an array of hexadecimal values as given below.

byte Hexa_Val[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

I want to increase this array from the end. As in, go from {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} all the way to {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, and then increase the value next to it to {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}, and continue. The increment should go as shown below.

{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
.
.
.
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}
.
.
.
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}
{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}
.
.
.
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}

The code I am came up with is

byte Hexa_Val[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void setup() { Serial.begin(115200); }

void loop() {

Hexa_Val[7] = Hexa_Val[7] + 1;

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

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

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

if((Hexa_Val[7] = 0xFF) && (Hexa_Val[6] = 0xFF) && (Hexa_Val[5] = 0xFF) && (Hexa_Val[4] = 0xFF)){ Hexa_Val[3] = Hexa_Val[3] + 1; Hexa_Val[7] = 0x00; Hexa_Val[6] = 0x00; Hexa_Val[5] = 0x00; Hexa_Val[4] = 0x00;
}

if((Hexa_Val[7] = 0xFF) && (Hexa_Val[6] = 0xFF) && (Hexa_Val[5] = 0xFF) && (Hexa_Val[4] = 0xFF) && (Hexa_Val[3] = 0xFF)){ Hexa_Val[2] = Hexa_Val[2] + 1; Hexa_Val[7] = 0x00; Hexa_Val[6] = 0x00; Hexa_Val[5] = 0x00; Hexa_Val[4] = 0x00; Hexa_Val[3] = 0x00;
}

if((Hexa_Val[7] = 0xFF) && (Hexa_Val[6] = 0xFF) && (Hexa_Val[5] = 0xFF) && (Hexa_Val[4] = 0xFF) && (Hexa_Val[3] = 0xFF) && (Hexa_Val[2] = 0xFF)){ Hexa_Val[1] = Hexa_Val[1] + 1; Hexa_Val[7] = 0x00; Hexa_Val[6] = 0x00; Hexa_Val[5] = 0x00; Hexa_Val[4] = 0x00; Hexa_Val[3] = 0x00; Hexa_Val[2] = 0x00;
}

if((Hexa_Val[7] = 0xFF) && (Hexa_Val[6] = 0xFF) && (Hexa_Val[5] = 0xFF) && (Hexa_Val[4] = 0xFF) && (Hexa_Val[3] = 0xFF) && (Hexa_Val[2] = 0xFF) && (Hexa_Val[1] = 0xFF)){ Hexa_Val[0] = Hexa_Val[0] + 1; Hexa_Val[7] = 0x00; Hexa_Val[6] = 0x00; Hexa_Val[5] = 0x00; Hexa_Val[4] = 0x00; Hexa_Val[3] = 0x00; Hexa_Val[2] = 0x00; Hexa_Val[1] = 0x00;
}

if((Hexa_Val[7] = 0xFF) && (Hexa_Val[6] = 0xFF) && (Hexa_Val[5] = 0xFF) && (Hexa_Val[4] = 0xFF) && (Hexa_Val[3] = 0xFF) && (Hexa_Val[2] = 0xFF) && (Hexa_Val[1] = 0xFF) && (Hexa_Val[0] = 0xFF)){ Hexa_Val[7] = 0x00; Hexa_Val[6] = 0x00; Hexa_Val[5] = 0x00; Hexa_Val[4] = 0x00; Hexa_Val[3] = 0x00; Hexa_Val[2] = 0x00; Hexa_Val[1] = 0x00; Hexa_Val[0] = 0x00;
}

//to find out what I am sending for( byte a=0; a<8; a++ ) { Serial.print(Hexa_Val[a], HEX); } Serial.println();

delay(500);

}

But it just gives me zeros in the serial monitor. If I just leave the first two IF statements

byte Hexa_Val[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void setup() { Serial.begin(115200); }

void loop() {

Hexa_Val[7] = Hexa_Val[7] + 1;

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

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

//to find out what I am sending for( byte a=0; a<8; a++ ) { Serial.print(Hexa_Val[a], HEX); } Serial.println();

delay(500);

}

The increment will start from the third value on the right. Something like

00000200
00000300
00000400
00000500
00000600
00000700

How do I get it to increase as I mentioned at the beginning? Why does the code recognize 0xFF as a zero?

C Vith
  • 133
  • 1
  • 7

1 Answers1

2

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 &gt;= 0; i--) {
    if (Hexa_Val.bytes[i] &lt; 0x10) Serial.write('0');
    Serial.print(Hexa_Val.bytes[i], HEX);
    Serial.write(' ');
}
Serial.println(); 

delay(500);

}

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81