banjorobbie
New Member
Hello, this is my first post here so please forgive me for any errors posting this.
I have an Uno programmed to play a note on each of 3 buttons. Originally this worked fine as it was using a set scale of 3 notes but I wanted to use a pot to select one of 3 scales for each of those buttons to play. I've set about using a 2d array to hold all the notes and I have a pot that's mapped to output a value of 0-2 to reference each of the 3 scales but I'm unable to understand how to continue from this point in my code to access the array in order to change scales.
Any help at all would be welcome
I have an Uno programmed to play a note on each of 3 buttons. Originally this worked fine as it was using a set scale of 3 notes but I wanted to use a pot to select one of 3 scales for each of those buttons to play. I've set about using a 2d array to hold all the notes and I have a pot that's mapped to output a value of 0-2 to reference each of the 3 scales but I'm unable to understand how to continue from this point in my code to access the array in order to change scales.
Any help at all would be welcome
Code:
// 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
}
}