I don't see any clear problem but I don't know what the data should look like. The code does look reachable. How do you know its "not working" and can you be more specific about the actual symptom of the problem?
int printLDRaverage(short unsigned pin, short unsigned channel, long delayTime) {
Then this:
static long PLDRAaverage[maxChannels];
And the return instruction is: return PLDRAaverage[channel];
so, if you have more than 65536 32768 in the PLDRAaverage for that channel, it won't work.
Have you tried getting your variables right? casting or actually changing the variables would probably fix your problem.
either this
static int PLDRAaverage[maxChannels];
or this
return (int) PLDRAaverage[channel];//won't work if variable holds a value bigger than 32,768
Will make a difference. Give it a try.
P.S.: you have the same "problem" in the previous function. you define a return type of int and return a boolean.
well this is working now so thanks but I gotta another problem, but before i get to that I'll tell you what its intended for.
printLDRaverage is intended to to print the average of all the values recorded from an LDR between the delays times. Obviously CountDownInt is designed as a delay non delay and is based in this code http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.
What happened before was it would return 0, even if the average was calculated correctly.
Any way now the value returned is growing and growing and growing
here's the code again
int CountDownBool(boolean setorwatch, int channel, long delayTime) {
static long premil [maxChannels];
static long currentmils [maxChannels];
static boolean inttoset [maxChannels];
currentmils [channel] = millis();
if (currentmils [channel] - premil [channel] > delayTime) {
premil[channel] = currentmils[channel];
inttoset[channel] = false;
return setorwatch;
}
}
int printLDRaverage(short unsigned pin, short unsigned channel, long delayTime) {
static boolean PLDRAonOrOff[maxChannels];
static long PLDRAread[maxChannels];
static int PLDRAreading[maxChannels];
static int PLDRAaverage[maxChannels];
static boolean intialize[maxChannels];
static boolean PLDRAReturn[maxChannels];
if (intialize[channel] == false) {
PLDRAread[channel] = 0;
PLDRAreading[channel] = 0;
intialize[channel] = true;
PLDRAonOrOff[channel] = false;
}
PLDRAread[channel] = PLDRAread[channel] + analogRead(pin);
PLDRAreading[channel]++;
if (PLDRAonOrOff[channel] == false){
PLDRAonOrOff[channel] = true;
CountDownBool(true, channel, delayTime);
PLDRAaverage[channel] = (PLDRAread[channel] / PLDRAreading[channel]);
Serial.println(PLDRAaverage[channel],DEC);
PLDRAread[channel] = 0;
PLDRAreading[channel] = 0;
PLDRAReturn[channel] = true;
}
PLDRAonOrOff[channel] = CountDownBool(false, channel, delayTime);
if (PLDRAReturn[channel] == true) {
PLDRAReturn[channel] = false;
Serial.println(8,DEC);
return PLDRAaverage[channel];
}
}
I changed CountDownInt to CountDownBool because I was using boolean instead, but I also modified it ti make more logical, as such I've had to change the way the function is used.
Please point out why it keeps growing
@ AWOL it doesn't return anything if the condition is not met, but thats how I intended it, so its waits until the time is up too set the variable. But CountDownBool is not the function I'm having trouble with, its printLDRaverage.
@bubulindo Yeah there is. The average is only set at every delayTime, and its always reset to the "value reading" / "reading number". I know that's not the case because Serial.println show that the average doesn't grow
AWOL it doesn't return anything if the condition is not met, but thats how I intended it
But you've declared the function with a return type - you must return something, whether or not that's what you intended.
(Hint: it does return something anyway - just not what you may have wanted)
As you can see, you can get away with not returning a value from a function, as long as the rest of your code is aware of when it may or may not use it. It compiles, although many compilers will emit a warning for this behaviour - another strong hint that you should avoid it.
But it's like setting a bear trap in the hallway every time you leave the house. Works fine as long as you remember to disarm it every single time you come home. But if you don't, or worse, someone else needs access to the place, you've caused a serious problem for no good reason.
The compiler acts a little like a drill sergeant in this regard:
"Listen maggot, you have declared this function as returning an int, so you had better damn well return an int, or I will find one to return for you; one that you proabably won't like!"