tone generating help

i am making an instrument, and all i need is simple sound output that sounds solid and clean. the way this sample is intended to work, is that you have three buttons a speaker and a potentiometer. the potentiometer range is split into thirds. when a button is pressed it emits a tone form the speaker. depending on which position the potentiometer is in, it should play different notes. so effectively you can play 9 different notes with 3 buttons.
my problem comes with my process. the problem is that each time the code loops, it plays the tone, therefore the tones played upon a button press go on and off at a insanely high rate, distorting the notes and forcing me to add a delay to recognize notes audibly. my question is how can i change my code so that when a button is pressed, the entire code does not loop. keep in mind that i need to be able to play multiple notes at once if possible, or at least be able to play a note then another note say with a 10 millisecond gap. the last requirement is that if i am holding a button, and i turn the potentiometer, the tone with change according to the zone i am in without removing my finger from the button.
i know this sounds complicated, especially for a first post, but if i can get this to work, it will really go somewhere.
thanks for the help in advance and hello to the community!

with out further adieu, the code, well commented too.

/*
RANGED INSTRUMENT SAMPLE 

this is is a test for the changing of notes an instrument plays with a potentiometer, so with three 
buttons and a potentiometer, nine notes can be played. this will be part of a larger project in
the near future.

PINS:

digital:
0 = button 1 to ground
1 = button 2 to ground
2 = button 3 to ground
8 = speaker output to ground

analog:
0 = potentiometer from 5v+ to pin 0 and lastly ground

*/

#include "notes.h" //full scale of notes to read from
boolean button[] = {0, 1, 2};  //create array for button pins
boolean speaker = 8;  //sets speaker pin
boolean buttonset = 0;  //variable for reading button push
int notes[] = { NOTE_FS3, NOTE_AS3, NOTE_CS4, NOTE_DS3, NOTE_FS3, NOTE_AS4, NOTE_CS3, NOTE_FS3, NOTE_GS4 };
              // notes used in this practice sketch above ^
int potpin = 0; // potontiometer set to analog pin 0
int potMin = 0; // minimum value for potontiometer range
int potMax = 1023; // maximum value for potentiometer range
void setup() {
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT); // sets buttons to outputs
  }
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH); // recognizes buttons as HIGH
  }
}

void loop()
{
  int potval = analogRead(potpin); // realtime value of the potentiometer
  int range = map(potval, potMin, potMax, 0, 2); // splits the potentiometer into thirds and 
                                                 //sets the thirds to a number: 0, 1 or 2 
  for(int x=0; x<3; x++) {
    buttonset = digitalRead(button[x]);
    switch(range) { // switch cases are the three positions of the potentiometer, so each third is a case
    case 0 : // first third of the potentiometers range
      if(buttonset == LOW && button[x] == 0) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_FS3, 25);          // then play note F sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 1) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_AS3, 25);          // then play note A sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_CS4, 25);          // then play note C sharp
        delay(20);
      }
      else { // if no button is pressed, dont make sound
        noTone(speaker);
      }
    case 1 : // second third of potentiometer range
      if(buttonset == LOW && button[x] == 0) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_DS3, 25);          // then play note D sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 1) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_FS3, 25);          // then play note F sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_AS3, 25);          // then play note A sharp
        delay(20);
      }
      else { // if no button is pressed, dont make sound
        noTone(speaker);
      }
    case 2 : // last third of potentiometer range
      if(buttonset == LOW && button[x] == 0) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_CS3, 25);          // then play note C sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 1) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_FS3, 25);          // then play note F sharp
        delay(20);
      }
      if(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_GS4, 25);          // then play note G sharp
        delay(20);
      }
      else { // if no button is pressed, dont make sound
        noTone(speaker);
      }
    break; 
    }
  }
}
int range = map(potval, potMin, potMax, 0, 2);

This will not do what the comment that follows says it will do. The map function uses integer arithmetic, causing lots of truncation. The only value of potval that will result in map returning 2 is 1023.

Instead you should define two more variables:

int topOfRange1 = potMax/3;
int topOfRange2 = topOfRange1 * 2;

Then, use in if/else if/else construct to assign a value to range:

if(potval < topOfRange1)
   range = 0;
else if(potval < topOfRange2)
   range = 1;
else
   range = 2;

Throughout the switch block, you check to see if the button is pressed in 9 places. It either is or it isn't. If it isn't, the whole switch block should be skipped. In other words, only execute the switch block if button[x] IS pressed.

Make this change, first.

In the switch statement, you are comparing the value in button[x] to see which note to play. The button array contains pin numbers. The pin number shouldn't define which note to play. If you move the switches to pins 8, 9, and 10, that would not change the note to be played, would it?

You should be checking the value of x, not button[x].

By the way, using digital pins 0 and 1 as input is not really a good idea. They prevent you from using serial communications, which is a good thing to use for debugging. It works, but they should be used only as a last resort. Keep the pins 0 and 1 available for as long as possible.

I'd recommend understanding what I'm suggesting here, and making, or not making, changes, and reposting the code.

Then, we can help you get the rest of the way to your goal.

thank you very much, i will check up on these things and post back when i correct these errors. i am very new to the arduino environment so any help is appreciated.

yes i know double post, any-who here is my updated code as of today. i added in the suggestions you mentioned, and the program works. i also added in some code to make an RGB LED light up according to the color of the button pressed (red white and light blue). so my next step with which i am open to suggestions is to be able to play multiple notes at the same time if at all possible then the last step is to be able to rotate the potentiometer, and when it changes zones while a button is held, the button will change to the appropriate note. so here is my code:

/*
RANGED INSTRUMENT SAMPLE 

this is is a test for the changing of notes an instrument plays with a potentiometer, so with three 
buttons and a potentiometer, nine notes can be played. this will be part of a larger project in
the near future.

PINS:

digital:
0 = button 1 to ground INPUT
1 = button 2 to ground INPUT
2 = button 3 to ground INPUT
8 = speaker to ground OUTPUT

analog:
0 = potentiometer from 5v+ to pin 0 and lastly ground INPUT
5 = red end of common cathode RGB LED to 5v+ OUTPUT
6 = blue end of common cathode RGB LED to 5v+ OUTPUT
9 = green end of common cathode RGB LED to 5v+ OUTPUT

*/

#include "notes.h" //full scale of notes to read from
boolean button[] = {2, 3, 4};  //create array for button pins
boolean speaker = 8;  //sets speaker pin
boolean buttonset = 0;  //variable for reading button push
int notes[] = { NOTE_FS3, NOTE_AS3, NOTE_CS4, NOTE_DS3, NOTE_FS3, NOTE_AS4, NOTE_CS3, NOTE_FS3, NOTE_GS4 };
              // notes used in this practice sketch above ^
int potpin = 0; // potontiometer set to analog pin 0
int potMax = 1023; // maximum value of potentiometer
int potThird = potMax / 3; // 1/3 of the range of the potentiometer
int pot2Thirds = potThird * 2; // 2/3 of the range of the potentiometer
int range = 0; // makes the variable range
int red = 5; // red led on analog out 5
int blue = 6; // blue led on analog out 6
int green = 9; // green led on analog out 9

void setup() {
  for(int x=0; x<3; x++) {
    pinMode(button[x], OUTPUT); // sets buttons to outputs
  }
  for(int x=0; x<3; x++) {
    digitalWrite(button[x], HIGH); // recognizes buttons as HIGH
  }
}

