// musical notes
int C = 1046;
int D = 1175;
int E = 1319;
int F = 1397;
int G = 1598;
int A = 1760;
int B = 1976;
int C1 = 2093;
int D1 = 2349;
const int columns = 3;
const int scales = 3;
int potVal = 0;
const int notes[scales][columns] = {
{C, D, E},
{F, G, A},
{B, C1, D1}
};
const int numberOfButtons = 3;
int buttonPin[numberOfButtons] = {2, 7, 4};
int ledPin[numberOfButtons] = {11, 10, 9};
int buttonState = 0; // variable for reading the pushbutton status
int speaker = 3; // name of the speaker key
void setup() {
Serial.begin(9600);
for (int i = 0; i < numberOfButtons; i++) {
pinMode(buttonPin[i], INPUT);
pinMode(ledPin[i], OUTPUT);
}
pinMode(speaker, OUTPUT); // set speaker to be an output
}
void loop() {
int potVal = map(analogRead(A2), 0, 1024, 0, 3);
Serial.println(potVal);
for (int i = 0; i < numberOfButtons; i++) {
checkButton(buttonPin[i], notes[i], ledPin[i]);
}
}
void checkButton(int buttonPin, int note, int ledPin)
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
tone(speaker, note); // play the note
delay(100); // wait for 1/10th of a second
} else {
digitalWrite(ledPin, LOW);
noTone(speaker); // stop playing the note
}
}