Decimal to Binary display

So I'm working on a way to display what mode I have selected on 9 individual LEDs. I want to convert the number to binary and then write it on the LEDs. I've been working on this for literally hours and there just has to be an easy way to do this. Heres what I have so far, its really badly worded I think and it doesn't work, mostly because of the mixed data types Im using.

    int pinArray[] = {2,3,4,5,6,7,8,12,13};    
        
         int binaryArray0[] = {0,0,0,0,0,0,0,0,0};
        int binaryArray1[] = {0,0,0,0,0,0,0,0,1};  
        int binaryArray2[] = {0,0,0,0,0,0,0,1,0};
        int binaryArray3[] = {0,0,0,0,0,0,0,1,1};
        int binaryArray4[] = {0,0,0,0,0,0,1,0,0};
        int binaryArray5[] = {0,0,0,0,0,0,1,0,1};
        int binaryArray6[] = {0,0,0,0,0,0,1,1,0};
        int binaryArray7[] = {0,0,0,0,0,0,1,1,1};
        int binaryArray8[] = {0,0,0,0,0,1,0,0,0};
        int binaryArray9[] = {0,0,0,0,0,1,0,0,1};
        int binaryArray10[] = {0,0,0,0,0,1,0,1,0};
        int binaryArray11[] = {0,0,0,0,0,1,0,1,1};
        int binaryArray12[] = {0,0,0,0,0,1,1,0,0};
        int binaryArray13[] = {0,0,0,0,0,1,1,0,1};
        int binaryArray14[] = {0,0,0,0,0,1,1,1,0};
        int binaryArray15[] = {0,0,0,0,0,1,1,1,1};
        int binaryArray16[] = {0,0,0,0,1,0,0,0,0};  
        int binaryArray17[] = {0,0,0,0,1,0,0,0,1};
        int binaryArray18[] = {0,0,0,0,1,0,0,1,0};
        int binaryArray19[] = {0,0,0,0,1,0,0,1,1};
        int binaryArray20[] = {0,0,0,0,1,0,1,0,0};
        int binaryArray21[] = {0,0,0,0,1,0,1,0,1};
        int binaryArray22[] = {0,0,0,0,1,0,1,1,0};
        int binaryArray23[] = {0,0,0,0,1,0,1,1,1};
        int binaryArray24[] = {0,0,0,0,1,1,0,0,0};
        int binaryArray25[] = {0,0,0,0,1,1,0,0,1};
        int binaryArray26[] = {0,0,0,0,1,1,0,1,0};
        int binaryArray27[] = {0,0,0,0,1,1,0,1,1};
        int binaryArray28[] = {0,0,0,0,1,1,1,0,0};
        int binaryArray29[] = {0,0,0,0,1,1,1,0,1};
        int binaryArray30[] = {0,0,0,0,1,1,1,1,0};
        int binaryArray31[] = {0,0,0,0,1,1,1,1,1};
        int binaryArray32[] = {0,0,0,1,0,0,0,0,0};
        int binaryArray33[] = {0,0,0,1,0,0,0,0,1};
        int binaryArray34[] = {0,0,0,1,0,0,0,1,0};
        int binaryArray35[] = {0,0,0,1,0,0,0,1,1};
        int binaryArray36[] = {0,0,0,1,0,0,1,0,0};       
        
void setup(){
  Serial.begin(9600);
  
  for (int count = 0; count <8; count++){
    pinMode(pinArray[count],OUTPUT);
  }
}

int BINwrite(int a){
  
  for (int count = 0; count < 9; count++){
    digitalWrite(pinArray[count], LOW);
  }
    
      String string1 = "binaryArray";
      String string2 = a;
      String binArray = string1 + string2;
      
      if(binArray[0] == 1) digitalWrite(pinArray[0], HIGH);
      if(binArray[1] == 1) digitalWrite(pinArray[1], HIGH);
      if(binArray[2] == 1) digitalWrite(pinArray[2], HIGH);
      if(binArray[3] == 1) digitalWrite(pinArray[3], HIGH);
      if(binArray[4] == 1) digitalWrite(pinArray[4], HIGH);
      if(binArray[5] == 1) digitalWrite(pinArray[5], HIGH);
      if(binArray[6] == 1) digitalWrite(pinArray[6], HIGH);
      if(binArray[7] == 1) digitalWrite(pinArray[7], HIGH);
      if(binArray[8] == 1) digitalWrite(pinArray[8], HIGH);
      
      delay(2);
         
}
    
