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;
}
}
}