How to show binary with 6 leds?

Hi, I am a beginner and I need your help. I want to display binary number (for example the 000001 will show the last led on and the rest are off) and increment the number every second. I want to do this while I am connecting 6 Leds to the analog pins A0 to A5. I have already built the circuit. Now, I am left with the coding part. how can I fix the code below so that I am able to do so ( I am using this code because I want to make use of direct port manipulation.


void setup() {

  DDRB = DDRB | B00100000; 
}

void loop() {


 
  PINB = B00100000; 
  PINB = B00100000;
  
  delay(1);
}

Why 26? 6 bits can represent a number between 0 and 63.

What does that mean?

The analog input pins on an Uno (A0-A5) are digital pins with the special function of analog input. Use them like you would use any digital pins. You can use pin numbers 14-19 instead of A0-A5.

2 Likes

Every kind of numerical data, as well as text, is store as binary in the memory. Integers, floats, ASCII... al stored binary. Your writing "binary" makes no sense to us, only tells You don't really know the word You use.

Don't ever make connections to any circuit being on power. That has blown up many circuits.

Describe the "big picture", what the project is supposed to do. Drop the idea telling how it will be done.

1 Like

You can make a "mask" to read (evaluate) one bit at a time. Start with a value of 1 (which is also one in binary).

Then use bitwise and with your counting variable, and it will either be zero or not-zero depending on the binary value of bit-zero, and you can turn the associated LED on or off.

Next, bit-shift left the mask and bitwise and again to check the value of bit 1, and update that LED.

...etc.

26?

With 6-bits you can count to 63 decimal.
111111 binary = 63 decimal.
(You can convert with the Windows Calculator in Programmer Mode.)

2 Likes

oh i see thanks

Use multiple bitRead()s to get the status of the bits in your counter.  digitalWrite() those results to the corresponding LED output.

1 Like

Hi, I am a beginner and I need your help. I want to display binary number (for example the 000001 will show the last led on and the rest are off) and increment the number every second. I want to do this while I am connecting 6 Leds to the analog pins A0 to A5. I have already built the circuit. Now, I am left with the coding part. how can I fix the code below so that I am able to do so ( I am using this code because I want to make use of direct port manipulation.


void setup() {

  DDRB = DDRB | B00100000; 
}

void loop() {


 
  PINB = B00100000; 
  PINB = B00100000;
  
  delay(1);
}

Also I have a question regarding this forum, if I put my problem and people tried to help but then I edited the problem how would I let them know that I edited ? do I have to post it again ? or reply to everyone letting him/her know ?

For changes, it’s preferred if you post them as new items continuing in the same thread, that way the conversation flows properly.

Any particular reason you’re using direct port access ?
At 1Hz, you could walk around the block in the idle time.

Everything is represented as binary inside the any computer, controller.
What You use is integers, words, float, characters (text).

Split Your questions into 2 parts.

  1. What hardware do You intend to use? Do You know how to do it?
  2. What do You want to do with it?

You look like aiming at advanced use of the internal features of the controller.

Explain in a brief way what You want to achieve, not how You think it can be done.

Grab pen and paper and make a little schematic of the project. Lots of words, the Shakespeare way, is not the description helpers want to see.

I have merged your cross-posts @alkaothm.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please take some time to pick the forum category that best suits the subject of your question and then only post once to that forum category. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Hi, @alkaothm
What model Arduino are you using?
A circuit diagram showing the I/O pins and how you have wired the LEDs would be helpful.
Not a Fritzy image but a hand drawn picture would be great.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

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