Convert Decimal to Binary and write to pins.

Howdy,

I am working on a large project, and I need a little bit of guidance. I have a 7-segment display hooked up to an IC that has 4 inputs, and reads those as a binary value consisting of 4 bits. It then outputs the value to the 7-segment display in the proper format to display a number.

In my program, I have a variable holding a number between 0 and 6. Is there a simple way to convert this number to its respective binary value and then output it to pins 11-14?

For example, if I had the number 6 in a variable, I want it to convert it to binary (0110) and then output 0 to pin11, 1 to pin12, 1 to pin13, and 0 to pin14.

Is there a simple way to do this?

Any and all help is appreciated.

{
    int var = get_value_between_zero_and_six();
    digitalWrite(11, HIGH && (var & B00001000));
    digitalWrite(12, HIGH && (var & B00000100));
    digitalWrite(13, HIGH && (var & B00000010));
    digitalWrite(14, HIGH && (var & B00000001));
}

I'm assuming you want the most significant bit to go to digital pin 11. (Your example value of 6 is a palindrome in binary.) Also, note that digital pin 14 is labeled as analog pin 0 on the Arduino board. Nothing wrong with that, but just seemed an unusual choice.

There are many ways to calculate the second argument to digitalWrite(). I chose the form (1 && (var & bitmask)), which evaluates to 1 or 0 appropriately, without shifting operations.

1 Like

Thank you, that is helpful.

I'm confused about the inputs, however.

Currently, I have 3 buttons, a 7 segment, an LED, and a robotic speed controller hooked up to a motor. I need 3 digital inputs and 6 digital outputs.

I'm confused by the pin-mapping since I'm referencing this:

If it makes a difference, I'm using an Arduino Diecimila USB controller.

Any suggestions of what pins to use?

All of the Arduino library software uses the "pin numbers" that are stenciled in white paint onto the board (shown in red in your diagram), not the ATmega168 chip positions (shown in black in your diagram).

So if you were assuming the black 11~14 positions, the software should actually use 5~8. The Arduino platform is becoming compatible with more than one type of microcontroller model, so using the physical chip positions varies, but the Arduino functions remain consistent.

Alright, that helps, thank you.

Here's another issue, though.

I have a rotary encoder attached to a motor in this program. I want it to continue to rotate the motor until it reaches a certain pre-defined value. I have read up on the rotary encoder setups for the arduino, and I understand them, but I have a question about interrupts.

While it seems to make them more efficient, will it effect my code if it works as follows:

while( varA != varB)
{
//Send analog pulse to speed-controller
}

Then, the interrupts would force the program to use the predefined functions specified in the program. In those functions, I increment or decrement varA based on the direction of the encoder. "varB" is a value that I am trying to reach for the encoder. Would it still be able to compare varA and varB while the encoder functions are running, however?

I can explain this better if it's not clear.

I can explain this better if it's not clear.

That might be helpful.

Normally the strategy of using interrupt routines is to do as little as possible inside the interrupt routine, doing it inline and not calling any subroutine functions, just increment or decrement a global variable or flag variable that the main sketch can test or compare and take action on. The idea is to take up as few processor cycles as possible to lessen the risk of missing future interrupt signals while servicing a previous interrupt signal or effecting other waitinf interrupt functions. While a ISR routine is being service all other interrupts are disabled until the ISR routine completes.

In the case of interrupting encoder signals the minimum required task is to update (by incrementing or decrementing) a global 'step count' variable and a global direction flag variable inside the ISR routine and then return to the main sketch. Then the main sketch can act on these changed global variables as it loops through the main routine.

That make sense?

Lefty

That does help, Lefty; I opted to just use the simple rotary code since interrupts weren't what I needed.

Alright, I think I have my code working, but I'm having an issue hooking up the final product. I can't figure out what pinouts to use. Here's what I have:


It's hooked up to a Diecimila USB, and I have the following pins specified:

0 and 1 - rotary encoder
2, 3, and 4 - buttons
5, 6, 7, 8 - 7-segment display
11 - motor (PWM)

If my understanding is correct, each of those numbers refers to the pins labeled "Digital I/O" on the board, so 0 and 1 are Digital I/O 0 and 1. Is this correct?

If my understanding is correct, each of those numbers refers to the pins labeled "Digital I/O" on the board, so 0 and 1 are Digital I/O 0 and 1. Is this correct?

That is correct. By the way generally people avoid using digital pins 0 & 1 as they are hardwired to the USB serial link used to upload new programs or send serial data to a PC application.

Lefty