The code is calling false information

Hi, I am using ESP32 controller. I'm not very good at arduino. According to the code I shared, gr2 information should be displayed on the screen. However, the code finds the gr1 info every time. I could never understand this. Can you help?

#include <LiquidCrystal_I2C.h>                                            
LiquidCrystal_I2C lcd(0x3f, 16,2);

float frequency = 100;
float PotV=50 ;             
float k1=1, k2=2, k3=10, k4=8, k5=3, k6=5;
float f1, f2, f3, f4, f5, f6 ;
float fa1, fa2, fa3, fa4, fa5, fa6 ;
float fb1, fb2, fb3, fb4, fb5, fb6 ;      

void setup()
{
  Serial.begin(115200);    
  lcd.begin();
  lcd.clear();         
  lcd.backlight(); 
}

void loop()
{
f1 = k1 * PotV; f2 = k2 * PotV; f3 = k3 * PotV; 
f4 = k4 * PotV; f5 = k5 * PotV; f6 = k6 * PotV;
 
  fa1 = f1 * 0.9;    fb1 = f1 * 1.1;
  fa2 = f2 * 0.9;    fb2 = f2 * 1.1;
  fa3 = f3 * 0.9;    fb3 = f3 * 1.1;
  fa4 = f4 * 0.9;    fb4 = f4 * 1.1;
  fa5 = f5 * 0.9;    fb5 = f5 * 1.1;
  fa6 = f6 * 0.9;    fb6 = f6 * 1.1;
     
    if (fa1<=frequency<fb1)
     {                            
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:1" );
     }
      
     else if (fa2<=frequency<fb2)
     {                            
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:2" );
     }
     
     else if (fa3<=frequency<fb3)
     {                            
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:3" );
     }
      
     else if (fa4<=frequency<fb4)
     {                                
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:4" );
     }         

     else if (fa5<=frequency<fb5)
     {                                                           
                                 
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:5" );
     }
      
      else if (fa6<=frequency<fb6)
     {                                                           
                                 
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:6" );
     }
      
     else
     {                            
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:7" );
     }
     }

Have you tried printing the values just before the if statements ? Are they what you expect ?

  if (fa1 <= frequency < fb1)

Please explain what this line if code does, or should do

Hello aozdemir58

The sketch won´t compile.

To prevent code duplicates you may check the ussage of structured arrays.

f1a and f1b are the sixteen percent and above ten percent limits of the k values. The frequency value was set to 100. When trading with potv=50 value, the k2 value is; 2x 50=100. For this reason, g2 should be on the screen as it stays within the limits of fa2= 90 and fb2=110. When I look at the screen, it shows g1. I see this on the screen as g1 in the same way in my other frequency tests.
So here is what I want to do; I want to find between which fa and fb values the frequency falls.

I couldn't fully understand this. Is it possible for you to give an example?

As before

Hi, @aozdemir58

Is that your complete code?
Is there some missing off the end?

Thanks.. Tom. :smiley: :+1: :coffee: :australia:

