I am trying to complete the Project 7, Keyboard Instrument, from the Arduino Starter Kit. I am getting a compilation error related to the first two lines of code and cannot work out why. My code and the error is copied below. Any help would be greatly appreciated.
```cpp
int buttons[6];
//set up an array with 6 integers
int buttons[0] = 2;
// set the first value of the array to be 2
int notes[] = { 262, 294, 330, 349 };
void setup() {
Serial.begin(9600);
}
void loop() {
int keyVal = analogRead(A0);
Serial.println(keyVal);
if (keyVal == 1023) {
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
tone(8, notes[2]);
} else if (keyVal >= 5 && keyVal <= 10) {
tone(8, notes[3]);
} else {
noTone(8);
}
}
Error received:
...Arduino\keyboard_mar24a\keyboard_mar24a.ino:4:14: error: conflicting declaration 'int buttons [0]'
int buttons[0] = {2};
^
...Arduino\keyboard_mar24a\keyboard_mar24a.ino:1:5: note: previous declaration as 'int buttons [6]'
int buttons[6];
^~~~~~~
exit status 1
Compilation error: conflicting declaration 'int buttons [0]'
Thanks for your reply. I understand the error message. However, the code is exactly as given in the Project Book. I had a quick look at the reference material and it seemed to suggest these lines of code are as they should be. However, it's obvious they are not. I just can't figure the correct syntax here.
Because this line begins int, which is a variable type, like float or char, it is not assigning a value to a variable, but declaring a new variable. But a variable with that name already exists.
If you removed the int, it still would not be allowed because an assignment like that isn't allowed outside of a function like setup() or loop(). If you moved it inside setup(), that would work.
Thank you to everyone in this thread for their help and encouragement. It is very good to know that there is a great community out there or beginners like me.