This is my first post so apologies in advance for any problems I may cause. I am in the process of a project design for college and have encountered a problem. My design involves different values being output for a specific range value that if entered. I am doing this using else if statements, however, i find this taking up a lot of space. I have attached a sample of the code to show what I mean.
Please don't post pictures of code. Post it as text between [code] [/code] tags (or use the code button </>). Your code shouldlook like thisand that also makes it easy to copy to a text editor.
If you organize the sequence of tests properly it will simplify the code. For example
if (num <= 100) {
}
else if (num <= 140) {
}
else if (num <= 180) {
}
// etc
Your idea was tried and tested with the Arduino to good success, however, it still leaves me with a lot of memory being used. I was guided in the direction of a lookup table but due to no set increments in my data, this seems as though it would not be the right direction to pursue.
The best solution is to use an array as a look up table.
One array has the sequence of thresholds in it, the other array holds the output numbers you want.
You search through the first array with a loop looking for a point where the current array value is lower than your target and the next array value is above it. When you find that point your output number is in the other array at that index you found that condition.
It is only a few lines.
A follow up question in regard the lookup table, as my range is between two values is there anyway of doing this or will I have to put in each number individually into my lookup table. My input could be any number between 0-1000, with the output only effected in steps of 40.
You will only need the high end of each range i.e. 100, 140, 180 etc. Store them in increasing order. Iterate through the array until num is less than or equal to the stored array value. Then look up the corresponding value of clicks at the same position in the second array.
The way you were originally doing this doesn't seem like it would be consuming much memory but If space is really a problem I'd be tempted to pursue Robin2's idea and try to find (using Excel perhaps) a formula to derive clicks from num. You may want to consider putting your arrays in progmem q.v. in that scenario if remaining RAM is your problem.
EDIT:
rc( ) returns a byte, not an int as I originally posted.
EDIT:
Also, you can take away the "+ 1" if you delete the "0" from the array. I guess that was some residue I overlooked.
as my range is between two values is there anyway of doing this or will I have to put in each number individually into my lookup table.
I said
One array has the sequence of thresholds in it.
So that would be
int inputThreshold [] = {0, 100 , 140, 180, 220, 260, 300, 340, 380, 420, 460, 500, 540, ..... and so on
The output array like boolrules has it.
Then search the inputThreshold array until you find an index with inputThreshold [i ] greater than your input value and inputThreshold [ i+1 ] less that it. Then the clicks value is just conv[i ].
That will work for any set of numbers.
However boolrules has cleverly spotted that your threshold numbers go up in steps of 40 making your problem a type 1) lookup table. +1 to him.