I'm trying to create a 2D array of notes that are split into 3 scales. There are 3 notes in each scale. There are 3 buttons to play each note and I want to use an available potentiometer to select each of the scales to be played.
Despite my searching I cannot understand how to achieve this correctly. So far this is what I have.
Using an Arduino Uno:
// 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 numScales = 3;
const int numNotesPerScale = 3;
const int notes[numScales][numNotesPerScale] = {
{C, D, E},
{F, G, A},
{B, C1, D1}
};
const int numberOfButtons = 3;
int buttonPin[numberOfButtons] = {2, 7, 4};
//int notes[numberOfButtons] = {C, D, E};
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() {
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 numScales = map(analogRead(A2), 0, 1023, 0, 2);
//int numScales = analogRead(A2 / 341);
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
}
}