简体   繁体   中英

How to find elements greater than or less than in a list on random number

I have a list of elements (random numeric numbers) that are constantly varying at the output(100 indices), and I want to do with it is if any number generated at output(even if one) is equal or above number '27' turn led on, but if numbers are all less than '27' turn led off. The logic I created for it is not working, even though i am getting output of numbers above value 27 (like 25,26.5,27.8, 23, 29.01, 30.87,23.....so on) still LED is not accurately turning on. Here is my part of code from Arduino:

 for (int x = 0 ; x < 101 ; x++)  
  {
    Serial.print("Pixel ");
    Serial.print(x);
    Serial.print(": ");
    Serial.print(myList[x], 2); #myList has got the 100 random numeric values
    Serial.print("C");
    Serial.println();
    if(myList[x<100]>=27) 
       digitalWrite(ledPin, HIGH);  
    if(myList[x<10]<27) 
       digitalWrite(ledPin, LOW);     

  }

One possible way to solve your problem is to find the largest value in the array. This can be done using a single pass (loop) over the array.

If the largest value is less than 27 , the all values are less than 27 .

If the largest value is equal or larger than 27 , then that condition is satisfied.

You can "optimize" this by breaking out of the loop when you find a value equal over 27 , and you don't need to find a larger value.

It is a simple search algorithm, it may help you:

void loop{
    ...
    ...
    ...
    boolean found = false;
    int x = 0;
    while(x<101 && !found){
        if(myList[x]>=27){
            found = true;
        }
         x++;
    }
    if(found) digitalWrite(ledPin, HIGH);
    else digitalWrite(ledPin, LOW);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM