2

I have an Arduino project to do, which requires a countdown timer(which I've already done). I connected a push button, and when I press that button I need it to increase my countdown timer by +10 seconds.

Can anyone help me with adding the code for this button?

Here is the code so far(I declared the button, but I don't know if it's any good):

#include <LiquidCrystal.h>

int buttonPin = 7;
int buttonState = 0;
int seconds = 30; // count seconds
int minutes = 0; // count minutes
int hours = 0; // count hours
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // pins connected to LCD

void setup()
{
  lcd.begin(16, 2); //set up the LCD's number of columns and rows
  pinMode(buttonPin, INPUT);
}
void loop()
{
  buttonState = digitalRead(buttonPin);
  lcd.setCursor(1, 0);
  lcd.print ("Sosire in:");
  lcd.setCursor(6, 1);
  lcd.print(":");
  lcd.setCursor(9, 1);
  lcd.print(":");

  seconds--;
  delay(1000);

 if (seconds < 0)
  {
    minutes--;
    seconds = 59;
  }
 if (minutes < 0)
  {
    hours--;
    minutes = 59;
  }
 if (hours < 0)
  {
    hours = 0;
    minutes = 0;
    seconds = 30;
  }
 if (minutes > 9)
  {
    lcd.setCursor(7, 1);
    lcd.print(minutes);
  }
 else
  {
    lcd.setCursor(7, 1);
    lcd.print("0");
    lcd.setCursor(8, 1);
    lcd.print(minutes);
    lcd.setCursor(9, 1);
    lcd.print(":");
  }

 if (seconds > 9)
  {
    lcd.setCursor(10, 1);
    lcd.print(seconds);
  }
 else
  {
    lcd.setCursor(10, 1);
    lcd.print("0");
    lcd.setCursor(11, 1);
    lcd.print(seconds);
    lcd.setCursor(12, 1);
    lcd.print(" ");
  }

 if (hours > 9)
  {
    lcd.setCursor(4, 1);
    lcd.print(hours);
  }
 else
  {
    lcd.setCursor(4, 1);
    lcd.print("0");
    lcd.setCursor(5, 1);
    lcd.print(hours);
    lcd.setCursor(6, 1);
    lcd.print(":");
  }
 if (seconds == 0)
  {
    lcd.clear();
    lcd.print("Este in statie");
    delay(5000);
    lcd.clear();
  }

}
  • Your code is very hard to read, please align it (parenthesis under each other and code within indented properly). – Michel Keijzers Jun 26 at 16:38
  • You should always name your variables for readability. Instead of "H", "M" and "S", use English names like "hours", "minutes", and "seconds". It will make it a lot easier for other people to help you, and also help you read your own code! – HavocRC Jun 26 at 17:41
  • My program works correctly , but i don't know how to make when i press the button to increase time with 10+ seconds. – Radu Lucian Jun 26 at 17:46
  • Increase which time? You will need to be more specific, you have 3 different 'time' variables, hours, minutes, and seconds. – HavocRC Jun 26 at 18:05
  • Seconds , increase S with 10 seconds when i press the button. – Radu Lucian Jun 26 at 18:08
4

I don't fully understand your program, but I hope to give some guidelines/tips:

  • Instead of using all kind of different variables, with hour, minutes and seconds, with with only one variable that keeps the time. E.g. a long integer value that contains 24 * 60 * hours + 60 * minutes + seconds as one value. E.g. 12:34:56 will be 24 * 60 * 12 + 60 * 34 + 56 = 19,376.
  • Subtracting one or 10 seconds will be as easy as subtracting a value 1 or 10.
  • To calculate back to seconds, minutes and hours, use 3 formulas (assuming time is the time value):
int hours   = (int) time / 60 / 24;
int minutes = (int) (time - hours * 60 * 24) / 60;
int seconds = time % 60;
  • Keep the code to display the LCD separate, probably it's easier to update every digit every second. You probably change it every second, or when the button has been pressed, which is very slow for a MCU.
  • When using a button (you already put it in a variable), just remove 10 seconds, and update the time.
  • However, check also the Blink Without Delay example on the Arduino website, since when you press a button, high likely the button will bounce and one press may result in several times 10 seconds will be subtracted.
  • And as said above, use good and English variable names, and English comments (except the GUI text to be displayed... O zi buna).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.