Shop OBEX P1 Docs P2 Docs Learn Events
How to select highest array value? — Parallax Forums

How to select highest array value?

lardomlardom Posts: 1,659
edited 2013-02-10 09:28 in Propeller 1
In Spin I was wondering how to have a wheeled robot rotate 180° taking distance measurements every 30°. The variables would be named "270°, 300°..etc. I thought I could use a case statement but it does not seem to apply. Is there a way to select a variable based on one that is 'closest' to a target number? I'm only interested in the highest or the lowest value not the average. (If an array would add to the overhead I wouldn't use one.)

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2013-02-08 09:17
    Maybe more of an example of your program or values we'll be working with will help. If you're just checking to see if 270 or 300 is greater that's easy.

    But if you have an array of many values, you could use one of the sorting algorithms here: http://obex.parallax.com/objects/715/ . Then the first index would be the lowest, and the last would be the highest.
    Or you make a "lowest" and "highest" variable and check to see if the newest input it lower or higher (respectively) than the existing value and store the new value.
  • lardomlardom Posts: 1,659
    edited 2013-02-08 09:23
    Or you make a "lowest" and "highest" variable and check to see if the newest input it lower or higher (respectively) than the existing value and store the new value.
    I think that'll work. Thanks.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-02-09 09:13
    With a small array it should be easy to index through to find the smallest or largest -- here's one example that lets you pass the address of the array (so you can use it on more than one) and the size. Copy and flip the logic for high value.
    pub lowest(p_array, size) | lowval, check
    
      lowval := long[p_array]                                       ' get 1st element
    
      repeat size-1
        p_array += 4                                                ' get next element
        check := long[p_array]
        if (check < lowval)                                         ' test
          lowval := check                                           '  update if new low
    
      return lowval
    
  • lardomlardom Posts: 1,659
    edited 2013-02-09 09:54
    Thanks JonnyMac. I'm getting the expected results on the PST but there was a lot of 'copy and paste' in the methods. Readability makes things so much easier.
    There are a lot of pieces to this puzzle. The idea was to have the robot rotate 360° taking a distance measurement every 30° then using case to turn the robot toward the greatest distance. An interesting puzzle that I'm still trying to solve.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-02-09 16:48
    Not knowing what you wanted I wrote that code to return the lowest value; below is a modification that returns the index of the lowest value -- this should be easy to convert to a movement direction.
    pub low_index(p_array, size) | idx, lowidx, check
    
      lowidx := 0
      check := long[p_array][0]
    
      repeat idx from 0 to size-1
        if (long[p_array][idx] < check)
          lowidx := idx
          check := long[p_array][idx]
    
      return lowidx
    
  • lardomlardom Posts: 1,659
    edited 2013-02-10 09:28
    @JonnyMac, My code, with all the "if" and "case" statements, needed almost two pages for each stepper since they're run in different cogs. Your code is eight lines long. Pretty good!
    "return MyVar" will replace what I normally write; "result := MyVar".
    p_array += 4                                                ' get next element
       check := long[p_array]
    
    What does this expression mean? "long[p_array]" Is it the long aligned data or the memory address? I ask because you use "+= 4" which points to the next long address.
Sign In or Register to comment.