void loop()
{
  int potval = analogRead(potpin); // realtime value of the potentiometer
  if(potval < potThird) { // if pot is in the first third, do case 0
    range = 0;
  }
  else if(potval < pot2Thirds) { // if pot is in the center third, do case 1
    range = 1;
  }
  else { // if pot is in final third, do case 2
    range = 2;
  }
  for(int x=0; x<3; x++) {
    buttonset = digitalRead(button[x]); // reads if a button is pressed or not
    analogWrite(red, 255); // when no button is pressed, red light is off
    analogWrite(blue, 255); // when no button is pressed, blue light is off
    analogWrite(green, 255); // when no button is pressed, green light is off
    
    switch(range) { // switch cases are the three positions of the potentiometer, so each third is a case
    case 0 : // first third of the potentiometers range
      while(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_FS3);                  // then play note F sharp
        analogWrite(red, 0); // red light is on when first button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 2) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 3) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_AS3);                  // then play note A sharp
        analogWrite(red, 0); // red light max brightness when second button is pressed
        analogWrite(blue, 200); //  blue light is 4/5 brightness when second button is pressed 
        analogWrite(green, 200); // green light is 4/5 brightness when second button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 3) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 4) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_CS4);                  // then play note C sharp
        analogWrite(red, 200);  // red light 4/5 brightness when third button is pressed
        analogWrite(blue, 0);  //  blue light is max brightness when third button is pressed 
        analogWrite(green, 200); // green light is 4/5 brightness when third button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 4) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      noTone(speaker); // turns off sound if no button is pressed
    case 1 : // second third of potentiometer range
      while(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_DS3);                  // then play note D sharp
        analogWrite(red, 0); // red light is on when first button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 2) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 3) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_FS3);                  // then play note F sharp
        analogWrite(red, 0);  // red light max brightness when second button is pressed
        analogWrite(blue, 200);  //  blue light is 4/5 brightness when second button is pressed 
        analogWrite(green, 200);  // green light is 4/5 brightness when second button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 3) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 4) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_AS3);                  // then play note A sharp
        analogWrite(red, 200);  // red light 4/5 brightness when third button is pressed
        analogWrite(blue, 0);  //  blue light is max brightness when third button is pressed 
        analogWrite(green, 200);  // green light is 4/5 brightness when third button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 4) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      noTone(speaker); // turns off sound if no button is pressed
    case 2 : // last third of potentiometer range
      while(buttonset == LOW && button[x] == 2) { // if the button is pressed, and it is the first button,
        tone(speaker, NOTE_CS3);                  // then play note C sharp
        analogWrite(red, 0); // red light is on when first button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 2) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 3) { // if the button is pressed, and it is the second button,
        tone(speaker, NOTE_FS3);                  // then play note F sharp
        analogWrite(red, 0);  // red light max brightness when second button is pressed
        analogWrite(blue, 200);  //  blue light is 4/5 brightness when second button is pressed 
        analogWrite(green, 200); // green light is 4/5 brightness when second button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 3) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
      while(buttonset == LOW && button[x] == 4) { // if the button is pressed, and it is the third button,
        tone(speaker, NOTE_GS3);                  // then play note G sharp
        analogWrite(red, 200);  // red light 4/5 brightness when third button is pressed
        analogWrite(blue, 0);  //  blue light is max brightness when third button is pressed 
        analogWrite(green, 200); // green light is 4/5 brightness when third button is pressed
        buttonset = digitalRead(button[x]); // checks to see button state
        if(button[x] != 4) { // breaks from while loop if button is no longer pressed.
          break;
        }
      }
        noTone(speaker); // turns off sound if no button is pressed
    break; // ends case
    }
  }
}

yes i know double post

That's OK as long as you add new information, which you did.

so my next step...is to be able to play multiple notes at the same time

Whether or not that is possible depends on the way that the tone library is written. Try it and see.

the last step is to be able to rotate the potentiometer, and when it changes zones while a button is held, the button will change to the appropriate note.

This code already does that. Whether you observe this behavior, or not, depends on how the tone library works.

If the tone function does not return until the note completes, then you won't be able to play multiple tones at the same time, nor will you be able to change tones until the tone completes. If it returns right as soon as the note begins to play, then you should be able to play multiple notes together, and add a new note to the mix as the buttons are pressed or the potentiometer is turned.