Serial Monitor Wont display my code

Hi all,

I am unsure on why my code wont output. Can anyone help?

It is has been tested in the QT environment, and i wanted to change it into arduino for other reasons.

int linearSearch(float array[], int size, float searchValue)
{
    for(int i = 0; i < size; i++)
    {
        if (searchValue == array[i])
        {
            return i;
        }
    }
    return -1;
}



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

}

void loop() 
{
    float a[] = {44.68,44.65,44.62,44.6,44.57,44.54,44.52,44.49,44.46,44.44,44.41,44,39,44.36,44.33,44.3,44.28,44.25,44.22,
                     44.2,44.17,44.14,44.12,44.09,44.06,44.03,44.01,43.98,43.95,43.93,43.9,43.87,43.84,43.82,43.79,43.76,43.73,
                     43.71,43.68,43.65,43.62,43.6,43.57,43.54,43.51,43.48,43.46,43.43,43.4,43.37,43.34,43.32,43.29,43.26,43.23,
                     43.2,43.18,43.15,43.12,43.09,43.06,43.04,43.01,42.98,42.95,42.92,42.89,42.87,42.84,42.81,42.78,42.75,42.72,
                     42.7,42.67,42.64,42.61,42.58,42.55,42.53,42.5,42.47,42.44,42.41,42.38,42.35,42.33,42.3,42.27,42.24,42.21,
                     42.18,42.15,42.13,42.1,42.07,42.04,42.01,41.98,41.95,41.92,41.9,41.87,41.84,41.81,41.78,41.75,41.72,41.7,
                     41.67,41.64,41.61,41.58,41.55,41.52,41.49,41.47,41.44,41.41,41.38,41.35,41.32,41.29,41.26,41.24,41.21,41.18,
                     41.15,41.12,41.09,41.06,41.03,41.01,40.98, 40.95,40.92,40.89,40.86,40.83,40.81,40.78,40.75,40.72,40.69,40.66,
                     40.63,40.6,40.58,40.55,40.52,40.49,40.46,40.43,40.4,40.38,40.35,40.32,40.29,40.26,40.23,40.2,40.18,40.15,40.12,40.09,
                     40.06,40.03,40.01,39.98,39.95,39.92,39.89,39.86,39.84,39.81,39.78,39.75,39.72,39.69,39.67,39.64,39.61,39.58,39.55,39.52,
                     39.47,39.44,39.41,39.38,39.35,39.33,39.3,39.27,39.24,39.21,39.19,39.16,39.13,39.1,39.07,39.05,39.02,38.99,38.96,38.93,
                     38.91,38.88,38.85,38.82,38.79,38.77,38.74,38.71,38.68,38.66,38.63,38.6,38.57,38.54,38.52,38.49,38.46,38.43,38.41,38.38,
                     38.35,38.32,38.3,38.27,38.24,38.21,38.19,38.16,38.13,38.1,38.08,38.05,38.02,37.99,37.97,37.94,37.91,37.88,37.86,37.83,37.8,37.77,
                     37.75,37.72,37.69,37.67,37.64,37.61,37.58,37.56,37.53,37.5,37.47,37.45,37.42,37.39,37.37,37.34,37.31,37.29,37.26,37.23,37.2,37.18,
                     37.15,37.12,37.1,37.07,37.04,37.02,36.99,36.96,36.94,36.91,36.88,36.85,36.83,36.8,36.77,36.75,36.72,36.69,36.67,36.64,36.61,36.59,
                     36.56,36.53,36.51,36.48,36.45,36.43,36.4,36.37,36.35,36.32};

        float userValue;
    
        Serial.print("Enter a Frequency: ");
        while(Serial.available() ==0)
        {  }
        userValue = Serial.parseFloat();
        Serial.println(userValue);

        int result = linearSearch(a, 301, userValue);
        
       if (result >= 0)
        {
          Serial.print("Frequency: ");
          Serial.println(a[result]);
          Serial.print("mmH20 Level: ");
          Serial.println(result);
        }
        else
        {
          Serial.print("Frequency ");
          Serial.print(userValue);
          Serial.println(" was not found.");
        }
        
        

}
 if (x=1)

I assume that you meant

 if (x==1)

I think you have a couple of issues beyond what UKHeliBob pointed out:

  1. That is a really large table for a local variable. You should make that table static or global because I'm sure it exceeds the stack space. When I ran it on an UNO it failed. When I made it static it worked.
  2. Because you are using == with float values you are unlikely to get a match. You need to check to see if the float values are within a range.

Change has been made wrong code sorry

How do i make the table static/global?

Change:

float a[] = {44.68,44.65,44.62,44.6,44.57,44.54,44.52,44.49,44.46,44.44,44.41,44,39,44.36,44.33,44.3,44.28,44.25,44.22,

to:

static float a[] = {44.68,44.65,44.62,44.6,44.57,44.54,44.52,44.49,44.46,44.44,44.41,44,39,44.36,44.33,44.3,44.28,44.25,44.22,

Please don't do that in future because it invalidates comments made about the original code. If you make changes to code please post the revised version in a new reply

Okay this now works. However, index 0 wont appear...

You mean when you enter 44.68?

Yes, it shows the else statement

Does your linearSearch() function look like this?

int linearSearch(float array[], int size, float searchValue)
{
    for(int i = 0; i < size; i++)
    {
        if (searchValue == array[i])
        {
            return i;
        }
    }
    return -1;
}

yes

I stated in reply #3 you cannot rely on == to compare float numbers.

Here is a decent article on why that is:

https://www.geeksforgeeks.org/problem-in-comparing-floating-point-numbers-and-how-to-compare-them-correctly/

Turns out all the numbers dont work except 44.65...

how would i implement that for my code?

The solution was in the article I referenced. You can start with this and adjust the precision to your liking:

int linearSearch(float array[], int size, float searchValue)
{
    for(int i = 0; i < size; i++)
    {
        if (abs(searchValue - array[i]) < 1e-5)
        {
            return i;
        }
    }
    return -1;
}

Okay thanks, This works fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.