Showing seconds when button is pressed

Hi! im trying to show the seconds when pressing the button. I'm using a 4x4 matrix keypad for the buttons and a 4digit ssd.

I know that this Time=(Hrs*100+Min); Display.setNumber(Time); shows the hrs and min on the 4 digit SSD but my aim is to show the seconds when pressing the button '0'

Is there any other "Time" variable for this? for me to show seconds?

#include <SevSeg.h>
SevSeg Display;
#include <Keypad.h>
//CLOCK
const unsigned long period = 1000;
const unsigned long led_period = 500;
unsigned long startMillis;
unsigned long led_startMillis;
unsigned long currentMillis;
unsigned long led_currentMillis;
const int hrs_btn = A0;
const int min_btn = A1;
const int ledPin = A2;
int Hrs = 1;
int Min = 2;
int Sec = 58;
int Time;
int time;
int ledState = LOW;

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','A','B'},
  {'C','D','E','F'}
};

byte rowPins[ROWS] = {A3,A2,A1,A0}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {13,A5,A4}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long loopCount;
unsigned long startTime;
boolean blink = false;
boolean ledPin_state;

void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);              // Sets the digital pin as output.
    digitalWrite(ledPin, HIGH);           // Turn the LED on.
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.
    kpd.addEventListener(keypadEvent); // Add an event listener for this keypad
    //clock
  pinMode(hrs_btn, INPUT_PULLUP);
  pinMode(min_btn, INPUT_PULLUP);
  byte numDigits = 4;
  byte digitPins[] = {9,10,11,12}; //change depending on wire
  byte segmentPins[] = {2,3,4,5,6,7,8};
  bool resistorOnSegments = false;
  byte updateWithDelays = false;
  bool hardwareConfig = COMMON_CATHODE;
  bool leadingZeroes = true;
  bool disableDecPoint = true;
  Display.begin(hardwareConfig,numDigits, digitPins, segmentPins, resistorOnSegments, updateWithDelays,leadingZeroes, disableDecPoint);
  Display.setBrightness(100);  
}

void loop(){
    char key = kpd.getKey();
currentMillis = millis();


  if(currentMillis - startMillis >= period)
  {
    Sec  = Sec + 1;
    startMillis = currentMillis;
  }
  led_currentMillis = millis();
  if(led_currentMillis - led_startMillis >= led_period)
  {
    led_startMillis = led_currentMillis;
    if(ledState==LOW)
    {
      ledState = HIGH;
      if(digitalRead(hrs_btn) == LOW)
      {
        Hrs = Hrs +1;
      }
     if(digitalRead(min_btn)==LOW)
      {
        Min=Min+1;
        Sec=0;
      }
    }
    else
    {
      ledState=LOW;
    }
    digitalWrite(ledPin,ledState);
  }
  
  if(Sec==60)
  {
    Sec=0;
    Min=Min+1;
  }
  if(Min==60)
  {
    Min=0;
    Hrs=Hrs+1;
  }
  if(Hrs==13)
  {
    Hrs=1;
  }
  Time=(Hrs*100+Min);
  Display.setNumber(Time);
  Display.refreshDisplay();
  
   
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    switch (kpd.getState()){
    case PRESSED:
        if (key == '0') {

        }
       

        if (key == '1') {
          Min=Min+1;
        }
       
        
        if (key == '2') {
          Hrs=Hrs+1;
        }
        break;

    case RELEASED:
        if (key == '*') {
              // Restore LED state from before it started blinking.
            blink = false;
        }
        break;

    case HOLD:
        if (key == '*') {
            blink = true;    // Blink the LED when holding the * key.
        }
        break;
    }
}

Are you able to show hours and minutes at present?

Are you using 3 or 4 columns?

And are A0-2 assigned to the keyboard or individual buttons/LED?

Yes the hours and Minutes can be seen at the ssd. and I'm using the 4 columns. A0-A5 are assigned to the keyboard. A0-2 will not be using separate buttons I was just not able to delete it because im trying to do the Seconds to show up first. sorry about that

If I understand correctly, at present you are not using the buttons, but just want the seconds to show up as well.
You could try the following snippet (not tested):

//Change the variable name of Time to TimeHours
int TimeHours;
int TimeSeconds;

// in loop()
  TimeHours=(Hrs*100+Min);
  Display.setNumber(TimeHours);
  Display.refreshDisplay();
  
delay(2000);

  TimeSeconds= Sec;
  Display.setNumber(TimeSeconds);
  Display.refreshDisplay();

delay (2000);

I would strongly recommend not using similar variable names (time and Time).

Initially, the 4-digit display will show 0000 -- correct?
You press 0-button of Keypad; what you expect to appear on the display -- 0001?
You press 0-button again of Keypad; what you expect to appear on the display -- 0002?

Isn't this it?

int Sec = 58;

To display it:

  Display.setNumber(Sec);

Also, 'period' is a constant representing 1000ms. The meaning is baked (hard coded) into the code because it controls one second increments. Thus it deserves a much more descriptive name, I use

const int MS_PER_SEC = 1000;

It's superfluous to make it unsigned long, as it's value is always and forever 1000, by definition.

setNumber() method expects three arguments -- are the remaining two optional?

I just copied the line from the posted sketch:

  Display.setNumber(Time);

I am attaching the SevSeg.h Library for your reference.
SevenSeg-master.zip (617.0 KB)

Edit - you have posted a different library.

Sorry for the inconvenience! Now, the right one is attached.
SevSeg-master (1).zip (17.4 KB)

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