Need Program for Stopwatch for Arduino UNO

Hello everyone,

I need a stopwatch program with Stop/Reset with single Push button input for Arduino UNO with MAX7219 LED Dot Matrix 4 In 1 Display Module. Can anyone please help me out? I am new to programming Arduino UNO. Please help me out.

Thanks

when is your homework due?

1 Like

This forum works this way:

You make a first own attempt. What ever error the code might have
post your sketch as a code-section and ask specific questions.

If you don't know how to write code at all

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

1 Like

Got this Program from my friend but its not working.

#include <LedControl.h

// Define the LED matrix pins
const int dinPin = 2;
const int csPin = 3;
const int clkPin = 4;

// Define the push button pin
const int buttonPin = 5;

// Define the LED matrix object
LedControl lc = LedControl(dinPin, clkPin, csPin, 1);

// Define variables for the timer
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool isRunning = false;
bool wasRunning = false;
int buttonState = HIGH;
int lastButtonState = HIGH;

void setup() {
// Initialize the LED matrix
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);

// Initialize the push button
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
// Read the button state
buttonState = digitalRead(buttonPin);

// Check if the button has been pressed
if (buttonState == LOW && lastButtonState == HIGH) {
// If the timer is running, stop it
if (isRunning) {
elapsedTime = millis() - startTime;
isRunning = false;
wasRunning = true;
}
// If the timer is not running, start it
else if (!isRunning && !wasRunning) {
startTime = millis();
isRunning = true;
}
// If the timer was running and is now stopped, reset it
else if (!isRunning && wasRunning) {
elapsedTime = 0;
wasRunning = false;
}
delay(50); // Debounce the button press
}

// Update the last button state
lastButtonState = buttonState;

// Display the elapsed time on the LED matrix
if (isRunning) {
elapsedTime = millis() - startTime;
}
int minutes = elapsedTime / 60000;
int seconds = (elapsedTime % 60000) / 1000;
int tens = seconds / 10;
int units = seconds % 10;
lc.setChar(0, 0, '0' + minutes / 10, false);
lc.setChar(0, 1, '0' + minutes % 10, false);
lc.setChar(0, 2, ':', false);
lc.setChar(0, 3, '0' + tens, false);
lc.setChar(0, 4, '0' + units, false);

delay(10); // Update the display every 10ms
}

This is a too short description.
What do you observe if you run this program on your arduino Uno?

Does the code compile?
if the code does not compile

In general you should post code as a code-section. Here is a 3 minutes tutorial how to do that

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

This is the program I have simulated in wokwi.com

#include <LedControl.h>

// Define the LED matrix pins
const int dinPin = 2;
const int csPin = 3;
const int clkPin = 4;

// Define the push button pin
const int buttonPin = 5;

// Define the LED matrix object
LedControl lc = LedControl(dinPin, clkPin, csPin, 4);

// Define variables for the timer
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool isRunning = false;
bool wasRunning = false;
int buttonState = HIGH;
int lastButtonState = HIGH;

void setup() {
  // Initialize the LED matrix
  for (int i = 0; i < 4; i++) {
    lc.shutdown(i, false);
    lc.setIntensity(i, 8);
    lc.clearDisplay(i);
  }

  // Initialize the push button
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Read the button state
  buttonState = digitalRead(buttonPin);

  // Check if the button has been pressed
  if (buttonState == LOW && lastButtonState == HIGH) {
    // If the timer is running, stop it
    if (isRunning) {
      elapsedTime = millis() - startTime;
      isRunning = false;
      wasRunning = true;
    }
    // If the timer is not running, start it
    else if (!isRunning && !wasRunning) {
      startTime = millis();
      isRunning = true;
    }
    // If the timer was running and is now stopped, reset it
    else if (!isRunning && wasRunning) {
      elapsedTime = 0;
      wasRunning = false;
    }
    delay(50);  // Debounce the button press
  }

  // Update the last button state
  lastButtonState = buttonState;

  // Display the elapsed time on the LED matrix
  if (isRunning) {
    elapsedTime = millis() - startTime;
  }
  int seconds = elapsedTime / 1000;
  int minutes = seconds / 60;
  seconds = seconds % 60;

  // Display the minutes
  lc.setChar(0, 0, '0' + minutes / 10, false);
  lc.setChar(0, 1, '0' + minutes % 10, false);
  lc.setChar(0, 2, ':', false);

  // Display the seconds
  lc.setChar(0, 3, '0' + seconds / 10, false);
  lc.setChar(0, 4, '0' + seconds % 10, false);

  // Copy the display to the other modules
  for (int i = 1; i < 4; i++) {
    for (int j = 0; j < 8; j++) {
      lc.setChar(i, j, lc.setChar(0, j), false);
    }
  }

  delay(10);  // Update the display every 10ms
}


Got this errors

Firstly I have thought to simulate the Program and if the program works then I can dump it in the Arduino UNO.

still some things to learn. You should NEVER post pictures like screenshots.
Post error-messages as code-sections just as described in the tutorial.

Another important thing: post detailed information.
Working on WOKWI is a very importat detail. You can go on always posting quick and short and this will just delay solving your problems because you will get a lot of asking back questions you have to answer to get real support.

So it is about time
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

      lc.setChar(i, j, lc.setChar(0, j), false);

What is the third parameter of this call to lc.setChar() doing ?

Hello Sir,

Sir,i have no idea about the program as it was shared by my friend

Please help me in getting stopwatch program with Stop/Reset with single Push button input for Arduino UNO with MAX7219 LED Dot Matrix 4 In 1 Display Module.

The Program should display MM:SS: MS on the MAX7219 LED Dot Matrix 4 In 1 Display Module.

I have tried multiple pre-defined programs from https://wokwi.com but failed to execute,Please support me in this issue.

Thanks

The minimum that you can do is share the real links to these wokwi simulations

Please be honest with us. Is this "friend" an AI like ChatGPT" ?

Yes sir its ChatGPT only

ChatGPT gives the impression it is very confident about it's answers, but it should not be because it has been trained on answers that are both correct and incorrect and it is not able to distinguish which are correct.

This means any code given by an AI must be carefully checked by an expert coder, to find any errors or misunderstanding by the AI. As a beginner, you will not be able to find these errors.

Please go back and fix your code tags in post #4.

+1

You are not really seeking for help, right? You are looking someone to do your project. is it a school project ? can you give us more information on context ? why do you need this ? if you can't program and don't want to learn, there are really really cheap stop watch from the far east..

If you really want help then read this and take the adice given there to your heart

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

The code in the wokwi simulation does work.
first button press starts stopwatch second press pauses stopwatch
only thing that is missing is the resetting to zero.

If you are unable to write down a new prompt that asks chatGPT to add the reset
you are really not worth beeing helped in any way.

I am pretty sure that you are able to write such a prompt.
It seems that you are too impatient or too lazy to do so.

I have written such a prompt and chatGPT created code that works.
But I will never post this code for you. Becaus part of education is
helping to be able to do it yourself
developing patience and perseverance.

If everyone on this planet behaved like you, human civilization would be doomed. There would be no one left who would have control over technology and artificial intelligence. And I believe that this process of ever-increasing stupidity must be stopped.

1 Like