Reading led 7 segment please help!!!

Hello everyone, thanks for taking the time to read this, I really appreciate it.

  • So I have 3 7 segment display that displays outside, what i want to do is read into the Arduino
  • It is common anode
  • pin A,B,C,D,E,F,G set pin 2,3,4,5,6,7,8 arduino
  • 3 pin digit set 20,21,19
    this is my code.
    ///////////////////////////////////////
    volatile int flag1 = HIGH;
    int A = 2;
    int B = 3;
    int c = 4;
    int D = 5;
    int E = 6;
    int F = 7;
    int G = 8;

byte State_A;
byte State_B;
byte State_C;
byte State_D;
byte State_E;
byte State_F;
byte State_G;

void setup()
{
Serial.begin(9600);
pinMode(20, INPUT);
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(c, INPUT);
pinMode(D, INPUT);
pinMode(E, INPUT);
pinMode(F, INPUT);
pinMode(G, INPUT);

attachInterrupt(3, capture1, RISING);
}
void loop {
if (flag1==HIGH){
flag1=LOW;
State_A = digitalRead(A);
State_B = digitalRead(B);
State_C = digitalRead(c);
State_D = digitalRead(D);
State_E = digitalRead(E);
State_F = digitalRead(F);
State_G = digitalRead(G);
if(State_A==0 && State_B==0 && State_C==0 && State_D==0 && State_E==0 && State_F==0 && State_G==1){Serial.println("0");}
......
if(State_A==0 && State_B==0 && State_C==0 && State_D==0 && State_E==1 && State_F==0 && State_G==0){Serial.println("9");}
}
void capture1()
{
flag1 = HIGH;
}
///////////////////////////////////////

when i open serial monitor, it's wrong when i change the number of machine.it alway set 8 number...i mean pin A,B,C,D,E,F,G is "1".
how can i fix it.... :confused: :confused:

phoxom113:
thanks for taking the time to read this, I really appreciate it.

But it's such a pity you didn't take the time to read How to use the forum :frowning:

Which board are you using? I see a attachInterrupt(3, capture1, RISING); so not an Uno...

i use arduino mega 2560.

You might want to do something with the first part of my reply as well. Otherwise I don't expect a lot of answers...

septillion:
You might want to do something with the first part of my reply as well. Otherwise I don't expect a lot of answers...

i apologize if i do something wrong, i'm not good at english, this is 1st i use forum on web.
1 ditgit(1 led) i set pin 20 to set attachinterupt(3) for board arduino mega 2560, is this right?

If your English isn't good enough to read How to use the forum you might want to get help in your native language...

septillion:
If your English isn't good enough to read How to use the forum you might want to get help in your native language...

i can read a little bit, if more...i can translate on google.hjhj.because i know this is big forum for arduino

Are the following statements correct in respect of your post?

1. You have 3-digit CA(common anode)-type 7-segment multiplexed display unit. Let us designate them from left as DP0, DP1, and DP2

2. The segments A, B, C, D, E, F, and G are being driven by DPin-2,3,4,5,6,7, and 8 of Arduino MEGA. You are not driving the dot/point (.) of the display devices?

3. The anode terminals CA0 (common anode pin of DP0), CA1, CA2 of the display devices are being driven by DPin- 20,21, and 19.

4. On the basis of the above information, can the following diagram be presented for your display unit?

atemp3CA.png

5. Now, my questions?
(a) Do you have any plan to install current limiting resistors at the CA-pin of the display devices?
(b) What is the purpose of this display unit? What kind of information are you going to show using this display unit?
(c) Do you know how much current a digital IO pin of the MEGA can deliver? Assume that it is 20 mA and all the segments of DP0 will be ON. A segment normally draws 10-15 mA current to give an acceptable level of brightness; if so, there is a requirement of 7x15 = 105 mA current for all the 7-segments of a display device which an IO pin is unable to delver. In this case, do you have any plan to derive the CA-pins/segment-pins using external current drivers?

atemp3CA.png

@GolamMostafa

It looks like OP wants to read the display, not drive it.

I think you will need to use a 'scope to understand the timing of the cathode and anode signals. Could be that the anode goes high before the cathodes go low, for example.

However, I don't think your method of setting a flag in the interrupt will work correctly. You may need to read each of the inputs, and save their values, in the interrupt. Otherwise, by the time loop() has checked the flag and captured the inputs, the anode may have gone low again.

Also, the cathode drivers may be open-collector, so even if you read the inputs at the correct time, they may be floating.

I change digitalRead => analogRead and change pinMode(A, INPUT_PULLUP),i connect resistor 1k pin A to 3,3v.it's ok. The number's analog about 800 when led pin A on.i try connect res 1k to all pin B,C,D,E,F,G is go crazy...analog number is change to much...i dont know why​:frowning::frowning:

Read and implement what PaulRB told you in reply #9.

However, I don't think your method of setting a flag in the interrupt will work correctly. You may need to read each of the inputs, and save their values, in the interrupt. Otherwise, by the time loop() has checked the flag and captured the inputs, the anode may have gone low again.

That is what you need to do.

Your code as it is will only read one digit of your three even when you make these changes.

Grumpy,i dont know how to check capture,
I change
Void setup(){
......
Attachinterupt(3,capture,rising)}
Void loop(){
If (flag==HIGH){
Flag=LOW
If (state_a<800 && state_b<800......)
One=0
.......}
Serial.println(one)
}

Void capture(){
Flag=HIGH
state_a=analogRead(a)
State_b=analogRead(b)
.....}

is that right?

Each cathode needs its own interrupt otherwise you don’t know what you are reading.

Each state_a and so on variable needs to be declared as volatile.

Do not do any printing in an interrupt service routine.

I read 1 led first,if ok then i read 3 led,thats why only have 1 attachinterupt
I declare volatile int sate_a
Volatile int stae_b
........
But number of analog still change

Post your full code again. This time use copy for forum and paste it into the reply.

Grumpy_Mike:
Post your full code again. This time use copy for forum and paste it into the reply.

this is my code

volatile int flag = HIGH;

int A =A0;
int B =A1;
int C =A2;
int D =A3;
int E =A4;
int F =A5;
int G =A6;
int ONE = 20;
 volatile int  State_A=0;
 volatile int  State_B=0;
 volatile int State_C =0;
 volatile int State_D =0;
 volatile int State_E =0;
 volatile int State_F =0;
 volatile int State_G =0;
byte LED1;

void setup()
{
 Serial.begin(9600);
 pinMode(ONE, INPUT_PULLUP);
 pinMode(A, INPUT_PULLUP);
 pinMode(B, INPUT_PULLUP);
 pinMode(C, INPUT_PULLUP);
 pinMode(D, INPUT_PULLUP);
 pinMode(E, INPUT_PULLUP);
 pinMode(F, INPUT_PULLUP);
 pinMode(G, INPUT_PULLUP);
attachInterrupt(3,capture1,RISING); // led 1 first
}

void loop()
{ 
 if (flag==HIGH){
  flag=LOW;
if(State_A<=800 && State_B<800 && State_C<800 && State_D<800 && State_E<800 && State_F<800 && State_G>900){LED1=0 ;}
 ..........
if(State_A<800 && State_B<800 && State_C<800 && State_D<800 && State_E>900 && State_F<800 && State_G<800){LED1=9;}
}
Serial.print("LED1:");
Serial.println(LED1);
}      
void capture1()
{  flag=HIGH;
  State_A= analogRead(A); 
  State_B= analogRead(B);     
  State_C= analogRead(C);
  State_D= analogRead(D);
  State_E= analogRead(E);
  State_F= analogRead(F);
  State_G= analogRead(G);

}

Thanks.
At first glance that looks not so bad.
However, you are using analogRead, instead of digitalRead and the decoding - segments to numbers is a bit "pants" ( British slang for not very good ). Also after the first "if" statement in the loop function add a:-
noInterrupts();
call and before the Serial.Print statement add a
interrupts();
call.
Finally delete the } after the last if(State_A<800 &&..... line and place a } at the very end of the loop function.

Because the analogRead takes 0.1mS to process then for the 7 you use in the interrupt service routine you will use up 0.7mS. This could well be slower than that board is refreshing the digits on the display.

So try this code and see if you are getting a consistent reading from just one segment.

volatile int flag = HIGH;

byte A =A0;
byte ONE = 20;
volatile int  State_A=0;

void setup()
{
 Serial.begin(9600);
 pinMode(ONE, INPUT_PULLUP);
 pinMode(A, INPUT_PULLUP);
attachInterrupt(3,capture1,RISING); 
}

void loop()
{
 if (flag==HIGH){
  noInterrupts();
  flag=LOW;

Serial.print("Segment a:");
if(State_A > 800) Serial.println("Off");
   else Serial.println("On");
interrupts();
delay(200);
 } 
}
     
void capture1()
{  flag=HIGH;
  State_A = analogRead(A);
}

Now I want to see a picture of how you have wired it up, so showing the board and the wires into the Arduino.

thank you so much!
let me play with this and see what i can come up with.
this is wire.
i connect A,B,C,D,E,F and 20 arduino to arduino board.

Well not quite what I was asking for. I wanted to check that the ground on your device is connected to the ground on the Arduino. And I can't honestly say I am convinced that it is.