Need help for a timer projet

Hi,
I'm trying to replicate this timer project (the one with the buttons).

(sorry for the French)

I have 2 problems :

  • the timer does not want to restart if the remaining time is less than 1 minute;
  • I would like to do the project on an Arduino Mega 2560, but when testing it with the same
    pin layout, it doesn't work.

The arduino uno version:

The arduino mega 2560 version:

#include "Wire.h"
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C LCD(0x27, 16, 2);

boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;

byte w = 0;

int SEC = 0;
int MIN = 0;
unsigned long timer;

void setup() {
   pinMode(2, INPUT_PULLUP);
   pinMode(4, INPUT_PULLUP);
   pinMode(6, INPUT_PULLUP);
   pinMode(8, INPUT_PULLUP);

   pinMode(10, OUTPUT);
   pinMode(12, OUTPUT);
   digitalWrite(10, HIGH);

   LCD.init();
   LCD.backlight(); 

   LCD.setCursor(2, 0);
   LCD.print("TIMER  STOP");
   LCD.setCursor(5, 1);
   LCD.print(MIN);
   LCD.print(" : ");
   LCD.print(SEC);
}

void loop() {
   boolean button1IsUp = digitalRead(2);
   boolean button2IsUp = digitalRead(4);
   boolean button3IsUp = digitalRead(6);
   boolean button4IsUp = digitalRead(8);

    if (button1WasUp && !button1IsUp) {
      delay(10);
      button1IsUp = digitalRead(2);
      if (!button1IsUp) {
         MIN = MIN - 1; SEC = 0;
         if (MIN < 0) { MIN = 0; }
         LCD.clear(); 
         LCD.setCursor(2, 0); 
         LCD.print("TIMER STOP"); 
         LCD.setCursor(5, 1); 
         LCD.print(MIN); 
         LCD.print(" : "); 
         LCD.print(SEC);
      }
   } 
   button1WasUp = button1IsUp;
   
   if (button2WasUp && !button2IsUp) {
      delay(10); 
      button2IsUp = digitalRead(4); 
      if (!button2IsUp) { 
         MIN = MIN + 1; SEC = 0; 
         LCD.clear(); 
         LCD.setCursor(2, 0); 
         LCD.print("TIMER STOP"); 
         LCD.setCursor(5, 1); 
         LCD.print(MIN); 
         LCD.print(" : "); 
         LCD.print(SEC);
      }
   } 
   button2WasUp = button2IsUp;

   if (button3WasUp && !button3IsUp && MIN > 0) {
      delay(10);
      button3IsUp = digitalRead(6);
      if (!button3IsUp) {
         if (SEC == 0) { SEC = 60; MIN = MIN - 1; }
         if (MIN < 0 ) { MIN = 0; }
         digitalWrite(10, LOW);
         w = 1;
      }
   }
   button3WasUp = button3IsUp;

   if (button4WasUp && !button4IsUp) {
      delay(10);
      button4IsUp = digitalRead(8);
      if (!button4IsUp) {
         MIN = 0; SEC = 0;
         digitalWrite(10, HIGH);
         LCD.clear();
         LCD.setCursor(2, 0);
         LCD.print("TIMER STOP");
         LCD.setCursor(5, 1);
         LCD.print(MIN);
         LCD.print(" : ");
         LCD.print(SEC);
      }
   }
   button4WasUp = button4IsUp;

   while (w == 1 ) {
      if (millis() - timer > 995) {
         timer = millis();
         SEC = SEC - 1;

         if (SEC == 0 && MIN == 0) {
            LCD.clear();
            LCD.setCursor(2, 0);
            LCD.print("TIMER  STOP");
            LCD.setCursor(5, 1);
            LCD.print(MIN);
            LCD.print(" : ");
            LCD.print(SEC);
            digitalWrite(10, HIGH);
            tone(12, 100);
            delay(500);
            noTone(12);
            w = 0;
         }

         if (SEC == 0) {
            SEC = 59; MIN = MIN - 1;
            if (MIN < 0 ) { MIN = 0; }
         }

         if (w == 1) {
            LCD.clear();
            LCD.setCursor(2, 0);
            LCD.print("TIMER START");
            LCD.setCursor(5, 1);
            LCD.print(MIN);
            LCD.print(" : ");
            LCD.print(SEC);
         }
     }
 
     boolean button3IsUp = digitalRead(6);
     if (button3WasUp && !button3IsUp) {
        delay(10);
        button3IsUp = digitalRead(6);
        if (!button3IsUp) {
           LCD.setCursor(2, 0);
           LCD.print("TIMER  STOP");
           w = 0;
        }
     }
     button3WasUp = button3IsUp; 
   }
}

Thank you

If You use a French project as presentation posting in the French forum section would likely make more helpers take on Your question.

In my experience, posting in english usually gives me more answers.

I looked at the comments on my project and indeed there was a comment that said that the SDA (A4) and the SCL (A5) should be plugged into communication 20 and 21 respectively.

Now, there is still the problem of the timer that cannot be restarted when there is less than 1 minute on the clock.