void loop(){
  for (int count = 0; count <37; count++){
BINwrite(count);
  }
}

This is all just a test version and once I get it figured out I will put it in the rest of the code. Im trying to make a function that does this for me so that I can use it a bunch of times. I have the loop just running the function with different values for testing purposes. The adding of the strings bit works great and spits out the right name for the array variables but it doesn't work when it goes through my if statements. I'm guessing its because its looking at it like a string and not a variable name so its just not analyzing it right. I've tries a bunch of different methods and just not having luck, this was the most promising.

I guess another way you could do it would be to give it a String of values (like 00010100) and have it look at each individual character and assign it to a variable or maybe just use it in the if statement to skip a step.

I figured out a way to do it but its very bulky and slimming it down would be good.

    int pinArray[] = {2,3,4,5,6,7,8,12,13};    
        
      
void setup(){
  Serial.begin(9600);
  
  for (int count = 0; count <8; count++){
    pinMode(pinArray[count],OUTPUT);
  }
}

int BINwrite(int print1){
  
  int binaryArray[] = {0,0,0,0,0,0,0,0,0};
  //  int binaryArray[8];
  

  if (print1 == 1)  binaryArray[8] = 1;  
  if (print1 == 2) binaryArray[7] = 1;        //   {0,0,0,0,0,0,0,1,0};
  if (print1 == 3){
  binaryArray[7] = 1;        //   {0,0,0,0,0,0,0,1,1};
  binaryArray[8] = 1;} 
  if (print1 == 4) binaryArray[6] = 1;        //   {0,0,0,0,0,0,1,0,0};
  if (print1 == 5){
  binaryArray[6] = 1;        //   {0,0,0,0,0,0,1,0,1};
  binaryArray[8] = 1;} 
  if (print1 == 6){
  binaryArray[6] = 1;        //   {0,0,0,0,0,0,1,1,0};
  binaryArray[7] = 1;}
  if (print1 == 7){
  binaryArray[6] = 1;        //   {0,0,0,0,0,0,1,1,1};
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 8) binaryArray[5] = 1;        //   {0,0,0,0,0,1,0,0,0};
  if (print1 == 9){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,0,0,1};
  binaryArray[8] = 1;}
  if (print1 == 10){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,0,1,0};
  binaryArray[7] = 1;}
  if (print1 == 11){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,0,1,1};
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 12){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,1,0,0};
  binaryArray[6] = 1;}
  if (print1 == 13){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,1,0,1};
  binaryArray[6] = 1;
  binaryArray[8] = 1;}
  if (print1 == 14){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,1,1,0};
  binaryArray[6] = 1;
  binaryArray[7] = 1;}
  if (print1 == 15){
  binaryArray[5] = 1;        //   {0,0,0,0,0,1,1,1,1};
  binaryArray[6] = 1;
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 16) binaryArray[4] = 1;        //   {0,0,0,0,1,0,0,0,0};  
  if (print1 == 17){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,0,0,1};
  binaryArray[8] = 1;}
  if (print1 == 18){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,0,1,0};
  binaryArray[7] = 1;}
  if (print1 == 19){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,0,1,1};
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 20){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,1,0,0};
  binaryArray[6] = 1;}
  if (print1 == 21){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,1,0,1};
  binaryArray[6] = 1;
  binaryArray[8] = 1;}
  if (print1 == 22){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,1,1,0};
  binaryArray[6] = 1;
  binaryArray[7] = 1;}
  if (print1 == 23){
  binaryArray[4] = 1;        //   {0,0,0,0,1,0,1,1,1};
  binaryArray[6] = 1;
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 24){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,0,0,0};
  binaryArray[5] = 1;}
  if (print1 == 25){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,0,0,1};
  binaryArray[5] = 1;
  binaryArray[8] = 1;}
  if (print1 == 26){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,0,1,0};
  binaryArray[5] = 1;
  binaryArray[7] = 1;}
  if (print1 == 27){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,0,1,1};
  binaryArray[5] = 1;
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 28){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,1,0,0};
  binaryArray[5] = 1;
  binaryArray[6] = 1;}
  if (print1 == 29){        //   {0,0,0,0,1,1,1,0,1};
  binaryArray[4] = 1;
  binaryArray[5] = 1;
  binaryArray[6] = 1;
  binaryArray[8] = 1;}
  if (print1 == 30){       //   {0,0,0,0,1,1,1,1,0};
  binaryArray[4] = 1;
  binaryArray[5] = 1;
  binaryArray[6] = 1;
  binaryArray[7] = 1;}
  if (print1 == 31){
  binaryArray[4] = 1;        //   {0,0,0,0,1,1,1,1,1};
  binaryArray[5] = 1;
  binaryArray[6] = 1;
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 32) binaryArray[3] = 1;        //   {0,0,0,1,0,0,0,0,0};
  if (print1 == 33){
  binaryArray[3] = 1;        //   {0,0,0,1,0,0,0,0,1};
  binaryArray[8] = 1;}
  if (print1 == 34){
  binaryArray[3] = 1;        //   {0,0,0,1,0,0,0,1,0};
  binaryArray[7] = 1;}
  if (print1 == 35){
  binaryArray[3] = 1;        //   {0,0,0,1,0,0,0,1,1};
  binaryArray[7] = 1;
  binaryArray[8] = 1;}
  if (print1 == 36){
  binaryArray[3] = 1;        //   {0,0,0,1,0,0,1,0,0};   
  binaryArray[6] = 1;}
 
  
  for (int count = 0; count < 9; count++){
  if (binaryArray[count] == 1) digitalWrite(pinArray[count], HIGH);
  else digitalWrite(pinArray[count], LOW);
}
      
    //  delay(50);
         
}
    
