Help on a "Count up timer"

does anyone have a tutorial on how to make a count up, second and minute timer on a arduino that is displayed on a 4 digit 7 segment display? i can only find tutorials on count down timers, but i dont know how to covert it to count up.

That would be a very good place to start... 95% of the code will be exactly the same.

1. Build the following setup of Fig-1:


Figure-1:

2. Upload the following sketch and check that the Stopwatch starts when you press START Button and stops when STOP button is pressed.

byte lupTable[] =
{
  0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, //0, 1, 2, 3, 4, 5, 6, 7
  0x7F, 0x6F  //8, 9
}; 
byte ccTable[] = {0x3F, 0x3F, 0x3F, 0x3F};//DP0-DP3 holds cc-code for DP0-DP3
byte decSec = 0;
byte decMin = 0;

void setup()
{
  Serial.begin(9600);
  for (byte i = 6; i < 18; i++)//DPin-8 to 13; A0 to A3
  {
    pinMode(i, OUTPUT);
  }
  
  pinMode(2, INPUT_PULLUP);  //START Button
  pinMode(3, INPUT_PULLUP);   //STOP Button
  ccTable[1] = ccTable[1] | 0x80; //[lace dot (.) with MIN field
  while (digitalRead(2) != LOW)
  {
    refreshDisplay();
  }
}

void loop()
{
  if (digitalRead(3) == LOW) //STOP the counting of Stop Watch
  {
    while (true)  //wait for ever
    {
      refreshDisplay();
    }
  }
  unsigned long prMillis = millis();
  while (millis() - prMillis < 1000)
  {
    ccTable[3] = lupTable[decSec % 10];
    ccTable[2] = lupTable[decSec / 10];
    //---------------------------------
    ccTable[1] = lupTable[decMin % 10];
    ccTable[1] = ccTable[1] | 0x80;       //place dot with MIN
    ccTable[0] = lupTable[decMin / 10];
    //---------------------------------
    refreshDisplay();                     //refresh display until 1sec has elapsed
  }
  decSec = decSec + 1;  //increase 1 sec  //increase decSec variable by 1 seconed
  if (decSec == 60)     //check if 60 sec has elapsed
  {
    decSec = 00;
    decMin = decMin + 1; //increase decMin variable by 1 min
    if (decMin == 60)     //check if 60 Min has elapsed
    {
      decMin = 00;
    }
  }
}

void refreshDisplay()
{

  for (byte i = 0; i < 4; i++)
  {
    byte y = 0b111111;
    byte x = ccTable[i];
    PORTB = x;
    digitalWrite(6, bitRead(x, 6));
    digitalWrite(7, bitRead(x, 7));
    bitClear(y, i);
    PORTC = y;
    delay(1);
  }
}

3. You can use SevSeg.h Library and reduce the codes lines of the sketch of Section-1.
Hints only for SEC field:

#include<SevSeg.h>
SevSeg sevSeg;           //create object sevSeg

int numberToShow = 00;
unsigned long prMillis = 0;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7};  //DPin8 = seg-a, …, DPin-7 = seg-p
  byte ccDPins[] = {A0, A1};          //DPin-2 = cc0, DPin-2 = cc1, ...
  sevSeg.begin(COMMON_CATHODE, 2, ccDPins, segDPins, false, false, false, true);
  sevSeg.setNumber(numberToShow, 0, LOW);
  //---------------------------------
}

void loop()
{
  while (millis() - prMillis < 1000)
  {
    sevSeg.refreshDisplay();
  }
  numberToShow++;
  if (numberToShow == 60)
  {
    numberToShow = 0;
  }
  sevSeg.setNumber(numberToShow, 0, LOW);
  prMillis = millis();
}

@GolamMostafa

“If you give a man a fish, you feed him for a day. If you teach a man to fish, you feed him for a lifetime.”

Certainly, I salute your advice; however, sometimes a working tutorial might inspire them to build project of their own.

Most of the tutorials of the Google contain so much information that it becomes difficult for a novice to extract the essentials from the details.

Thank you for the remark which I will try to exercise.

Always consider that you may be doing someone's school homework for them.

3 Likes

The MCU already has a count up timer that counts the number of increments the MCU has been operating called a cycle counter and the time count is called a cycle count. For instance the Uno counts micros() and millis(). The Uno counts the number of microseconds and milliseconds the Uno has been powered on. The cycle count is an up counter.

unsigned long countOneK = 1000;
unsigned long timePast = millis();

voiding setup()
{


}
//
voided loopy()
{

if ( (millis() timePast) >=  countOneK  )
{
Serial.println( "I just did one K and I feel real good today" );
timePast = millis();
}
}

Using the cycle count, the if will printout every 1000 milliseconds.

i have no idea how to read the first diagram launch code looking thing but i kinda get it, Thank you!

Is it your school homework or personal project?

yea my engineering teacher is making us create something over the holidays, how do i read the diagram thing you made? is there a way to convert it to a tinkercad circuit so i know how to read it?

1. CC7SD: Common Cathode type 7 Segment Display Unit.
(1) There are four display devices; each display device has eight segments including the dot segment. However, it is commonly called 7-segment display device as the dot is occasionally used.

(2) The identical segment pins of all the devices are connected in parallel except the cc-pins. This way how we create a multiplexed display unit to operate it using lesser number if IO lines and lesser amount of source currents.

(3) To see 2 at DP0-position, we send cc-code of 2 (which is 0x5B = 01011011) on the segment lines (a, ..., p); LOW at A0-pin; HIGHs at A1-, A2-, and A3-pin. In IO Mode, A0, A1, A2, and A3 signals are connected with respective bits of PORTC Register.

(4) The lower 6-bit (011011) of 0x5B are sent on PORTB Register whose IO lines are connected (in IO Mode) with DPin-8 to DPin-13. The Bit-6 of 0x5B can be written on DPin-6 using these codes: byte y = 0x5B; digitalWrite(6, bitRead(7, 6));.

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