Hi, I´m creating a counter with a push button. I already have a piece of code that does the trick, but I´ll be using it many times in another project so I want to turn it into a function. The original code just prints when the push button is pressed, but the function keeps printing even when not pressed. How can I solve the problem???
Orignial Code
int buttonPin = 5;
int duration;
int startTime;
int endTime;
byte timerRunning;
void setup(){
pinMode (buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
// buttonPin pressed & timer not running already
if (timerRunning == 0 && digitalRead(buttonPin) == LOW){
startTime = millis();
timerRunning = 1;
}
// timer running, buttonPin released
if (timerRunning == 1 && digitalRead(buttonPin) == HIGH){
endTime = millis();
timerRunning = 0;
duration = (endTime - startTime)/1000;
Serial.print ("buttonPin press time in seconds: ");
Serial.println (duration);
}
}
Function Code
int buttonPin = 5;
int duration;
int startTime;
int endTime;
int timerRunning;
void setup(){
pinMode (buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
int dt;
dt = TimeBetPre(buttonPin);
Serial.print ("buttonPin press time in seconds: ");
Serial.println (dt);
//delay (1000);
}
int TimeBetPre(int buttonPin){
// buttonPin pressed & timer not running already
if (timerRunning == 0 && digitalRead(buttonPin) == LOW){
startTime = millis();
timerRunning = 1;
}
// timer running, buttonPin released
if (timerRunning == 1 && digitalRead(buttonPin) == HIGH){
endTime = millis();
timerRunning = 0;
duration = (endTime - startTime)/1000;
}
//
return duration;
}
Chris is right about the behaviour of your posted code.
What your function does is:
if time-measuring not yet started and then button gets pressed take snapshot of time
if time-measuring is active and button gets released take second snapshot of time and calculate duration.
Due to the fact that your variables
startTime
endTime and duration are defined as global
and
that you placed the command
return duration
outside the if-conditions whenever you call your function tha last calculated duration is given back.
You have written that you want to use this time-measuring multiple times
I doubt that you just want to re-call last measured duration multiple times.
You should describe in normal words and in detail what the functionality shall be.
I emphasise in normal words! You are a beginner. This means you have (until today) a rather small knowledge about programming. It will be much easier for you to describe your future usage of the "time-measuring-function" in normal words than trying it as code or pseudo-code.
You should describe it giving an overview and describe in detail.
If you keep it short all that happens is slowing down caused by asking back-postings for the overview and the details.
So IMHO becoming a pro starts with giving an overview and the details of your project.
What do you want your function to do if the button has not yet been pressed? Should it return a value (like 0 or -1) or should it not return and prevent your sketch from doing anything else until the button is pressed?
What do you want your function to do if the button has been pressed but has not yet been released? Should it return a value (like 0 or -1 or elapsed press time) or should it not return and prevent your sketch from doing anything else until the button is released?
By 'many times' do you mean for many different buttons (different input pins)? If so, would I be correct in guessing that each button should have its own timer?