Help me making a jumbo timer with Arduino mega and 7 segment jumbo display

hi mates, i am trying to make a 3 minutes timer with 3 jumbo 7 segment displays with display1=minute, display2=seconds1 and display2=seconds2. Am facing an issue with the code where the minute display is not getting updated after 60 seconds. both the seconds display works perfectly and the minutes display remains displaying 0. Kindly help me out with the below code.

// Define pin numbers for each segment (a to g) for the minutes display
const int segmentPinsMinutes[] = {2, 3, 4, 5, 6, 7, 8};

// Define pin numbers for each segment (a to g) for the first seconds display
const int segmentPinsSeconds1[] = {22, 24, 26, 28, 30, 32, 34};

// Define pin numbers for each segment (a to g) for the second seconds display
const int segmentPinsSeconds2[] = {31, 33, 35, 37, 39, 41, 43};

int currentMinutes = 0;
int currentSeconds = 0;

void setup() {
  // Set segment pins as OUTPUT for all displays
  for (int i = 0; i < 7; i++) {
    
    pinMode(segmentPinsMinutes[i], OUTPUT);
    pinMode(segmentPinsSeconds1[i], OUTPUT);
    pinMode(segmentPinsSeconds2[i], OUTPUT);
  }
}

void loop() {
  unsigned long startTime = millis() / 1000;

  for (int minutes = 0; minutes < 3; minutes++) {
    for (int seconds = 0; seconds < 60; seconds++) {
      unsigned long currentTime = startTime + minutes * 60 + seconds;
      displayTime(currentTime / 60, currentTime % 60);
      delay(1000); // Display each second
    }
    // Add a small delay before restarting the loop for the next minute
    delay(500); // Adjust as needed
  }

  // After 3 minutes, you can choose to reset the timer or take some other action
  // For now, let's reset the timer to 0 and wait for a brief moment
  startTime = millis() / 1000;
  delay(2000); // Wait for 2 seconds before restarting the timer
}



void displayTime(int minutes, int seconds) {
  const byte digitSegments[][7] = {
    {0, 0, 1, 0, 0, 0, 0}, // Digit 0
    {1, 1, 1, 1, 1, 0, 0}, // Digit 1
    {0, 1, 0, 0, 0, 1, 0}, // Digit 2
    {0, 1, 0, 1, 0, 0, 0}, // Digit 3
    {1, 0, 0, 1, 1, 0, 0}, // Digit 4
    {0, 0, 0, 1, 0, 0, 1}, // Digit 5
    {0, 0, 0, 0, 0, 0, 1}, // Digit 6
    {0, 1, 1, 1, 1, 0, 0}, // Digit 7
    {0, 0, 0, 0, 0, 0, 0}, // Digit 8
    {0, 0, 0, 1, 0, 0, 0}  // Digit 9
  };

  // Display the tens and units of minutes
  int tensMinutes = minutes / 10;
  int unitsMinutes = minutes % 10;

  // Display the minutes
  displaySegments(segmentPinsMinutes, digitSegments[tensMinutes]);
  delayMicroseconds(500); // Add a small delay for multiplexing
  displaySegments(segmentPinsSeconds1, digitSegments[unitsMinutes]);
  delayMicroseconds(500);

  // Display the tens and units of seconds on the first display
  int tensSeconds = seconds / 10;
  int unitsSeconds = seconds % 10;
  displaySegments(segmentPinsSeconds1, digitSegments[tensSeconds]);
  delayMicroseconds(500);
  displaySegments(segmentPinsSeconds2, digitSegments[unitsSeconds]);
  delayMicroseconds(500);
}


void displaySegments(const int pins[], const byte segments[]) {
  // Display the segments for the selected digit
  for (int i = 0; i < 7; i++) {
    digitalWrite(pins[i], segments[i]);
  }
}

I noticed that the minutes display is getting updated from 0 to 1, after 10 minutes.

thanks in advance.

next step is to place your code between code tags

:sweat_smile: Have updated. Sorry for the inconvenience.

// Define pin numbers for each segment (a to g) for the minutes display
const byte segmentPinsMinutes[] = {2, 3, 4, 5, 6, 7, 8};

// Define pin numbers for each segment (a to g) for the first seconds display
const byte segmentPinsSeconds1[] = {22, 24, 26, 28, 30, 32, 34};

// Define pin numbers for each segment (a to g) for the second seconds display
const byte segmentPinsSeconds2[] = {31, 33, 35, 37, 39, 41, 43};

const byte digitSegments[] = {
  0b0010000, // Digit 0
  0b1111100, // Digit 1
  0b0100010, // Digit 2
  0b0101000, // Digit 3
  0b1001100, // Digit 4
  0b0001001, // Digit 5
  0b0000001, // Digit 6
  0b0111100, // Digit 7
  0b0000000, // Digit 8
  0b0001000  // Digit 9
};

unsigned long startTime;

void setup() {
  // Set segment pins as OUTPUT for all displays
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPinsMinutes[i], OUTPUT);
    pinMode(segmentPinsSeconds1[i], OUTPUT);
    pinMode(segmentPinsSeconds2[i], OUTPUT);
  }
  startTime = millis();
}

