[solved] A project with more than 13 DigitalPins on Leonardo

Hello everyone I have the Idea of make a RTC with a RTC Module and 7segments 4 digits to display the time, all this with a Leonardo board and only that.

Digital Pins from 6 to 13 are reserved to the 7segments and Digital Pin 14,15,16 and 5 controls the digits in the display; digital pins 2 and 3 are reserved for the I2C RTC module.

the problem is that when I was using my code for the 7 segment display with that setup only works the digit with the pin 5 and the digital pins 14, 15 and 16 didn't work, what i have to do? are something that i have done wrong?
thank you guys for the help and advices.

this is my setup:


and this is my code that only must handle the display:

// display 4 digit 7 segments
int Digit1 = 14;
int Digit2 = 15;
int Digit3 = 16;
int Digit4 = 5;

int SegmentA = 6;
int SegmentB = 7;
int SegmentC = 8;
int SegmentD = 9;
int SegmentE = 10;
int SegmentF = 11;
int SegmentG = 12;
int SegmentDP = 13;

byte H = 0;
byte h = 5;
byte M = 2;
byte m = 2;
int ms = 0;
byte s = 0;

unsigned long mills;
unsigned long old_mills;

byte del = 1;

int calib = 235;


//235 se adelanta unos milisegundos
//236 se atraza unos milisegundos
// cambiar el "del" aumenta o disminuye mucho la velocidad
void setup() {
  // put your setup code here, to run once:
  pinMode(Digit1, OUTPUT);
  pinMode(Digit2, OUTPUT);
  pinMode(Digit3, OUTPUT);
  pinMode(Digit4, OUTPUT);

  pinMode(SegmentA, OUTPUT);
  pinMode(SegmentB, OUTPUT);
  pinMode(SegmentC, OUTPUT);
  pinMode(SegmentD, OUTPUT);
  pinMode(SegmentE, OUTPUT);
  pinMode(SegmentF, OUTPUT);
  pinMode(SegmentG, OUTPUT);
  pinMode(SegmentDP, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  

   ms = ms + 1;
   if (ms >= calib){
   ms = 0;
   s= s + 1;
   }
   

  if (s >= 60){
    s = 0;
    m = m + 1;
  }
  if (s%2){
    digitalWrite(SegmentDP,LOW);
  }
  else{
    digitalWrite(SegmentDP,HIGH);
  }
  if (m >= 10){
    m = 0;
    M = M + 1;
  }
  if (M >=6){
    M = 0;
    h++;
  }
  if ( h >= 10){
    h = 0;
    H = H + 1;
  }
  if (H >= 10){ 
    H = 0; 
  }

  if ((H==1) && (h==3)){
    H=0;
    h=1;
  }

  numero(m);  
  digitalWrite(Digit1,HIGH);
  delay(del);
  digitalWrite(Digit1,LOW);
  numero(M);
  digitalWrite(Digit2,HIGH);
  delay(del);
  digitalWrite(Digit2,LOW);
  numero(h);
  digitalWrite(Digit3,HIGH);
  delay(del);
  digitalWrite(Digit3,LOW);
  numero(H);
  digitalWrite(Digit4,HIGH);
  delay(del);
  digitalWrite(Digit4,LOW);
//ultimo ajuste
delayMicroseconds(10);
}

// numeros
void numero(int i) //dibuja el numero especificado antes se debe encender el digito
{

  switch (i){

  case 1: 
    digitalWrite(SegmentA, HIGH);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, HIGH);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, HIGH);
    digitalWrite(SegmentG, HIGH);
    break;

  case 2:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, HIGH);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, LOW);
    digitalWrite(SegmentF, HIGH);
    digitalWrite(SegmentG, LOW);
    break;

  case 3:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, HIGH);
    digitalWrite(SegmentG, LOW);
    break;

  case 4:
    digitalWrite(SegmentA, HIGH);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, HIGH);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, LOW);
    break;

  case 5: 
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, HIGH);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, LOW);
    break;

  case 6:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, HIGH);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, LOW);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, LOW);
    break;

  case 7:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, HIGH);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, HIGH);
    digitalWrite(SegmentG, HIGH);
    break;

  case 8:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, LOW);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, LOW);
    break;

  case 9:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, HIGH);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, LOW);
    break;

  case 0:
    digitalWrite(SegmentA, LOW);
    digitalWrite(SegmentB, LOW);
    digitalWrite(SegmentC, LOW);
    digitalWrite(SegmentD, LOW);
    digitalWrite(SegmentE, LOW);
    digitalWrite(SegmentF, LOW);
    digitalWrite(SegmentG, HIGH);
    break;
  }
}

sorry for my poor English

ehrja:
Digital Pins from 6 to 13 are reserved to the 7segments and Digital Pin 14,15,16 and 5 controls the digits in the display; digital pins 2 and 3 are reserved for the I2C RTC module.

Why are 2 and 3 used for I2C? Why not use the dedicated I2C pins instead? The Leonardo doesn't need pins 0 and 1 to communicate with the PC over USB, so those are available too.

ehrja:
the problem is that when I was using my code for the 7 segment display with that setup only works the digit with the pin 5 and the digital pins 14, 15 and 16 didn't work

What is "didn't work?"

You shouldn't refer to the analog pins as "14" and "15". Instead, refer to their pre-defined constants "A0", "A1", etc.

Just a suggestion, but if you could see your way to incorporate a MAX7219 to drive the LED, you could save a lot of pins and there are libraries available to support it. Might make your project easier.

I do realize you want to do it with the RTC and Leonardo only, but just saying. After all, the Leonardo is punishment enough and you might just want to make life a little easier.

James C4S

As you can see in the picture I'm using the dedicate I2C pins instead 2 and 3, but 2 and 3 are mirrors of dedicated I2C pins, at least in Leonardo; If I use 1 and 0 I'll also need one extra pin and I thought use A0, A1 and A2 as digital pins, perhaps my only question here is how can I enable A0,A1,A2 as Digital pins?...
thank you for your answer.

BillO

That was my first idea, to use a shift register and save a lot of pines. I just receive the RTC module and the display from ebay seller, the only package that get lost was the package of the shift registers, and I thought - hey, we can do it anyway, since Leonardo has 20 digital pins -
thank you for your answer.

ehrja:
As you can see in the picture I'm using the dedicate I2C pins instead 2 and 3

My bad, I thought I2C on Leonardo wasn't mapped to GPIO.

ehrja:
I thought use A0, A1 and A2 as digital pins, perhaps my only question here is how can I enable A0,A1,A2 as Digital pins?...

pinMode(A0, OUTPUT); // (or INPUT)

You shouldn't refer to the analog pins as "14" and "15". Instead, refer to their pre-defined constants "A0", "A1", etc.

Yeah... I keep thinking on this a little while and I get that, I thought -maybe he mean in the sketch- and yep

I change this:

int Digit1 = 14;
int Digit2 = 15;
int Digit3 = 16;
int Digit4 = 5;

for this:

int Digit1 = A0;
int Digit2 = A1;
int Digit3 = A2;
int Digit4 = 5;

and it works...
look:

thank you man you are so kind.

I don't get that at first :confused:
Now I can go ahead...