struct help

I am trying the take readings from four analog inputs and order them by size. I thought of using a struture as that way i can assign the value read to one variable and also a unique value to another variable corrisponding the the input.

That way when i sort the values into order of size i wont loose which value came from where.

In the grand scheme of things i am trying to make a robot that will detect sound and move towards it, the alalgo inputs are connected to mics that are positioned 90 degrees out from each other around the robot, i want to use the highest reading to tell the robot which direction to go however i also want the robot to detect if the sound is half way between to mics, i.e. there two values are nearly the same.

so basicly the code would go;

  • copy values to struct,

  • sort struct the decending order,

  • look at block zero and block 1 and if there within say 100 of eachother and the mics are next to eachother then sound is between them,

  • else sound is infront of highest.

My problem is i cant get a structure defined and also i dont know how to sort efficently, please can someone help?

Mark

I'm not sure you need a "struct" when a simple array ought to suffice.

Efficient sort of four values isn't really an issue. Even a bubble sort would do the job.

Ok, but how would the arduino know what value is from which mic after it has been sorted as it would know longer occupy the same slot in the array as it was placed in before?

I threw together a bit of code that should work for figuring out which one of the sensors is receiving the higher value. Not sure if the if() statements will work how i wrote them. They compiled just fine but i don’t have the hardware to test it. Of course the amount you set so the +/- spread between sensors may need to be adjusted.

I think this about covers it for you. There may be a better way to do it. This is just what i could come up with in about 15 min.

int sensorPin0 = 0;
int sensorPin1 = 1;
int sensorPin2 = 2;
int sensorPin3 = 3;
int sensorVal0 = 0;
int sensorVal1 = 1;
int sensorVal2 = 2;
int sensorVal3 = 3;

void setup()
{
}
    

void loop()
{
  sensorVal0 = analogRead(sensorPin0);
  sensorVal1 = analogRead(sensorPin1);
  sensorVal2 = analogRead(sensorPin2);
  sensorVal3 = analogRead(sensorPin3);
  
  if (sensorVal0 > sensorVal1 && sensorVal2 && sensorVal3)
  {
    //perform action
  }
  if (sensorVal1 > sensorVal0 && sensorVal2 && sensorVal3)
  {
    //perform action
  }
  if (sensorVal2 > sensorVal0 && sensorVal1 && sensorVal3)
  {
    //perform action
  }
  if (sensorVal3 > sensorVal0 && sensorVal1 && sensorVal2)
  {
    //perform action
  }
  if ((sensorVal0 + 100) > sensorVal1 && (sensorVal0 - 100) < sensorVal1)
  {
    //perform action for between sensors
  }
  if ((sensorVal0 + 100) > sensorVal3 && (sensorVal0 - 100) < sensorVal3)
  {
    //perform action for between sensors
  }
  if ((sensorVal2 + 100) > sensorVal1 && (sensorVal2 - 100) < sensorVal1)
  {
    //perform action for between sensors
  }
  if ((sensorVal2 + 100) > sensorVal3 && (sensorVal2 - 100) < sensorVal3)
  {
    //perform action for between sensors
  }
}

One way to approach this would be to calculate the angle the sound is coming from. If you have four amplitude/level measurements - you could pass these to a function that performs its "magic" and returns an angle relative to the zero direction of the robot.

int GetSoundDirection(int head_amplitude, int right_amplitude, int rear_amplitude, int left_amplitude) {
  // Calculate and return angle
}

If you like this approach you may want to have a go at calculating the angle or otherwise come back for hints.