void loop() {
  static unsigned long currentTime = startTime;
  int temp = (millis() - startTime)  / 1000;
  if ( currentTime < temp)displayTime( currentTime = temp );
}

void displayTime(unsigned long secs) {
  static byte unitsMinutes = (secs / 3600) % 10;
  static byte tensSeconds = (secs % 60) / 10;
  static byte unitsSeconds = secs % 10;

  displaySegments(segmentPinsMinutes, digitSegments[unitsMinutes]);
  displaySegments(segmentPinsSeconds1, digitSegments[tensSeconds]);
  displaySegments(segmentPinsSeconds2, digitSegments[unitsSeconds]);
}

void displaySegments(const int pins[],  byte segments) {
  for (int i = 0; i < 7; i++)digitalWrite(pins[i], segments & (1 << i));
}

repaired

Compilation error: braces around scalar initializer for type 'const byte {aka const unsigned char}'

Getting this...

and now? did you tryed repaired version from post #4 ?

This is the error am getting with your code. I hv successfully changed the code and now its working properly. Thank You

Here's the code for your red,

// Define pin numbers for each segment (a to g) for the minutes display
const int segmentPinsMinutes[] = {2, 3, 4, 5, 6, 7, 8};

// Define pin numbers for each segment (a to g) for the first seconds display
const int segmentPinsSeconds1[] = {22, 24, 26, 28, 30, 32, 34};

// Define pin numbers for each segment (a to g) for the second seconds display
const int segmentPinsSeconds2[] = {31, 33, 35, 37, 39, 41, 43};

int currentMinutes = 0;
int currentSeconds = 0;

void setup() {
  // Set segment pins as OUTPUT for all displays
  for (int i = 0; i < 7; i++) {
    
    pinMode(segmentPinsMinutes[i], OUTPUT);
    pinMode(segmentPinsSeconds1[i], OUTPUT);
    pinMode(segmentPinsSeconds2[i], OUTPUT);
  }
}

unsigned long startTime; // Declare startTime outside the loop

void loop() {
  startTime = millis() / 1000; // Set startTime only once

  for (int minutes = 0; minutes < 3; minutes++) {
    for (int seconds = 0; seconds < 60; seconds++) {
      unsigned long currentTime = startTime + minutes * 60 + seconds;
      displayTime(currentTime / 60, currentTime % 60);
      delay(1000); // Display each second
    }
    // Add a small delay before restarting the loop for the next minute
    delay(500); // Adjust as needed
  }

  // After 3 minutes, you can choose to reset the timer or take some other action
  // For now, let's reset the timer to 0 and wait for a brief moment
  startTime = millis() / 1000;
  delay(2000); // Wait for 2 seconds before restarting the timer
}




void displayTime(int minutes, int seconds) {
  const byte digitSegments[][7] = {
    {0, 0, 1, 0, 0, 0, 0}, // Digit 0
    {1, 1, 1, 1, 1, 0, 0}, // Digit 1
    {0, 1, 0, 0, 0, 1, 0}, // Digit 2
    {0, 1, 0, 1, 0, 0, 0}, // Digit 3
    {1, 0, 0, 1, 1, 0, 0}, // Digit 4
    {0, 0, 0, 1, 0, 0, 1}, // Digit 5
    {0, 0, 0, 0, 0, 0, 1}, // Digit 6
    {0, 1, 1, 1, 1, 0, 0}, // Digit 7
    {0, 0, 0, 0, 0, 0, 0}, // Digit 8
    {0, 0, 0, 1, 0, 0, 0}  // Digit 9
  };

  // Display the tens and units of minutes
  int tensMinutes = minutes / 1;
  int unitsMinutes = minutes % 1;

  // Display the minutes
  displaySegments(segmentPinsMinutes, digitSegments[tensMinutes]);
  delayMicroseconds(500); // Add a small delay for multiplexing
  displaySegments(segmentPinsSeconds1, digitSegments[unitsMinutes]);
  delayMicroseconds(500);

  // Display the tens and units of seconds on the first display
  int tensSeconds = seconds / 10;
  int unitsSeconds = seconds % 10;
  displaySegments(segmentPinsSeconds1, digitSegments[tensSeconds]);
  delayMicroseconds(500);
  displaySegments(segmentPinsSeconds2, digitSegments[unitsSeconds]);
  delayMicroseconds(500);
}


void displaySegments(const int pins[], const byte segments[]) {
  // Display the segments for the selected digit
  for (int i = 0; i < 7; i++) {
    digitalWrite(pins[i], segments[i]);
  }
}

i don't think so

  int tensMinutes = minutes / 1;
  int unitsMinutes = minutes % 1;

this is the place that i altered.

the only problem am facing now is, its not stopping after 3 mins. if possible kindly help me in that.

may be it should be?

  int tensMinutes = minutes / 10;
  int unitsMinutes = minutes % 10;

What is the purpose of this? It seems that this will cause your timer to be less accurate.

mega

1 Like

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