Decimal to binary conversion with LED

Hey folks. My task is to create a decimal (0-15) to binary converter using some loops. I did manage to print double result, but I can not light the LEDs. Basically the idea is like that:

  1. I set the pins as output in Setup.

  2. In Void I do a loop that print the result.

  • I also plan to light the LEDs here, so I store the binary result in array (BinaryList[3][14)
    However I can not do that. My plan is to check in double for loop, every element of BinaryList, and if it is ==1 than I light a pin, else i keep it LOW. After delay 1000, turn all off. Could you please help me on that? I did that in several places, but it seems to do nothing. Also any recommendations on the code will be very helpful! Thank you!
  1. Finally I do a conversion in my function called decToBinary();

Here is my setup:


I also manage to print a correct result.

Code:

int BinaryList[3][14]; //List with all of the binary numbers
int decNumber;

void setup() {
   Serial.begin(9600);
   for (int x=2;x<6;x++) //Set output pins;
    {
      pinMode (x, OUTPUT);
    }
}


void loop() {
    for (int decCount=0;decCount<16;decCount++) { 
    decNumber = decCount;           //set the dec number
    decToBinary();
     Serial.println ("");
     Serial.print ("Decimal: ");
     Serial.println (decCount);
     Serial.print ("Double: ");
       for(int y=0;y<4;y++) {        //adding arry to array list, and printing all of the values
       BinaryList[y][decCount]=Binary[y];
       Serial.print (BinaryList[y][decCount]);     
     }
    Serial.println ("");
    Serial.print ("------------");
   }
}


void decToBinary () {
int remainder;
 for (int i=3;i>=0 ;i--) {
    remainder=decNumber%2; //determination of remainder
    decNumber=decNumber/2; //division by 2
    Binary[i]=remainder; //an array is getting the value of the decimal
 }
delay(1000);
}```

Unless there is more to the requirement than I understand then you are making things more complicated than they need to be

Take a look at this sketch

byte count = 0;

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

void loop()
{
  Serial.print("count:");
  Serial.print(count);
  Serial.print("    ");
  for (int bit = 3; bit >= 0; bit--)
  {
    byte bitValue = bitRead(count, bit);
    Serial.print(bitValue);
    Serial.print(" ");
  }
  Serial.println();
  delay(1000);
  count++;
  if (count > 15)
  {
    count = 0;
  }
}

To turn LEDs on and off, put their pin numbers in an array, use the value of the bit variable as the index to that array and bitValue to control the state of the LED for that bit

1 Like

Thank you UKHeli! This is exactly what I am trying, and miserably failing to do. I made it more complicated intentionally, just to challenge my self if I will be able to think out how to do the conversion with my function, but I got stuck on the LEDs solution.

byte count = 0;
const byte ledPins[] = {9, 6, 5, 3};

void setup()
{
  for (int p = 0; p < 4; p++)
  {
    pinMode(ledPins[p], OUTPUT);
    digitalWrite(ledPins[p], HIGH); //initial LED state
  }
  Serial.begin(115200);
}

void loop()
{
  Serial.print(count);
  Serial.print(":   ");
  for (int bit = 3; bit >= 0; bit--)
  {
    byte bitValue = bitRead(count, bit);
    Serial.print(bitValue);
    Serial.print(" ");
    digitalWrite(ledPins[bit], !bitValue);
  }
  Serial.println();
  delay(1000);
  count++;
  if (count > 15)
  {
    count = 0;
  }
}

You will need to change the logic of the output to the LEDs depending on whether your LEDs are active HIGH or active LOW

1 Like

I understand, and thanks again. But the logic here is a bit different than what I got. For example my function do not return a value, that I can assign to bitValue instead it change existing variable. Maybe that is a mistake, but I messed the things with "return". Also my variable is an array of 4 that holds the binary number, but it is actually an int. Maybe it is not the right way to do things. But still is it not possible to take the values from list of array (BinaryList[3][14]), compare them, and set lights on when value is 1, and off when value is 0?

Start by getting the array declared properly

int BinaryList[3][14]; //List with all of the binary numbers

This array has 3 rows, each of 14 values but I thought that you wanted to count up to 15

Personally I would turn the array the other way round and have 15 rows, each of 3 values and declare it as a byte array so as not to waste even more memory that you already are. Ai would also populate the array in setup(), or a function called from setup because it only needs to be done once

To show the bit values on the LEDs, use a for loop (or the loop() function) to count from 0 to 15, use the current count as the index to the first level of the array and a for loop to get the value of each bit in that number and use that value to control the LED by using the inner for loop variable as the index to the associated LED pin

Having said all that, there is a simpler way as you have seen

I think that array count from 0, so if one needs array of 15 elements should actually declare array[14]. Also I do try with loop function, yes, but something I do wrong. I will post example, if I get to something useful.

Sorry, but you are wrong

If you declare an array with 14 elements, as you have, then they will be numbered 0 to 13 and the array can only hold 14 values

1 Like

OK, thanks! I have moved the population on void set up, and will declare the arrays with correct values.

Hey UKHeliBob, your suggestions made the project successful. It works! :slight_smile: Thank you, all issues came from the bad array declaration.

Here is my last code:

int Binary[4] = {0, 0, 0, 0};
int BinaryList[4][16]; //List with all of the binary numbers
int decNumber;


void setup() {
  Serial.begin(9600);
  for (int x=2;x<6;x++) //Set output pins;
   {
     pinMode (x, OUTPUT);
     digitalWrite(x, LOW);
   }
   for (int decCount=0;decCount<16;decCount++) { 
   decNumber = decCount;           //set the dec number
   decToBinary();
    Serial.println ("");
    Serial.print ("Decimal: ");
    Serial.println (decCount);
    Serial.print ("Double: ");
      for(int y=0;y<4;y++) {        //adding arry to array list, and printing double value
      BinaryList[y][decCount]=Binary[y];
      Serial.print (BinaryList[y][decCount]);
     }
 Serial.println ("");
 Serial.print ("------------");
   }
}


void loop() {
   for (int blinks=0;blinks<4;blinks++) { //Blink
     for (int i=2;i<6;i++) { 
     delay(50);
     digitalWrite(i, HIGH);
     } 
     delay(50); 
     for (int i=2;i<6;i++) { 
     digitalWrite(i, LOW);
     } 
}
delay(1000); 
for (int B=0;B<16;B++) {  //Show binary number on LEDs, and print
   for(int y=0;y<4;y++) {       
   Serial.print (BinaryList[y][B]);
     if (BinaryList[y][B]==1){
     digitalWrite(y+2, HIGH); 
     } 
     else {
       digitalWrite(y+2, LOW);
       }
   } Serial.println ("");
   delay(2000); 
 }
}


void decToBinary () {
int remainder;
for (int i=3;i>=0 ;i--) {
   remainder=decNumber%2; //determination of remainder
   decNumber=decNumber/2; //division by 2
   Binary[i]=remainder; //an array is getting the value of the decimal
}
}
1 Like

Where is the Binary array declared in your code ?

Sorry It was a formatting error from me posting the code. Booth of the arrays are on top. The first one was not visible.

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