In addition, the project dates from 2021 and the last comment from 2022. I have little hope of being answered, but I'm going to give it a shot anyway.

Hardly when using a French project description. Using an English description, I can imagine You're right.

What does that mean? New terminology to me.

Wokwi is not what I dig into, but other helpers do. Good luck!

I had to plug the sda and scl into different pins when using an arduino mega.

Why? I2C is a bus handling up to 127 devices.
Maybe OK but only I2C capable pins will do the job. Every digital pin will not work. I have a never used Mega and can't tell If You do the right or wrong thing.

I for one would spend more time on this kind of code if you

  • named the pins the buttons are on and used those names

  • described what each button is supposed to do if the names weren't clear enough

  • used better names than button3WasUp


What's the roll of button3, here in a section where MIN must be > 0?

   if (button3WasUp && !button3IsUp && MIN > 0) {
      delay(10);
      button3IsUp = digitalRead(6);
      if (!button3IsUp) {
         if (SEC == 0) { SEC = 60; MIN = MIN - 1; }
         if (MIN < 0 ) { MIN = 0; }
         digitalWrite(10, LOW);
         w = 1;
      }
   }
   button3WasUp = button3IsUp;

a7

Indeed. Something that might fall right out of that change would be accuracy of the one second intervals to the best the microprocessor can provide.

a7

I've understood that from reading the replies to Mega questions here, therefore my reservation. Moving pins I know but to which pins? Maybe the OP did the right thing. Checking a second time never hurts I think.

I'm new to arduino. I did an internet search to find a project that met my needs. I didn't code it (I don't know how to code). If you tell me it's poorly written, you know it better than I do.

I would not recommend this sketch unless someone is desperately searching for a good example of bad coding... :wink:

  • unnecessary code repetitions
  • "magic" pin numbers
  • awkward button functions
  • while loop in loop()
  • double local declaration of boolean button3IsUp in loop()

Surprisingly the I2C pins for different Arduino controllers are mentioned in a table in the French original link ...

Could be done much more readable and maintainable with a state machine and clear pin and function names ...

Too late today for me, but any volunteers?
ec2021

That's a common approach that mostly gives mothing useful.

Copying code from unknown people, You never know what You get. You're fortunate to come this far.
The aim for forum is helping members to learn and grow in coding. The aim is not being a fixing shop for free.

Get interested in learning coding and learn. That way this hobby will bring You a lot more fun and satisfaction. Coding for 50 years....

1 Like

Maybe it's a good idea to explain your project needs rather than to start with a sketch like this one ...

And what is your personal intention: Are you just interested in a solution for a single application or is this your start to learn coding?

I already have another project that I want to combine with this one. It is a box with 2 buttons and 2 lights (blue and red) and 2 7-segment displays of 4 inch. When the red button is pressed and held, the red light will turn on and the display will begin a 10 seconds countdown. When the button is released, the light and the countdown is reset (vice and verca for blue). I'd like to include a timer for play time. Since I already use an arduino mega, I would found it more elegant to do it with the arduino than just using a kitchen timer.

Let's see if I read the situation right:

You want a timer. But you can't bother to learn to code, so you pull up someone else's timer project from a foreign language Web site and expect us to adapt it to your needs. That, and it is not even clear how this timer project is supposed to work: I tried the Arduino Uno version you gave us in post #1, and I couldn't even get the timer to run.

This happens often here. People who can't bother to learn to code give us code that they don't understand, and can't be bothered to try to understand, that they expect us to adapt to their needs, thinking it will be an easy fix, even when the only sensible solution would be to throw everything away and start from scratch. That, and if you expect code to be written for you, for free, you will probably be disappointed.

As far as Arduino projects go, a timer is not a hard one to code. You might, however, find it a bit of a challenge to make this timer run at the same time as your 7-segment display countdown is running. I am sure that that is the next thing you will want: you will want to make both this timer and the 7-segment display countdown run at the same time, and you will find this to be harder than you anticipated.

Combining two projects to run on the same machine, at the same time, requires that you know what you are doing. If you cannot be bothered to take the time to learn, then that is what the Jobs and Paid Consultancy forum category is for. Here is a link to that category: Jobs and Paid Consultancy - Arduino Forum

Yes you do.

You could have mentioned it by link, or actually just continued over there.

a7

Hello lolendo

What are the names and function of these I/O pins?

grafik

Sorry, my mistake, I didn't know there was this category. I did not intend to insult anyone or not pay for the work.
What would be the best way to proceed? Do I post explaining my project in jobs and paid consultancy and wait for a response with a price?

Hi, paulpaulson

Is this answers your question.

void setup() {
   pinMode(2, INPUT_PULLUP); //-1 min
   pinMode(4, INPUT_PULLUP); //+1 min
   pinMode(6, INPUT_PULLUP); //start/stop timer
   pinMode(8, INPUT_PULLUP); //reset

   pinMode(10, OUTPUT); //Red led
   pinMode(12, OUTPUT); //buzzer
   digitalWrite(10, HIGH);

If you're willing to work on my project, we can work out a price.

private message sent already.