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() {
lcd.begin(16,2);
pinMode(upBtn, INPUT_PULLUP);
pinMode(downBtn, INPUT_PULLUP);
pinMode(startBtn, INPUT_PULLUP);
pinMode(endBtn, INPUT_PULLUP);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Minutes : ");
lcd.print(btnPushCounter);
}
void loop() {
checkUp();
checkDown();
// countDown();
if(bPress){
bPress = false;
displayNumber();
}
}
void displayNumber(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Minutes : ");
lcd.print(btnPushCounter);
}
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;
btnPushCounter++;
}
delay(50);
}
// 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;
btnPushCounter--;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// 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.setCursor(2,7);
lcd.print(minutes);
lcd.print(" ");
lcd.print(seconds);
delay(50);
timeRemaining = timeLimit - millis();
}
}
1 Answer 1
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.
i am working on project ABC, here i my code
.... we need to see something like thisi 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?
countDown()
intocheckStart()
?