Keyboard Instrument - Project 7 rom Project Book

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]'

The error message is quite clear: error: conflicting declaration 'int buttons [0]'

Do you understand what that means? It's not a cryptic error. There are 2 definitions of the same thing and they conflict with each other.

Did you change the code from the tutorial? Check any changes carefully.

int buttons[0] = 2;
// set the first value of the array to be 2

You cannot set the first value of the array like that. And you have made a conflicting declaration.

Try;

int buttons[6] = {2, 0, 0, 0, 0, 0};
//set up an array with 6 integers
//and set the first value of the array to be 2

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.

Thanks, I'll try that one.

Thanks, PaulRB, that did the trick. Seems there is an issue with the documentation.

Post a link to that.

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.

Thanks for bringing this to our attention @weeburan. I have submitted a formal report to the team at Arduino responsible for the Project Book.

I apologize for any confusion caused by the error.

The buttons array is not used by the program in any way, so the fix will be to simply remove those lines. Here is the corrected code:

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);
  }
}

There seems to different versions of the Arduino Cookbook.

This is from a Arduino Cookbook I downloaded.

@weeburan is referring to the "Arduino Projects Book", which is included in the Arduino Starter Kit:

šŸ“·

starterkit_02.unbox_1000x750.jpg by Arduino - CC BY-SA 4.0

Thanks , I miss read "Project" Book as "Cook" Book.

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.