Creating a lookup/truth table

I’m trying to turn the attached truth tables into code within arduino IDE, but I can’t seem to get close to what I want. Any help would be appreciated.
So the user will input a value from 7GHz to 11GHz using the serial monitor. A 2GHz band will need to be outputted. So for example, the user inputs 8GHz

8-1= 7GHz for High pass side
8+1= 9GHz for Low pass side.

Now obviously there isn’t a set value for both so will need to look for the closest value to those but cannot be lower, for the high pass side, or higher, for the high pass side. That being 7.3 for the High pass side and 8.85 for the Low pass side.

I have tried making the Values into set binary and then turning them into an array, and then splitting the them into individual digits. I’ve only done four to start off with;

const 6 = 0000;
const 6.05 = 0001;
const 6.15 = 0010;
const 6.2 = 0011;

int myArray [] = {6, 6.05, 6.15, 6.2};

Any ideas?
I’ve attached the truth tables within an pdf document.
If you need this further explaining I’ll try my best to answer, but please bear with me.

Truth tables.pdf (73 KB)

ints do not have fractional parts.

const 6 = 0000;
const 6.05 = 0001;
const 6.15 = 0010;
const 6.2 = 0011;

I have no idea what that is, but beware of octal literals.

Yes I had a feeling that wouldn't work, worth a try though, hopefully I can somehow get my point across.

Please make another attempt to describe as clearly as possible what you want to do, not what you did.

The truth table appended to your post is useless for making decisions, because floating point numbers do not, in general, have an exact representation. For example, it is not possible to represent 0.1 exactly using floating point.

I see what you’re up to. It is totally unworkable. Also there are really no truth tables involved, which has confused everyone. But the answer is really simple. Consider low side first. If you create an array of values, the index into the array will correspond to the values. Forget about the fact that the values are binary. That doesn’t matter because a number is always a number:

float lowSideValues[] = {6.0, 6.05, 6.15... [the rest] ... 9.5};

now we will find the place in the table

float value = 7.0;
index = 0;
while (value < lowSideValues[index]) index++;
// now the result code is stored in index
// so we can remember it:
result = index;

Supposing as above, the input value is 7.0. In the while loop, index will increment until value > lowSideValues[9]. ‘index’ now contains the value 9, the loop has terminated and we go on and store it in result. ‘result’ == 9, but in binary that is 1001, which gives you exactly the bits that you need.

The high side can be done exactly the same way.

I am not sure what the application is, for example,
A) user enters value, Arduino replies with value via serial monitor
B) user enters value, arduino does stuff based on value entered.

if A as all you are doing, you can spend your code writing a complicated mathematical model that accurately responds via the serial monitor.

if B, it's very likely you can cut out all the middle garbage, when you realize the Arduino is a integral processor, without a arithmetic logic unit. you can map the input value to integral values and process them with much easier math from there. once you have mapped to integral values, you have the index to your array for your lookup table.

aarg, thank you, I don't know why I didn't see that in the first place. Many thanks. Also thank you for understanding my ridiculous question. So I guess my next question is how do you split the number into its separate binary digits?
For example; index = 10 //10 in binary is 1010

How do you split that into its separate digits?

Variable1 = 1
Variable2 = 0
Variable3 = 1
Variable4 = 0

bitRead?

C / C++ has a complete set of Bitwise Operators

Just tried that code and I’m getting a fair few errors. Am I putting stuff in the right place?

float lowSideValues[] = {6.0, 6.05, 6.15, 6.5, 7.2, 8.1, 9.5};
float value = 7.0;

void setup(){
Serial.begin(9600);
}
void loop()
{
index = 0;
while (value < lowSideValues[index]) index++;
result = index;
Serial.println(index);
}

I made some mistakes...try

float lowSideValues[] = {6.0, 6.05, 6.15, 6.5, 7.2, 8.1, 9.5};
float value = 7.0;
int index;
int result;

void setup(){
Serial.begin(9600);

index = 0;
while (value > lowSideValues[index]) index++;
result = index;
Serial.println(result);
}

void loop() {}

By the way, I coded this with greater than '>' without fully analyzing your requirements (in fairness you haven't really expanded on that). Depending on how you want to "slice it", you might need to use greater than or equal to '>='. Have a look and see which works for you. As it is now written, it will increment the index until a table value greater than the input value is found. Thus the table values represent the upper bound of each range.

In this case, when it tests (7.0 > 7.2) the index value is 4, and it will exit the while loop with that value. That may not be the way you want to partition the ranges. Here, the index of 4 indicates that the input value is greater than 6.5 and less than or equal to 7.2. I am not trying to give you a complete solution, just a method that you can adjust to your needs.

You also have to add protection checks so the index doesn't go out of bounds if a match is not found (for example if the input value is 42). That would be a guaranteed system crash, or freeze in an endless loop. Since the first value, 6.0 is an upper bound, any number greater than minus infinity and less than 6.0, will yield an index value of 0.

For me the serial monitor just constantly says 0.
Plus would the functions still work in the void loop ()

I was busily editing the code to change that mistake. It works now, prints "4". Yes, of course they will work in loop(), but if you put it there now by itself, it will just print the same number over and over and over... surely you will have some way to collect values to feed to it...

42?

a7

alto777:
42?

a7

Reflecting the deep thought it would go into... :slight_smile: