Author Topic: 7 segment 4 digit  (Read 1207 times)

0 Members and 1 Guest are viewing this topic.

Offline maze49Topic starter

  • Newbie
  • Posts: 3
  • Country: fr
7 segment 4 digit
« on: December 23, 2019, 12:37:33 pm »
Hi,

I introduce myself, begginer in electronic and coding. I got Arduino starter set and I really enjoy it.
My question is about the 7 segment 4 digit.

I display the same number on 4 digit OK
I display 1 2 3 4 with delay OK
I display 1234 with multiplexing OK
I display 0-9999 with push button OK

But, is it possible to control each digit individualy?
For example, Digit1 = 0-9 when Digit1 is 9 Digit2 = 1-5 when Digit2 is 5 Digit3 = 1-9 when Digit3 = 9 Digit4 = 1-9

hope to be clear :/

Thanks for your knowledge!
 

Offline admiralk

  • Regular Contributor
  • *
  • Posts: 178
  • Country: us
Re: 7 segment 4 digit
« Reply #1 on: December 23, 2019, 02:52:04 pm »
I am not familiar with 4 digit displays, but I would start by trying to control just a single digit. In other words, try just displaying 0-9 on the first digit while leaving the rest blank. Then do the same with the second digit, and so forth.
 
The following users thanked this post: maze49

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5135
  • Country: ro
  • .
Re: 7 segment 4 digit
« Reply #2 on: December 23, 2019, 03:17:48 pm »
The answer is it really depends on how your actual seven segment display is built.

The display may have a tiny controller chip between your arduino and the actual digits, in order to simplify the interaction with the digits. If this is the case, you're using SPI or I2C to "talk" to that chip sending it commands, and the controller chip does the harder work for you turning individual segments in all those digits.

If the seven segment digits have no in-between chip (this is most likely what you have) then there's two main kinds of such displays:

1. Common anode digits - these are digits that have one pin to give voltage to all segments and when you connect any other pin to ground (sink) that individual segment light up because electricity flows from the common anode pin to that other pin, through the led.

2. Common cathode digits - these have one pin for each segment through which you give voltage and all the cathodes of the leds inside the digit are connected to a single pin, which you usually connect to ground. You turn on individual segments by sending voltage through one of those pin (usually 8 of them, the seven segments of the digit plus the dot)

When there's multiple digits, there's usually one common anode or one common cathode for each digit, and the 8 segments of all digits are connected together.
For example, if you have a common anode display and you send power to all 4 common anodes, then when you connect one of the 8 other pins to ground, that segment of each digit will light up, because all four anodes receive voltage.
If you want to display numbers like 1234, you can't send voltage to all 4 anodes all the time, because the top right segment of "1" will also show up on 2, which you don't want.
So, you turn only one digit at a time: prepare the 8 segment pins for digit 1, turn on digit 1, wait a few ms, turn off digit, set the 8 segments for digit 2, turn on power to digit 2, wait a few ms ... repeat, and loop back to digit 1 for ever.

If you're asking how to display a number like "15" on a 4 digit display , how to extract the number 1 and the number 5, you have mathematical functions that give you the remainder of a division ...  so you can say something like
digit[3] = remainder of value divided by 10
value = value / 10
digit[2] = remainder of new value divided by 10
value = value / 10
...
digit[0] = remainder of value divided by 10

now you may have digit[0]=0 , digit[1]=0, digit[2]=1 , digit[3] = 5
If you don't want to display 0015 then you can simply do something like
start_from=0;
while (digit[start_from]==0 && start_from<3) start_from=start_from+1;

and now start_from will be the first digit that has non zero value, or the 4th digit.

 
 digit
« Last Edit: December 23, 2019, 03:23:49 pm by mariush »
 
The following users thanked this post: maze49

Offline maze49Topic starter

  • Newbie
  • Posts: 3
  • Country: fr
Re: 7 segment 4 digit
« Reply #3 on: December 23, 2019, 07:18:30 pm »
Hi,

Thanks admiralk,
Yes I do.
You are right, it is the best way to understand how 7 segment works.

mariush
Thanks for your explanations. I understand your code example and it looks like what I did.
Actually it works like this:

Digit1 and Digit2 count until 99 then restart to 0
When Digit1 and Digit2 write 99, Digit3 add 1 ect...

Now, I wanted to add "delay()"when it is 50 but my code doesn't like at all and I think to know why.
Because to display all my digit together I use "delayMicroseconds" for multiplexing.

