Round value to a list of set numbers.

Hi, new to Arduino so sorry if this is stupid,

I received the Arduino starter kit, and I gave my self a challenge. I used a piezo speaker, a light sensor and some resistors to create a circuit which allows me to change the pitch of the buzzer by moving my hand closer and further away from the light sensor.

What I would like to do is make it so that the frequency would "snap on" to each semi tone (musically speaking) so that it will round the number given by the light sensor, to the nearest semitone given by a list of frequencies (hz), and then this note will be played out. This allows the speaker to hit musically correct notes.

Here is my code currently

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
int sensorPin = 0;
void setup() {                
Serial.begin(9600);
}

void loop() {
sensorValue = analogRead(sensorPin);
Serial.println (sensorValue);
tone(8, sensorValue);
delay(20);

}

Thanks in advance

Tom

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Why not tell us what your desired list of numbers actually is?
As I imagine you are aware, the actual frequencies in Hz of semitones on the international tempered scale are not integers.
http://hyperphysics.phy-astr.gsu.edu/hbase/music/et.html#c2

a possible strategy

//
//    FILE: nearestSearchInArray.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: 
//    DATE: 2013-12-27
//     URL:
//
// Released to the public domain
//

int ar[5] = { 
  100, 300, 500, 700, 900 };

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");
}

void loop() 
{
  int x = analogRead(A0);

  int y = search(x);
  Serial.print(x);
  Serial.print(" -> ");
  Serial.println(y);
}

int search(int x)
{
  int idx = 0;  // initially the first one is the best.
  int minDistance = abs(x - ar[0]);  // remember smallest distance so far
  for (int i=1; i<5; i++)  // go through the array, we can skip the first
  {
    int d = abs(x - ar[i]);  // determine distance
    if (d < minDistance)   // smaller than smallest?
    {
      minDistance = d;   // remember value 
      idx = i;  // + position
    }
  }
  return ar[idx]; // minDistance;  // or idx;  if you want the index 
}

a much faster strategy is to make an array of bounds and just check the value against the bounds
in the example above the bounds array is
int bounds[] = { 200, 400, 600, 800, 1024 }; // add the max value ...