I wasn't to make a keyboard that has different octaves depending on a value of a potentiometer. To make it neater,I think it will be a good idea to put the different octaves in different arrays with the seven values in them for the buttons. I would then assign a button a note from each array depending on the value of the capacitor. Is this possible and would antibody be able to give an example of calling an individual value from each array?
keyPressed(note,octave);
I wasn't to make a keyboard that has different octaves depending on a value of a potentiometer
You would be better off using a multi position switch if possible because it is going to be difficult to set the pot to the precise position for each octave.
To derive which of 7 octaves to use from the value returned from the pot have a look at the map() function.
It will allow you to turn the 0 to 1023 output from the pot into a 0 to 6 value.
Don't use 7 arrays. Instead use a 2 dimensional array with a different octave on each row and the note values in the columns.
my question really is how do I call a certain value from within an array for a button? Also how do i use a two-dimensional array?
tone=keyPressed(note,octave);
in setup,
define an array
int array[numberOfnotes],[numberOfoctaves];
load the values into the array
loop
With the values in a 2 dimensional array you use the octave number as the index to the array row and the note number as the index to the array column.
There are endless online tutorials on 2 dimensional arrays in C
my question really is how do I call a certain value from within an array
You CALL functions. You ACCESS values from arrays. You sound like a doofus when you talking about calling values.
PaulS:
You CALL functions. You ACCESS values from arrays. You sound like a doofus when you talking about calling values.
I find this quite impertinent as I haven't been writing my own code very long. As a matter of a fact, this is my first full project where I want to write all the code as well as make the circuit. I see that you have written thousands of posts on the forum but I haven't and most have my posts have been questions. I will try and use a 2d array and report back with how it goes.
#include "pitches.h"
int Pot_pin = A0;
int Speaker_pin = 13;
int Notes [7][6] {
{Note_C1, Note_D1, Note_E1, Note_F1, Note_G1, Note_A1, Note_B1},
{Note_C2, Note_D2, Note_E2, Note_F2, Note_G2, Note_A2, Note_B2},
{Note_C3, Note_D3, Note_E3, Note_F3, Note_G3, Note_A3, Note_B3},
{Note_C4, Note_D4, Note_E4, Note_F4, Note_G4, Note_A4, Note_B4},
{Note_C5, Note_D5, Note_E5, Note_F5, Note_G5, Note_A5, Note_B5},
{Note_C6, Note_D6, Note_E6, Note_F6, Note_G6, Note_A6, Note_B6},
{Note_C7, Note_D7, Note_E7, Note_F7, Note_G7, Note_A7, Note_B7},
{Note_C8, Note_D8, Note_E8, Note_F8, Note_G8, Note_A8, Note_B8}
}
;
}
void setup() {
//Setup Pot as Input
pinMode (Pot_pin, INPUT);
//Setup Speaker as Output
pinMode (Speaker_pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Pot_config = map(0, 1023, 0, 6)
}
I haven't finished before anyone says anything but I am getting an error for the Note_C1 not being declared in the scope... I have added the pitches.h file into the same folder which means it should be read and I have made sure it has been defined under the same name as I copied the pitches.h page off of the https://www.arduino.cc/en/Tutorial/toneMelody page. Am I just getting this error because I haven't finished the code yet or is it wrong anyway?
From the page you linked to
#define NOTE_C1 33
Is that exactly what you have got in your pitches.h file ?
If so, then compare it with what you have got in your code.
I have compared it more than once and even retyped it and still don't understand why I get this error would anybody else be able to test the code with the 'pitches.h' file that I linked?
NOTE_C1 != Note_C1
Have you fixed that?
I have only just realised what you have been trying to tell me... It's all in caps! I didn't even realise that!
At last !
#include "pitches.h"
int button_1 = 2;
int button_2 = 3;
int button_3 = 4;
int button_4 = 5;
int button_5 = 6;
int button_6 = 7;
int button_7 = 8;
int Pot_pin = A0;
int Speaker_pin = 13;
int Notes [][7] {
{NOTE_C1, NOTE_D1, NOTE_E1, NOTE_F1, NOTE_G1, NOTE_A1, NOTE_B1},
{NOTE_C2, NOTE_D2, NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2},
{NOTE_C3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3},
{NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4},
{NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5},
{NOTE_C6, NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6},
{NOTE_C7, NOTE_D7, NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7},
}
;
}
void setup() {
//Setup Pot as Input
pinMode (Pot_pin, INPUT);
//Setup Speaker as Output
pinMode (Speaker_pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Pot_config = map(0, 1023, 0, 6)
Pot_value = analogRead(Pot_pin)
if (Pot_value = 0) {
Button1_high = digitalRead(button_1)
Button2_high = digitalRead(button_2)
Button3_high = digitalRead(button_3)
Button4_high = digitalRead(button_4)
Button5_high = digitalRead(button_5)
Button6_high = digitalRead(button_6)
Button7_high = digitalRead(button_7)
if (Button1_high = 1) {
//Play the tone
}
if (Button2_high = 1) {
//Play the tone
}
if (Button3_high = 1) {
//Play the tone
}
if (Button4_high = 1) {
//Play the tone
}
}
}
Here is my changed code. I am just getting one error now which is an expected declaration before } symbol.I think that I have everything in the right place around the array but could someone double-check?
int Notes [][7] {
Missing a little something.
}
;
}
Probably a little too much of something.
if (Button3_high = 1)
Not an error, per se, but I doubt you want an assignment there.(you've got a lot of those)