Search code examples
c++carduinopseudocode

Get seccond Largest Number in a key value pair array with specific distance


  • Im working on a project that maps points of height on the Neck.

  • Using an Arduino and a Hall Sensor, I map points of resistance in diferent locations.

  • im mapping in a HashMap my points (points[ stepper_motor_location][resistance])
  • I need to find the middle point (Cricothyriod ligament )
  • I know how to get the upper point ( thyroid cartilage ) by getting the max resistance from all locations.
  • but i don't know how to get the bottom point ( Cricoid Cartilage ) which is the seccond highest peak but with a considerable distance from the first highest peak.

So, the question is :

How do you get the 2nd Highest resistance in points[location][resistance] with a difference of at least 2-10 in location from the Highest resistance?

C, C++, Arduino, or pseudo-code, I sincerely don't mind.


Here's my code:

/* ###### VARIABLE ######## */
 /*  */
found bool = false; /* while our point is not found, FOUND IS FALSE */
resistance int []; /* resistance vector */
location int []; /* Motor location vector */
current_location int = 0; /* 0 which means the top part of the motor container */
current_resistance int; /* the resistance which the Hall Sensor registers at that moment */
points HashMap; /* HashMap with location as key and resistance at its location as value */
min_distance int = 50; /* value for the minimum distante between the highest point and the second highest point (Variable to be modified as we test more) */

/* ######## FUNCTIONS ######### */
 /*  */
move_check()
{
    location.push(current_location);
    current_resistance = GET_CURRENT_RESISTANCE;
    resistance.push(current_resistance);
    MOVE_MOTOR++;
    location++;
}

map_points()
{
    for(;location <= MAX_DISTANCE;) /* WE SET THE MAX_DISTANCE TO WHAT WE MEASURED BEFORE AS MAX DISTANCE */
    {
        move_check();
        RED_LED = 1;
        RED_LED = 0; /* TO SEE THAT IS MAPPING POINTS */
        }
    }
    CreateHashMap (points, location, resistance, MAX_DISTANCE); /* (VARIABLE_NAME , int, int, NUMBER_OF_ITEMS) */
}

get_top() /* gets the thryoid Cartilage location */
{
    maximum int = 0;
    max_location int = 0;
    for(int i = 0; i<= location.length(); i++)
    {
        if(maximum > max(points[i], maximum))
        {
            maximum = max(points[i], maximum);
        }
        else
        {
            maximum = max(points[i], maximum);
            max_location = i;
        }
    }
    return max_location;
}

get_bottom() /* gets the cricoid cartilage location*/
{
    bottom_point int;
    ??????????????????????????????
    return bottom_point;
}

goto_middle_point()
{
    RED_LED = 1;
    while(location != floor((get_top() + get_bottom()) / 2))
    {
        location--;
    }
    RED_LED = 0;
    BEEP_SPEAKER_LONG;
}

wait_button_press()
{
    resistance_now int = GET_CURRENT_RESISTANCE;
    while(resistance_now != GET_CURRENT_RESISTANCE)
    {
        RED_LED = 1;
        RED_LED = 0;
    }
    SLEEP(1000);
    found = true;
}

done(bool found)
{
    if(found)
    {
        while(location)/*  if location is 0 == false (top location) */
        {
            location--;
            MOVE_MOTOR--;
        }
    }
    BEEP_SPEAKER;
    BEEP_SPEAKER; /* beep speaker twice for done */
    RED_LED = 0; /* stops working */
    GREEN_LED = 1; /* signal for ready */
}

int main()
{
    map_points();
    goto_middle_point();
    wait_button_press();
    done();
    return 0;
}

Solution

  • I don't know which implementation of Arduino HashMap you are using, but if it's this one, this is the best I can come up with given the API. It is certainly not optimal, and a better data structure would be faster.

    Assuming I understand the problem correctly, I would just do a linear search. You know how to get the highest point. You know the location of the second highest point must be below the highest. (I'm adding here, you might need to subtract if your coordinate system is different). Assume the minimum difference in positions has to be min_dist (some value 2-10).

    HashMap<int, int, N> points = ...;
    int key_low = upper_position + min_dist;
    int peak_location = -1;
    for(int i=0; i < points.size(); ++i)
    { 
        int location = points.keyAt(i);
        if (location < key_low) continue;
        int resistance = points[location];
        if (peak_location < 0 || resistance > points[peak_location])
        {
            peak_location = location; 
        }
    }