It looks like you are declaring an array with zero elements then compounding the problem by trying to assign a value of 2 to a non existent array element
Even more ironically you never use the buttons array in the sketch
The square brackets say that buttons is an array of int. An array is like any other variable, except that instead of holding it only one value, an array can hold many values. When you declare an array variable, you tell it how many values it can hold. So holding zero values makes no sense.
int buttons[6];
void setup() {
Serial.begin(9600);
buttons[0] = 2; // YES
Serial.println(buttons[0]); // show first element of buttons[]
}
void loop() {
// empty
}
Show the internet link (URL) where you found your original code.
Who's getting "angry and prissy" in this thread? All I see is helpful suggestions.
And regarding the book where the code came from: it's possible the author didn't actually test the code that they put in the book, or they changed it last minute without testing the changes. It happens. The code as written in the book is not and never was correct. Take the advice and suggestions from this thread (or from the thread mentioned in #7) and move on.
Looks like this person is having the same issue #7
also if the book was written to teach and show methods of sketch programming then why put it in a book so every one that is trying to learn cant fix it because they don't know what it is your talking about
Now a fun fact for you...
this code works absolutely fine on the Arduino R4 board with the line
To all those that are having difficulty on the lesson #7 Keyboard Instrument this is the correct code and you can change it to what ever you like to understand the values, with an array and integers.
int buttons[6];
// set up an array with 6 integers
int buttons[6] = {1};
// give the first element of the array the value 2
leave the top portion off no need to include the above argument. Here is the code that should be in the book and or corrected have fun and play away your notes....you should end up with 8 notes from 4 keys here is the code...
Because the author of that book is human, and humans make mistakes (and I imagine a lot of Arduino books are self-published and without a big editing budget).
On the other hand, oftentimes an author will publish errata which show errors and corrections in a book and/or put out a new edition of the book with errors corrected.