Project Question

Salutations! Working on a piezo keyboard project from a kit, with 4 push buttons. So here is my question, I started off with this code copied from a book.

int buttons[6];
int buttons[0] = 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);
  }

}/code]



and here is the error message:

keyboard:2: error: conflicting declaration 'int buttons [0]'
 int buttons[0] = 2;
              ^
/Users/yogigirl/Documents/Arduino/keyboard/keyboard.ino:1:5: note: previous declaration as 'int buttons [6]'
 int buttons[6];
     ^
exit status 1
conflicting declaration 'int buttons [0]'

Now, I modified the code, to this:
[code]int buttons[4];
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);
  }

}/code]


I modified the number of arrays from 6 to 4, and took out the additional line of code before that. So, no more error message, however, only 3 of the 4 switches work. I do not believe I wired it wrong(although it is possible I did), but would this be due to the code I modified?

Thank you in advance for the help and insight!

When you int buttons[6]; you are declaring an array of int, length 6. Immediately then you int button[0] = 2;, which is an attempt to declare another array named buttons of length 0 and then attempt to initialize it with a value of zero.

I suspect what you meant in your second line is to initialize buttons[0] to be 2, which you can't do outside a function. What you could do with you first statement is int buttons[6] = {2}; which will declare the array and initialize. As there is only one value in the initialization, by default it will be assigned to buttons[0], the remaining array members will all be zero.

The name of the array suggests that it will contains buttons. You can NOT store hardware in an array, so the name could use some improvement.

If the array is supposed to hold pin numbers, which Arduino are you using that has more than 256 pins? Do you REALLY need an int array to hold pin numbers?

Do you REALLY plan to change pin numbers while the program is running? If not, the array should be const.

tried both, the first one int button[0] = 2; and it worked, and put it as a const, that also worked. thank you for the advice!

So The while recommendation was to useconst byte buttonPin[6] = {2];

But in practice it does not matter as you don’t seem to use that array at all...your code only depends on what is connected on Analog A0.... please describe your wiring

You should re-edit your first post to ensure the code tags properly start and end around your code. Here it’s all garbled and thus hard to read

the first one int button[0] = 2; and it worked

Complete rubbish. You declared an array that can hold zero elements and then stored one element in it. You just haven't been burnt yet. But, keep this stupidity up and you WILL.