Arduino driving 2x 7 segments displays.

Hello, i'm quite fresh with Arduinos so i wanted at first to play with some 7 segment displays, i bought a pair that shows 2 digits, anodes linked together. I've attached them all thru resistors to digital pins 2-6, and analog pins 0-4.

I've written the following code that i wanted to generate a number starting at 0 and ending up at 99, and showing it on the display, but i can't get one digit to show properly.

int j;
void loop(){
  for(int i=13;i<=99;i++){
    j=i%10;
  
    digitalWrite(6,HIGH); //Using this to change cathode 
    digitalWrite(18,LOW); // one cathode is at pin 18 and the other on pin 6
 
    switch(j){
      case 1:{ one();break;}  //the one, tho etc functios display the ceritan number 
      case 2:{ two();break;}  // on one 7 digit display.
      case 3:{ three();break;}
      case 4:{ four();break;}
      case 5:{ five();break;}
      case 6:{ six();break;}
      case 7:{ seven();break;}
      case 8:{ eight();break;}
      case 9:{ nine();break;}
      case 0:{ zero();break;}
      default:break;
      }
      clear();     // clear function sets all output pins to LOW
        
     j=(i/10)%10;
     digitalWrite(6,LOW);
    digitalWrite(18,HIGH);
 //  j=5;      Here i tried using a constant, just to test it, still it didn't show up tho
    switch(j){
      case 1:{ one();break;}
      case 2:{ two();break;}
      case 3:{ three();break;}
      case 4:{ four();break;}
      case 5:{ five();break;}
      case 6:{ six();break;}
      case 7:{ seven();break;}
      case 8:{ eight();break;}
      case 9:{ nine();break;}
      case 0:{ zero();break;}
    default: break; }
 
         } 
    clear();
     }
 
 
}

The number display functions each look something like this :

void one(){
  digitalWrite(2,HIGH);
  digitalWrite(17,HIGH);
    }

I have also tried this very simple example and everything works fine.

 /*
void loop(){
    digitalWrite(6,LOW);
    digitalWrite(18,HIGH);
      one();
    delay(1);
     clear();

    digitalWrite(6,HIGH);
    digitalWrite(18,LOW);  
     zero();
    delay(1);
  clear();     }
  */

It seems i run into trouble tho when i try to display a generated two digit number and i don't understand why. The variable j seems to be allright before trying to display it ( tested it with Serial.print(j) ).

I've also attached the initialization of the pins :

int pins[11]={
  -1,2,3,4,5,6,14,15,16,17,18};  //used -1 so that the vector starts at 1
void setup(){
  Serial.begin(9600);    // used for debugging
  for(int i=1;i<=11;i++){
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i],LOW);
  }
  digitalWrite(18,HIGH);
  digitalWrite(6,HIGH);
}

Thanx in advance.

I notice that there are a couple of delays of one millisecond each in the code that works. Have you tried adding those delays to the other code, that does not work? You may be setting up the segment output pins for such a brief period that nothing is visible on the display.

I have managed to get it to work, for some odd reason i had to set up a delay in each of the case lines, if i put the delay after the whole switch it didn't work. Tho even like this it's quite hard to find a suitable timing so that the display blinks fast enough to trick the eye; at the moment it's all working but they are flashing quite slowly, plus you can't really control the speed at which the number shown is increased.

If anyone has any good solutions towards a function that could display a 2 digit number on a display multiplexed by arduino itself, please share.

It would help us to understand what you've got if you could post a circuit diagram (schematic) of your Arduino and two displays. (Sorry, but a fuzzy photo taken with a mobile phone is unlikely to be clear enough!)

Here is the schematic

Well, my display has 2 digits compared to that one, so there's 8 anodes (internally connected in parralel for the two digits) and 2 cathodes. I've also added resistors on the cathodes. I know they're not needed but i kinda used them for structural purposes, and plus, i figured they couldn't do any harm. I hope the next pictures explain.

OK, I think the trouble was to do with the delays, or lack of them, in the code. I've changed things around a bit, and come up with this, for the "loop" code:

// one cathode is at pin 18 and the other on pin 6
const int cathL = 6;
const int cathR = 18;

void loop ()
{
  int i;    // counter to run through integers 13..99
  int j;    // counter to display each pair of digits 50 times
  int digit;
  
  for (i = 13; i <= 99; i++) {
    for (j = 0; j < 50; j++) {  // loop to display digits for one second 
      digit = i % 10;  // units digit
  
      digitalWrite (cathL, HIGH); //Using this to change cathode
      digitalWrite (cathR, LOW);
    
      show_digit (digit);
    
      delay (10); // 10ms delay to allow digit to show
    
      clear();    // clear function sets all output pins to LOW
        
      digit = i / 10;  // tens digit
     
      digitalWrite (cathL, LOW);
      digitalWrite (cathR, HIGH);
    
      show_digit (digit);

      delay (10); // 10ms delay to allow digit to show
    
      clear ();
    }
  }
}


void show_digit (int digit)
{
    switch (digit) {
    case 1:
      one (); // the one, two etc functions display the certain number
      break;  // on one 7 segment display.
    case 2:
      two ();
      break;
    case 3:
      three ();
      break;
    case 4:
      four ();
      break;
    case 5:
      five ();
      break;
    case 6:
      six ();
      break;
    case 7:
      seven ();
      break;
    case 8:
      eight ();
      break;
    case 9:
      nine ();
      break;
    case 0:
      zero ();
      break;
    default:
      break;
    }
}

But I haven't tried compiling it! So do let me know how you get on.

Note that I've moved the repeated sections of code out into a function, to save space in the Flash ROM. I've also added a loop to repeatedly display the digits (50 times) so that each number should show for one second, then go on to the next number.

WoW, your code works great, display is lighting up nicely now. Thank you for all the attention. Next i'm going to try the same thing but with interrupts, similar tho the arduino example for a 8x8 led matrix on the arduino website. Hopefuly i should have a closer control on the display frecvency and the main loop should be independant of the display.

Also, does the j variable make the display blink at 50Hz?

Glad to hear that it worked!

Also, does the j variable make the display blink at 50Hz?

No, the "j" variable counts 50 times around a section of code that has two 10ms delays in it. Multiplying all that out, the entire "j" loop will take one second to execute. The rate of multiplexing, which is 50Hz, is determined by the two "delay (10)" function calls. Try making one of them "delay (5)" and the other "delay (15)", giving the same overall delay of 20ms. See what happens!