project I'm trying to build this project with a simple code. Object is to hit up button to increment minutes or down to decrement minutes then hit start to start countdown, while its counting relay will turn On light till counting is done will turn light off and if I hit end button while counting it will end counting and turn off relay.

Here is my try I would appreciate if you could support me. i can not move to countDown() function in the loop if i press start I need help to figure out a good technique of declaring functions in void loop in arduino

#include <LiquidCrystal.h>

LiquidCrystal lcd(1,2,4,5,6,7);

#define numberOfSeconds(num) ((num/1000) % 60 )
#define numberOfMinutes(num) (((num/1000) / 60) % 60)

//unsigned long timeLimit = 3600000; // 1000 ms in 1sec, 60secs in 1min, 60mins in 1hr. so , 1000x60x60 = 3600000ms = 1hr

const int upBtn = 8;
const int downBtn = 9;
const int startBtn = 10;
const int endBtn = 11;

int btnPushCounter = 0;
int minutes;
int seconds;
unsigned long minutesGiven;
unsigned long timeLimit;
unsigned long timeRemaining;

int upBtnState = 0;
int lastUpBtnState = 0;

int downBtnState = 0;
int lastDownBtnState = 0;

int startBtnState = 0;
int lastStartBtnState = 0;

int endBtnState = 0;
int lastEndBtnState = 0;

bool bPress = false;

void setup() {

 pinMode(upBtn, INPUT_PULLUP);
 pinMode(downBtn, INPUT_PULLUP);
 pinMode(startBtn, INPUT_PULLUP);
 pinMode(endBtn, INPUT_PULLUP);

 lcd.print("Minutes : ");
void loop() {

//    countDown();
      bPress = false;

void displayNumber(){ 
    lcd.print("Minutes : ");

void checkUp(){
  upBtnState = digitalRead(upBtn);
  // compare the buttonState to its previous state
  if (upBtnState != lastUpBtnState) {
    // if the state has changed, increment the counter
    if (upBtnState == HIGH) {
        bPress = true;
  // save the current state as the last state, for next time through the loop
  lastUpBtnState = upBtnState;

void checkDown(){
  downBtnState = digitalRead(downBtn);

  // compare the buttonState to its previous state
  if (downBtnState != lastDownBtnState && btnPushCounter != 0 ) {
    // if the state has changed, increment the counter
    if (downBtnState == HIGH) {
        bPress = true;
    // Delay a little bit to avoid bouncing
  // save the current state as the last state, for next time through the loop
  lastDownBtnState = downBtnState;

void checkStart(){
  startBtnState = digitalRead(startBtn);
  if(startBtnState != lastStartBtnState){
    if(startBtnState == HIGH){
      bPress = true;
//      lcd.

void countDown() {
//  unsigned long timeLimit = 3600000;
  minutesGiven = btnPushCounter;
  timeLimit = minutesGiven * 1000 * 60;
  timeRemaining = timeLimit - millis();
  while(timeRemaining > 0){
    seconds = numberOfSeconds(timeRemaining);
    minutes = numberOfMinutes(timeRemaining);
    lcd.print(" ");
    timeRemaining = timeLimit - millis();
  • what is your question? – jsotola Jul 13 '19 at 7:41
  • just updated have a look please , thanks – Ahmed Younes Jul 13 '19 at 8:29
  • still no question being asked .... let me say it differently .... you posted i am working on project ABC, here i my code .... we need to see something like this i am working on project ABC, here i my code. i expect the program to do XYZ, but it does ZYX. how do i debug a problem like that? – jsotola Jul 13 '19 at 19:18
  • I think you are not searching for a way to declare function, but for the logic code, that you need to call them in the right situations. Is that correct? – chrisl Jul 13 '19 at 20:39
  • And why don't you simply put countDown() into checkStart()? – chrisl Jul 13 '19 at 20:41

I think what you're looking for is to declare your functions near the top of your file, usually before the first function you write, setup(), in this case. So add this just above your setup() function:

// Forward function declarations
void displayNumber(void);
void checkUp(void);
void checkDown(void);
void checkStart(void);
void countDown(void);

C compilers (and thus, C++ because it was derived from C) read the source code only once through, so any function must become known to the compiler ahead of where it is first called. This would require you to place all low-level functions first, then all functions that call those, then the functions that call those, ..., etc. And there are some cases that even this arrangement won't solve, e.g., functions that call each other (oww, my brain hurts!!). So function declarations are used to avoid this inconvenience.

Declaring a function just means informing the compiler of its name, the parameter types, and the return type -- the actual function code isn't required at this point.

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.