What do you think about?



« Last Edit: December 23, 2019, 07:37:46 pm by maze49 »
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5135
  • Country: ro
  • .
Re: 7 segment 4 digit
« Reply #4 on: December 23, 2019, 08:18:11 pm »
Well, that's fairly simple.

Instead of one variable holding the number, have two variables each holding what's displayed on 2 digits.
Do the stuff I wrote for each variable, to get digit 0 + digit1  and for the second number digit 2 + digit 3
add a third variable, let's say counter.
This is the amount of time you want to spend displaying the numbers on the display before you update the two numbers (by reading them from pins or whatever)
For example, if you spend 1ms on each digit, you're gonna have ~4ms spend displaying the 4 digits, so you want to loop 50 ms / 4ms = 12.5 times ... let's say 12
So you set the counter to 12

if counter variable is 0 then
  read number1
  read number2
  calculate digits
  if (value is 50)  set counter variable to number of loops for 50 ms
  if (value is not 50) set counter variable to minimum amount of loops, let's say for 10 ms
end if

while counter > 0
  display first digit , wait time
  display second digit, wait time
  display third digit, wait time
  display fourth digit, wait time
  counter = counter - 1
end while
 
repeat everything.

This method would work, but for that period of time where you loop through digits, you're not accepting any inputs.
A better method would be to learn to use interrupts, and set an interrupt on change.

When something changes on your input pins that are configured by you, your code is 'paused' and the arduino goes inside the function which does something, and when you exit out of that function, your loop resumes from where it left off .
So, your interrupt function can read the value on the pins every time the data on pins changes and update the two values and this way your values are always correct.
However, you can use two separate variables to keep what's shown on the digits, and only update these two variables when the counter variable goes down to 0.
 
The following users thanked this post: maze49

Offline maze49Topic starter

  • Newbie
  • Posts: 3
  • Country: fr
Re: 7 segment 4 digit
« Reply #5 on: December 23, 2019, 09:40:23 pm »
"Well, that's fairly simple"
Not totally for me ;D

I think to understand what you explain me. I must split each time the 4 digit are displaying in the loop like this I can increments a pause in the code without loosing the values.
So I tried but I do wrong because the problem is still here.


void loop()
{
 
   number++; 

  pickDigit(2);
  pickNumber(number/1000%10);
  delayMicroseconds(100);
 

  pickDigit(1);
  pickNumber(number/100)%10);
  delayMicroseconds(100);
 

  pickDigit(4);
  dispDec(3);
  pickNumber(number/10)%10);
  delayMicroseconds(100);
 

  pickDigit(3);
  pickNumber(number%10);
  delayMicroseconds(100);

  if (number > 50)
{
 number=0;
 delay(500);
}

So, when digit write 50 it go to 0 but the delay broke the code.




« Last Edit: December 23, 2019, 09:43:30 pm by maze49 »
 

Offline admiralk

  • Regular Contributor
  • *
  • Posts: 178
  • Country: us
Re: 7 segment 4 digit
« Reply #6 on: December 24, 2019, 01:27:10 am »
If you are going to show code, you should copy and paste what you are using instead of typing it out. Code tags (the # button) help also.

Anyway, I have no idea what those functions do, but I would guess that
Code: [Select]
pickDigit()

selects which digit you want to light. If that is the case and you are only using the first two, do not even bother to call the other two. But then again, you call
Code: [Select]
dispDec()

only on one of the digits so if that is the digit you are trying to display, you cannot get to 50 since it is more than one digit.

Without knowing what the functions do (I do not know Arduino, if they are part of that) there is not much else I can say since I am completely confused right now.  ???
 

Offline Raj

  • Frequent Contributor
  • **
  • Posts: 701
  • Country: in
  • Self taught, experimenter, noob(ish)
Re: 7 segment 4 digit
« Reply #7 on: January 04, 2020, 03:38:37 pm »
Master the use of % symbol and & symbol.
Even a kid knows +,-, /,* symbol
Your answer lies in code

Like you read 12 from first sensor, and 25 from second
You have display that can show 4 digit number but you can physically separate them to make it into a 2, 2 digit display

Now all you need to do is maths
Eg
((12*100)%100)+(25)
Aka ((num1*10)%100)+num2
« Last Edit: January 04, 2020, 03:42:39 pm by Raj »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf