Helpneeded for a project

Hello dear reader.

i have been troubled lately becose i cant get a code to work.
iam trying to get a timer of 7 minutes on a 4 digit-7 segment display that gets activated when a button is pressed i tried anything and everything.
but i can t get the darn thing to work if you can help it would be fabulous.

i am using and arduino uno rev3 , there is no issue with the wiring or hardware.

i am already thankfull if you are reading this.
thanks.
beawere i did use chatGPT to try and fix it i am a real beginner started about 1 month ago i am trying to learn coding by myself but AI does help me a ton .
here is every code i tried:

// Include the required library for 7-segment display
#include <SevSeg.h>

// Pin definitions for the 7-segment display
#define NUM_DIGITS 4
#define DIGIT_1 2
#define DIGIT_2 3
#define DIGIT_3 4
#define DIGIT_4 5
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

// Pin for the pushbutton
#define BUTTON_PIN 13

// Define the timer duration in milliseconds (7 minutes)
#define TIMER_DURATION 420000

// Create an instance of the SevSeg library
SevSeg sevseg;

// Global variables
unsigned long startTime = 0;
bool timerRunning = false;

void setup() {
  // Initialize the display
  byte numDigits = NUM_DIGITS;
  byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
  byte segmentPins[] = {SEGMENT_A, SEGMENT_B, SEGMENT_C, SEGMENT_D, SEGMENT_E, SEGMENT_F, SEGMENT_G};
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);

  // Set up the button pin as input
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  // Check if the button is pressed
  if (digitalRead(BUTTON_PIN) == LOW && !timerRunning) {
    // Start the timer
    startTime = millis();
    timerRunning = true;
  }

  // Check if the timer is running
  if (timerRunning) {
    // Calculate the elapsed time
    unsigned long elapsedTime = millis() - startTime;

    // Check if the timer has reached the duration
    if (elapsedTime >= TIMER_DURATION) {
      // Timer expired, reset
      timerRunning = false;
    }

    // Convert elapsed time to minutes and seconds
    int minutes = (TIMER_DURATION - elapsedTime) / 60000;
    int seconds = ((TIMER_DURATION - elapsedTime) / 1000) % 60;

    // Display the time on the 7-segment display
    sevseg.setNumber(minutes * 100 + seconds, 2); // Display only minutes and seconds
    sevseg.refreshDisplay();
  }
}

when that did not work i tried this:

// Include the required library for 7-segment display
#include <SevSeg.h>

// Pin definitions for the 7-segment display
#define NUM_DIGITS 4
#define DIGIT_1 2
#define DIGIT_2 3
#define DIGIT_3 4
#define DIGIT_4 5
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

// Define the timer duration in milliseconds (7 minutes)
#define TIMER_DURATION 420000

// Create an instance of the SevSeg library
SevSeg sevseg;

// Global variables
unsigned long startTime = 0;
bool timerRunning = true; // Start the timer automatically

void setup() {
  // Initialize the display
  byte numDigits = NUM_DIGITS;
  byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
  byte segmentPins[] = {SEGMENT_A, SEGMENT_B, SEGMENT_C, SEGMENT_D, SEGMENT_E, SEGMENT_F, SEGMENT_G};
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
}

void loop() {
  // Check if the timer is running
  if (timerRunning) {
    // Calculate the elapsed time
    unsigned long elapsedTime = millis() - startTime;

    // Check if the timer has reached the duration
    if (elapsedTime >= TIMER_DURATION) {
      // Timer expired, reset
      timerRunning = false;
    }

    // Convert elapsed time to minutes and seconds
    int minutes = (TIMER_DURATION - elapsedTime) / 60000;
    int seconds = ((TIMER_DURATION - elapsedTime) / 1000) % 60;

    // Display the time on the 7-segment display
    sevseg.setNumber(minutes * 100 + seconds, 2); // Display only minutes and seconds
    sevseg.refreshDisplay();
  }
}

after that :

// Include the required library for 7-segment display
#include <SevSeg.h>

// Pin definitions for the 7-segment display
#define NUM_DIGITS 4
#define DIGIT_1 2
#define DIGIT_2 3
#define DIGIT_3 4
#define DIGIT_4 5
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

// Define the timer duration in milliseconds (7 minutes)
#define TIMER_DURATION 420000

// Create an instance of the SevSeg library
SevSeg sevseg;

// Global variables
unsigned long startTime;
bool timerRunning = true; // Start the timer automatically

void setup() {
  // Initialize the display
  byte numDigits = NUM_DIGITS;
  byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
  byte segmentPins[] = {SEGMENT_A, SEGMENT_B, SEGMENT_C, SEGMENT_D, SEGMENT_E, SEGMENT_F, SEGMENT_G};
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);

  // Initialize the start time
  startTime = millis();
}

void loop() {
  // Check if the timer is running
  if (timerRunning) {
    // Calculate the elapsed time
    unsigned long elapsedTime = millis() - startTime;

    // Check if the timer has reached the duration
    if (elapsedTime >= TIMER_DURATION) {
      // Timer expired, reset
      timerRunning = false;
    }

    // Convert elapsed time to minutes and seconds
    int remainingTime = (TIMER_DURATION - elapsedTime) / 1000; // Remaining time in seconds

    // Calculate minutes and seconds
    int minutes = remainingTime / 60;
    int seconds = remainingTime % 60;

    // Display the time on the 7-segment display
    sevseg.setNumber(minutes * 100 + seconds, 2); // Display only minutes and seconds
    sevseg.refreshDisplay();
  }
}

you get it :

// Include the required library for 7-segment display
#include <SevSeg.h>

// Pin definitions for the 7-segment display
#define NUM_DIGITS 4
#define DIGIT_1 2
#define DIGIT_2 3
#define DIGIT_3 4
#define DIGIT_4 5
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

// Create an instance of the SevSeg library
SevSeg sevseg;

void setup() {
  // Initialize the display
  byte numDigits = NUM_DIGITS;
  byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
  byte segmentPins[] = {SEGMENT_A, SEGMENT_B, SEGMENT_C, SEGMENT_D, SEGMENT_E, SEGMENT_F, SEGMENT_G};
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
}

void loop() {
  // Display countdown timer from 99 to 0
  for (int i = 99; i >= 0; i--) {
    // Extract tens and units digits
    int tens = i / 10;
    int units = i % 10;

    // Display tens and units digits
    sevseg.setDigit(3, tens, false);
    sevseg.setDigit(2, units, true);

    // Refresh display
    sevseg.refreshDisplay();

    // Delay for 1 second
    delay(1000);
  }
}

i began simplifiying it:

// Pin definitions for the 7-segment display
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

void setup() {
  // Set segment pins as output
  pinMode(SEGMENT_A, OUTPUT);
  pinMode(SEGMENT_B, OUTPUT);
  pinMode(SEGMENT_C, OUTPUT);
  pinMode(SEGMENT_D, OUTPUT);
  pinMode(SEGMENT_E, OUTPUT);
  pinMode(SEGMENT_F, OUTPUT);
  pinMode(SEGMENT_G, OUTPUT);
}

void loop() {
  // Display countdown timer from 99 to 0
  for (int i = 99; i >= 0; i--) {
    // Extract tens and units digits
    int tens = i / 10;
    int units = i % 10;

    // Display tens digit
    displayDigit(tens, 3);

    // Display units digit
    displayDigit(units, 2);

    // Delay for 1 second
    delay(1000);
  }
}

void displayDigit(int digit, int position) {
  switch (digit) {
    case 0:
      displaySegments(true, true, true, true, true, true, false, position);
      break;
    case 1:
      displaySegments(false, true, true, false, false, false, false, position);
      break;
    case 2:
      displaySegments(true, true, false, true, true, false, true, position);
      break;
    case 3:
      displaySegments(true, true, true, true, false, false, true, position);
      break;
    case 4:
      displaySegments(false, true, true, false, false, true, true, position);
      break;
    case 5:
      displaySegments(true, false, true, true, false, true, true, position);
      break;
    case 6:
      displaySegments(true, false, true, true, true, true, true, position);
      break;
    case 7:
      displaySegments(true, true, true, false, false, false, false, position);
      break;
    case 8:
      displaySegments(true, true, true, true, true, true, true, position);
      break;
    case 9:
      displaySegments(true, true, true, true, false, true, true, position);
      break;
    default:
      // Display nothing if the digit is out of range
      displaySegments(false, false, false, false, false, false, false, position);
      break;
  }
}

void displaySegments(bool a, bool b, bool c, bool d, bool e, bool f, bool g, int position) {
  digitalWrite(SEGMENT_A, a);
  digitalWrite(SEGMENT_B, b);
  digitalWrite(SEGMENT_C, c);
  digitalWrite(SEGMENT_D, d);
  digitalWrite(SEGMENT_E, e);
  digitalWrite(SEGMENT_F, f);
  digitalWrite(SEGMENT_G, g);

  // Activate the digit
  digitalWrite(position, LOW);
}

that and a few bit more i am a 100 percent sure the hardware is fine.

which 4 digit-7 segment display? ( TM1637 ?)
how did you wire it?
how did you power it?
what code did you try?

➜ we are missing information. do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).

Hello zortpeq

You might ask chatGPT again to provide a schematic.

after you read the best practices, rather than spend time trying to fix chatGPT's code that you don't understand, you should spend that time more wisely training yourself in C++

There is a lot available like C++ Introduction to get you started where the web site lets you type and try code directly on line. It helps anchor knowledge and you don’t get distracted by terminal command lines and files on your machine etc… (but there is advertising).

you can get a book for paper reference as well if you are more of a conceptual learner willing to get acquainted with theoretical knowledge without even trying everything - that works for some too esp if you master a few other OO programming languages. I heard good things about The C++ Primer(not c++ primer plus) as a first book. You also have Scott Meyers' Effective C++ books worth looking at. Of course any good Bookshelf would not be complete without the original book by Bjarne Stroustrup.

My experience has been though that the language and libraries are so rich now that searching on line (cppreference.com and https://cplusplus.com/ are good sources) .

Investing time in having a good grasp on the syntax, grammar (understanding statements) and the role of types is going a long way. Don’t cut corners there.

Of course if you apply your skills in the arduino world and have questions, the forum is also happy to help out.

Have fun with C++!

All errors are caused by humans. The less we touch it, the better. If you understand this, you will solve most of your problems. Your wiring is the problem. Probably the COM pin.

Files for WOKWI.COM...

sketch.ino
// Include the required library for 7-segment display
#include <SevSeg.h>

// Pin definitions for the 7-segment display
#define NUM_DIGITS 4
#define DIGIT_1 2
#define DIGIT_2 3
#define DIGIT_3 4
#define DIGIT_4 5
#define SEGMENT_A 6
#define SEGMENT_B 7
#define SEGMENT_C 8
#define SEGMENT_D 9
#define SEGMENT_E 10
#define SEGMENT_F 11
#define SEGMENT_G 12

// Pin for the pushbutton
#define BUTTON_PIN 13

// Define the timer duration in milliseconds (7 minutes)
#define TIMER_DURATION 420000

// Create an instance of the SevSeg library
SevSeg sevseg;

// Global variables
unsigned long startTime = 0;
bool timerRunning = false;

void setup() {
  // Initialize the display
  byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
  byte segmentPins[] = {SEGMENT_A, SEGMENT_B, SEGMENT_C, SEGMENT_D, SEGMENT_E, SEGMENT_F, SEGMENT_G};
  sevseg.begin(COMMON_CATHODE, NUM_DIGITS, digitPins, segmentPins);

  // Set up the button pin as input
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  // Check if the button is pressed
  if (digitalRead(BUTTON_PIN) == LOW && !timerRunning) {
    // Start the timer
    startTime = millis();
    timerRunning = true;
  }

  // Check if the timer is running
  if (timerRunning) {
    // Calculate the elapsed time
    unsigned long elapsedTime = millis() - startTime;

    // Check if the timer has reached the duration
    if (elapsedTime >= TIMER_DURATION) {
      // Timer expired, reset
      timerRunning = false;
    }

    // Convert elapsed time to minutes and seconds
    int minutes = (TIMER_DURATION - elapsedTime) / 60000;
    int seconds = ((TIMER_DURATION - elapsedTime) / 1000) % 60;

    // Display the time on the 7-segment display
    sevseg.setNumber(minutes * 100 + seconds, 2); // Display only minutes and seconds
    sevseg.refreshDisplay();
  }
}

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -14.4, "left": -154.1, "attrs": {} },
    {
      "type": "wokwi-7segment",
      "id": "sevseg1",
      "top": -147.78,
      "left": -27.68,
      "attrs": { "digits": "4", "common": "cathode" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 6.2,
      "left": 28.8,
      "attrs": { "color": "green" }
    }
  ],
  "connections": [
    [ "sevseg1:DIG1", "nano:2", "green", [ "v-9.6", "h-67.19" ] ],
    [ "sevseg1:DIG2", "nano:3", "green", [ "v-19.2", "h-105.6" ] ],
    [ "sevseg1:DIG3", "nano:4", "green", [ "v-28.8", "h-134.4" ] ],
    [ "sevseg1:DIG4", "nano:5", "green", [ "v47.16", "h-153.6" ] ],
    [ "sevseg1:A", "nano:6", "green", [ "v-38.4", "h-105.6" ] ],
    [ "sevseg1:B", "nano:7", "green", [ "v-48", "h-163.2" ] ],
    [ "sevseg1:C", "nano:8", "green", [ "v27.96", "h-163.2" ] ],
    [ "sevseg1:D", "nano:9", "green", [ "v18.36", "h-153.6" ] ],
    [ "sevseg1:COM", "nano:GND.2", "black", [ "v56.76", "h-124.8" ] ],
    [ "sevseg1:G", "nano:12", "green", [ "v37.56", "h-211.2" ] ],
    [ "sevseg1:F", "nano:11", "green", [ "v-57.6", "h-144" ] ],
    [ "sevseg1:E", "nano:10", "green", [ "v8.76", "h-153.59" ] ],
    [ "nano:GND.1", "btn1:2.l", "black", [ "v0" ] ],
    [ "nano:13", "btn1:1.l", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

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