Counting Binaries on LED's

First Post, Hello! Thanks for the great community and reference material. Have fun with this one.

OK, rip this code apart. For most of you guru’s, the errors should be obvious, but I’m still learning, so be harsh… err gentle. Searching for the issue is difficult because a) it’s a logic issue and I’m not sure what to search for there, and b) There are a LOT of results for the commands, so sorting them is difficult.

LED’s are on Pins 2-9. I want to display a binary number using ON/OFF settings on each LED. 0-255.

I’ve had many iterations of this program, sometimes all the lights are on, sometimes some of them, right now none of them.

Also, the number displays correctly as the Decimal version, Binary equivalent, and then Character Array of the binary in the Serial.print commands, for the first loop. Second time around, even with an initialization loop, the numbers remain 1’s, with the NULL character near the front:

One issue is, on the second loop, you can see the array is populating “left to right”. I need it to populate Right to left. As in the 1’s digit is always the 1’s digit/LED.

The for loop that has the lengthof function in it probably is being used wrong, but since right now I’m not even getting any LED’s to turn on (I was using if (num[ x ]) {}, that turned them on, but never turned them off, now they don’t turn on), I figured I’d cry for help now.

/* Binary Counter 
Counts from 0 - 255
Displayed on 8 LED's on pins 2-9
*/

char num[9];
String buf;
char temp[9];
int x;

void setup()
{
  Serial.begin(9600);
  
  for(x=2;x<=9;x++)
  {
    pinMode(x,OUTPUT);
  }
  
  for(x=0;x<=8;x++)
  {
    num[x]=0;
  }
  
    
}

void loop()
{
  for(int i=0; i<256; i++)
  {
    buf = String(i,BIN);
    buf.toCharArray(temp,9);
    
    for (x=0; x <= sizeof(temp)-1; x++)
    {
      num[7-x]=temp[7-x];
    }
    
    for(x=0;x<=7;x++)
    {
      if (num[x]==1)
      {
        digitalWrite(x+2,HIGH);
      }
      else if (num[x]==0)
      {
        digitalWrite(x+2,LOW);
      }
    }    
    
    //DEBUG SERIAL PRINT
    Serial.print(i);
    Serial.print("--");
    Serial.print(buf);
    Serial.print("--");
    for (x=0; x<=8; x++)
    {
      Serial.print(num[x]);
    }
    Serial.print("\n ");
    //END DEBUG
    
    delay(100);
    
     for(x=0;x<=7;x++)
     {
       num[x]=0;
     }
      
    }
   
 }

Not sure what you need but:

for (x=0; x<=8; x++)

reversing

for (x=8; x>=0; x–)

First mistake is:

if (num[x]==1) 
//should read 
if (num[x]=='1')

Since num[ x ] is holding the character '0' or '1' it's never going to = 1, it's either 48 (for '0') or 49 ('1');

All good answers!

The Reversing of the for loop was next on my trial and error list, but as I was having LED issues, i said forget it and came here.

And yes, it has been 8-10 years since I last did any coding work, totally forgot I was dealing with a character array and not integers!!

MUahahaha.

Thanks for the help folks! Works great now.

Been teaching a friend how to count/think in Bases other than Decimal. Going to use this as a visual aid to show how binary progresses quickly, much easier than pen and paper.

It was quite fun, after he seemed to grasp bases 2-10, to see his brain vapor lock when I asked him to write the value 10 (decimal) in Base 11.

Tell him to convert this binary number to HEX

110000001111111111101110 time

LarryD:
Tell him to convert this binary number to HEX

110000001111111111101110 time

Ha! Had to go to wolframalpha for that one!

No… not this guy… too bad there’s not an ‘r’ in Hex.

And no, I don’t feel like figuring out which base does, and converting it. :smiley:

Side question. Can I run all 8 LED’s through one 330k resistor ( I don’t have any 220’s) to ground, or does each one need it’s own? Electronics are my weak spot here, which is saying a lot looking at my initial code up there!

For those who care, here’s the finished code, I figured somebody else might use it for a teaching tool or example. There is a commented out for() loop that allows for counting down too. I got tired of editing it each way. Yes, I could add a switch to to do that, but it was late.

/* Binary Counter 
Counts from 0 - 255 or 255-0
Displayed on 8 LED's on pins 2-9, with 1's digit on Pin 9, and 128 digit on pin2
Pot on analogIn 0 to adjust delay
*/

char num[9];
char temp[9];
int x;
//int y;
int mindel=100;  // Min and Max delay values
int maxdel=1000;

void setup()
{
  Serial.begin(9600);
  pinMode(0,INPUT);
  
  for(x=2;x<=9;x++)  // Initialize pinmodes for OUTPUT
  {
    pinMode(x,OUTPUT);
  }
  
  for(x=0;x<=7;x++) //Initialize num Array and add NULL to end of array.
  {
    num[x]=0;
  }
  
  num[8]=NULL;
  
    
}

void loop()
{
  //for(int i=255; i>=0; i--) // Count from 255 - 0
  for(int i=0; i<=255; i++) // Count from 0 - 255, COMMENT ONE OF THESE OUT
  {
    String buf = String(i,BIN); // Convert int to string, in binary
    buf.toCharArray(temp,9); // convert string to char array
    
   for(int y = 8-buf.length(), x = 0 ; x < buf.length() ; x++, y++) //populate num array with 1's digit in last spot
    {
      num[y]=temp[x];
    }
     
   for(x=0;x<=7;x++)  //Step through the num array to turn LEDs on/off
    {
      if (num[x]=='1')
      {
        digitalWrite(x+2,HIGH);
      }
      else 
      {
        digitalWrite(x+2,LOW);
      }
    }    
    
    /*  DEBUG SERIAL PRINT
    Serial.print(i);
    Serial.print("--");
    Serial.print(buf);
    Serial.print("--");
    Serial.print(buf.length());
    Serial.print("--");
    Serial.print(temp);
    Serial.print("--");
    Serial.print(sizeof(temp));
    Serial.print("--");
    for (x=0; x<=8; x++)
    {
      Serial.print(num[x]);
    }
    Serial.print("--");
    Serial.print(sizeof(num)-1);
    Serial.print("\n");
    //END DEBUG */
    
    delay(map(analogRead(0),0,1024,mindel,maxdel));  //Read pot to adjust delay
     
    for(x=2;x<=9;x++) // reset num array and turn off the lights.  
     {
       digitalWrite(x,LOW);
       num[x-2]=NULL;
     }     
   }
  
  delay(maxdel*4); 
   
 }

Side question. Can I run all 8 LED's through one 330k resistor ( I don't have any 220's) to ground, or does each one need it's own? Electronics are my weak spot here, which is saying a lot looking at my initial code up there!

You mean 330 ohm not 330K.
And no, you need one resistor for each LED.

LarryD:
You mean 330 ohm not 330K.

Well that would explain why they aren't lighting up!

Yes, a typo there.

Thank you.