I was going to post this in my other thread, but since I marked that as solved, the forum requested that I open a new topic. And since this topic is technically different than the other topic, here it goes...
Having trouble with passing arguments to a function that works if I use it with no return.
IE
void myfunction()
vs
int myfunction(int x, int y)
So here are the actual functions that I had created.
void getRpmData() {
if (rpmNewIsrMicros == true) {
prevRpmMicros = rpmMicros; // save the previous value
noInterrupts();
rpmMicros = rpmIsrMicros;
rpmNewIsrMicros = false;
interrupts();
rpmDuration = (rpmMicros - prevRpmMicros);
}
}
Simply call the function from somewhere else by
getRpmData()
Use the variables inside the function, great. It works...
But I wanted to transform the function into a return and return rpmMicros - prevRpmMicros.
So I created this monstrosity.
uint32_t getRpmData(uint32_t time1, uint32_t time2, uint32_t time3, bool flag) {
if (flag == true) { // if newRpmIsrMicros == true
uint32_t rD = 0;
time2 = time1; // prevRpmMicros = rpmMicros --- Save the last rpm micros
noInterrupts();
time1 = time3; // rpmMicros = rpmIsrMicros --- Set rpm micros to the value from the ISR, ie current time
flag = false;
interrupts();
rD = time1 - time2; // rpmMicros - prevRpmMicros
return rD; // return the above statement
}
}
I called it from the loop with:
rpmDuration = getRpmData(rpmMicros, prevRpmMicros, rpmIsrMicros, rpmNewIsrMicros);
But after some debugging I figured out that all the arguments pass as 0. So the return ends up being an incrementing of time, and that is it. No usable data.
My assumption is it has something to do with how fast the variables change, but I'm lost...