void loop(){

BINwrite(analogRead(2)/28);

}

any ideas?

I'm puzzled - the number is held in the AVR in binary - why bother converting it?

Maybe the OP didn't know that, how about giving him an example?

The way I'd do it after a quick look at the ref is:

byte b = byte(number);
for (int i = 0; i < 7; i++) {
    int bit = bitRead(byte, i);
    digitalWrite(pins[i], bit);
  }

Where pins is the array of output pins.

2 Likes

I would use a shift register for the job. There is a good tutorial on using the 74HC595 here: http://www.arduino.cc/en/Tutorial/ShiftOut. Using the code here: http://arduino.cc/en/Tutorial/ShftOut11 you can count from 0 to 255 on 8 LEDs.
After you have figured out why it works, you can begin to modify it for your own needs.

Onions.

go here

http://arduino.cc/en/Reference/BitRead

then kick yourself for writing out all that code xD

Yeah the shift reg is a good alternative if you need to free up some pins.

I started off with a 7segment display, which I wired directly, then modified it to use a shift register, then modified to use transistors to switch all the digits. Learning stuff incrementally like that is a bit easier if you're new to electronics and coding.

@Prawnstar, I posted that a bit further up :stuck_out_tongue:

Thanks so much for the help guys. I'll try out that code when I get home.

The reason I'm not trying to free up pins with a shift register is that the project is a very large led blinder. I'm putting the status leds in parallel with the pnp transistors that go to the big common anode leds. I don't want to display the program number on the big leds so I'll do a while loop that loop that looks at a toggle switch. If the switch is on it will digital write the pins controlling the negative color side of my leds to low so that only the status leds come on. While it's running that loop you an change the mode that it's in with 2 potentiometers and when you get it to the right one switch the toggle switch off again so it starts the program you've selected.

Hope this makes sense, I'm running low on pins for my project so I just decidied that the main leds and status leds could be friends and share. And yes I know that the status leds will be doing the samething as the main leds when it's not in the program selection loop and I'm cool with that. Writing all of that code was so painful, I just knew there had to be a better way but I couldn't think of one.

I just uploaded and tested the code from tomm and it works perfectly. Thanks so much you saved me a couple thousand bytes of space. The fact that I wrote pages of code to do something so easy annoys me