Compilation error: conflicting declaration 'int buttons [0]'

I do the programm from the book in the starter kit and i have an error.

this is the error:Compilation error:

conflicting declaration 'int buttons [0]'

and here is my code:

Int buttons[6];
int buttons[0] = 2;
int notes[] = {262,294,330,349};
void setup() {
  // put your setup code here, to run once:
  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);
   }
   }

Welcome to the forum

int buttons[0] = 2;

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

3 Likes

This makes no sense:

int buttons[0] = 2;

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.

2 Likes

I made it other but there is still an error

It is bad manners to alter a post, particularly code in a post, after it has been commented on. You should have posted the revised code in a new reply

Int buttons[6];

What is the data type of the buttons array ?

HINT : there is no standard data type named Int and the error message will have told you that and also suggested a fix

I also note that you are still not using the buttons array in the code

NO:

YES:

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.

2 Likes

back in 2014....

(post deleted by author)

Why can't I see a flagged post aimed at me?

OP never asked it to be explained. What's up?

int buttons[6];
// set up an array with 6 integers
int buttons[0] = 2;

The buttons array has been declared twice

Have you read post #6 above ?

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

int buttons[0] = 2;
Rem out

So yea I have moved on and now you know

Would you PM me your offensive stuff? I missed out on this one, too!

your example it dont work either

Yes, it do. Show me where your changes were made. Let's fix it.

The forum's new AI spambot is a bit too aggressive.

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...

int notes[] = {262,294,330,349};
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
}

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

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.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.