not sure what you're trying to do

  • PotV is a constant, which means f1, ... f6 are constants, and fa1..fa6 and fb1 .. fb6 are constants as well as the same value
  • guessing you meant if (fa1 < frequency && frequency < fb1
  • but since fa1..fa6 are all the same, all the if conditions are either true/false and "GR:1" .. "Gr:6" will all be sequentially printed on the LCD or "Gr:7" will be

what are you trying to do?

1 Like
if (fa1 <= frequency < fb1)

I guess you have a misconception about how if-conditions work

So to avoid misunderstandings explain in normal words what this if-condition shall check.

not a single programming-term ! just normal everyday words

best regards Stefan

1 Like

Psstttt, it is a secret :nerd_face:

Hello, sorry for the late reply.
"if (fa1 <= frequency < fb1)"
you are asking what i am aiming with this code. As I explained above; I want to print gr1 if the frequency stays in the range of fa1(50% below the product of the pot value set as 50 by the constant k1) and fb1(10% above the product of the pot value set as 50 by the constant k1). Likewise, the intervals continue until fa2 and fb2........... fa6 and fb6. each fa and fb represents 10% lower and 10% upper bounds. I'm trying to compare the numbers between these limits with the frequency and find out which group they match.
Let me give an example;
As you can see in the code, the frequency constant value is 100hz in this example code. Likewise, the pot value is: 50.
According to the fixed k1, k2, k3, k4, k5, k6 values (fa1, fb1), (fa2, fb2).......(fa6, fb6), we will prepare 6 groups in which we will search for our sample frequency.
For this, let's first find the f values;

f1 = k1 * PotV; f2 = k2 * PotV; f3 = k3 * PotV;
f4 = k4 * PotV; f5 = k5 * PotV; f6 = k6 * PotV;

later;
Let's find the fa and fb values. As I explained above, these values are the limits 10% below and 10% above the f values.

   fa1 = f1 * 0.9; fb1 = f1 * 1.1;
   fa2 = f2 * 0.9; fb2 = f2 * 1.1;
   fa3 = f3 * 0.9; fb3 = f3 * 1.1;
   fa4 = f4 * 0.9; fb4 = f4 * 1.1;
   fa5 = f5 * 0.9; fb5 = f5 * 1.1;
   fa6 = f6 * 0.9; fb6 = f6 * 1.1;

Now let's search our sample frequency (100hz) in each group;
(I'm sorry but I have to re-explain all the code here.)

if (fa1<=frequency<fb1) //that is, if the sample frequency is between these limit values, execute the commands below.

if (fa1<=frequency<fb1)
     {                            
     lcd.setCursor(0,0);      
     lcd.setCursor(0,1);     
     lcd.print("Gr:1" );
     }
      
     else if (fa2<=frequency<fb2)
  {
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:1" );
      }
      
      else if (fa2<=frequency<fb2)
      {
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:2" );
      }
     
      else if (fa3<=frequency<fb3)
      {
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:3" );
      }
      
      else if (fa4<=frequency<fb4)
      {
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:4" );
      }

      else if (fa5<=frequency<fb5)
      {
                                 
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:5" );
      }
      
       else if (fa6<=frequency<fb6)
      {
                                 
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:6" );
      }
      
      else
      {
      lcd.setCursor(0,0);
      lcd.setCursor(0,1);
      lcd.print("Gr:7" );
      }
      }

I don't know if I explained enough. after all, gr2 needs to be written to the screen according to the if condition. However, no matter what frequency I write, the result is always displayed as gr1. I don't understand where is the mistake.

I couldn't find a freighter to carry the very long code page here. :sweat_smile:

I was asking for an explanation without code.
Because you have a

misconception how if-conditions work

you can't code this way fa1 <= frequency < fb1

with using two partly conditions in that way
This is the problem

1 Like

by now i hope you realize from the comments, that that is not a meaningful statement

i believe what you intend is

1 Like

You said it right. Only the frequency value is variable. I wrote it here as an example. fixed ones; potv are k1,........k6 values. Therefore, depending on the frequency variables, f and fa1, fb1......fa6, fb6 values are also constant. Arrays are actually created with these values; (fa1, fb1).....(fa6, fb6). By searching the frequency in these series(gr1,....gr6), I want to determine which series it is in. just that much. In order to search within each group;
if (fa1<=frequency<fb1) // gr1
..........................
if (fa6<=frequency<fb6)// gr6
I put the condition.
Is this wrong logic?

No,

a more common structure is

    if (F1 > freq)
        ...
    else if (F2 > freq)
        ...
    else if (F3 > freq)
        ...
    else
        ...

if it's not F1 > and is F2 > then it is between F1 and F2

1 Like

it is a boolean syntax that is interpreted different than you expect it

you have to code two conditions each with only one comparing
and combine them with the logical AND-operator

This is what gcjr has posted

1 Like
if (fa1 < frequency && frequency < fb1)

if

  • part one: frequency is greater than fa1 (fa1 < frequency)
    and //........... (coded with a double-&& not a single-&=
  • part two: frequency is smaller or equal to fb1 (frequency<= fb1)

then ....

1 Like

Thank you for answere,
if (fa1 <= frequency < fb1)
Let me try to explain what I understand from the if operator as follows;
if the frequency matches one of the numbers from fa1 to fb1.