If statement doesn't work with array

I have the code below.

In setup i turn a integer value into binary array.

In loop:

And I want to blink led according to number my led doesn't turn off. only on it is.

Please help thank you.

char binary[9];
char* string;
//---------------var end-------------
void setup()
  for(byte i = 0; i < 7; i++){

  byte someValue = 13; //For this example, lets convert the number 20
  binary[9] = {0}; //This is where the binary representation will be stored
  someValue += 128; //Adding 128 so that there will always be 8 digits in the string
  itoa(someValue,binary,2); //Conver someValue to a string using base 2 and save it in the array named binary
  string = binary + 1; //get rid of the most significant digit as you only want 7 bits

  Serial.println(string); //print out our string.
  for(byte i = 0; i < 7; i++){
    digitalWrite(i+13,string[i] - '0'); //write to the pin (the - '0' converts the bit of the string to HIGH or LOW)

void loop()
for(byte i = 0; i < 7; i++){


else if(string[i]==0){digitalWrite(D0,LOW);}





Post your sketch in so called code tags "</>" to see how we can help.
Does the compiler likes the sketch?

I doubt a String can be compared with a numeric constant the way you’re thinking.

Sorry im new i fixed it

My compiler doesn´t like the posted sketch.


could you explain my mistake im new :slight_smile:
and what should i do to fix it

You have a nine element array, with elements numbered 0...8 inclusive.
There is no element 9.

Edit: what does "string" point to?

Edit:edit: found it :frowning:

...but there are no Strings.

once you've tended to the suggestions you can try to work with the following ideas to do the thing.

int x = 0b100101;
// First bit
(x >> 0) % 2; // 1
// Second bit
(x >> 1) % 2; // 0
// Third bit
(x >> 2) % 2; // 1

and so on and so forth

another way

int value = 26;
for (int i = 0; i < 8; i++) {
    if ((value >> i) & 0x01) {
        // Bit i is 1

    else {
        // Bit i is 0


This will not work, you have chars in the array '0' and '1', not the numbers 0 and 1.

The if of the else is not necessary, there are only two possibilities.


Close enough… a char certainly could contain a non-ASCII character, but there’s probably a better way to do it -

You’ll get there.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.