Project 07 Keyboard Instrument

Having a blast with my new Starter Kit. I’m up to project 07 and I’m having an array problem and a logic problem as well. I’ve pasted the code as shown in the book at the end of this post. Two problems
PROBLEM 1
1st if I check this code it tells me an error on line 4:

"conflicting declaration 'int buttons [0]'"
'buttons' has a previous declaration as 'int buttons [6]'"

So it doesn’t like my int buttons[0] = 2; statement. I’ve tried losing the int and changing = to == both to no avail. If I comment out this line it compiles fine loads and only the second button plays a note.

PROBLEM 2
I don’t see what buttons does after this. I thought it would be used to check the 4 physical buttons on the breadboard but those are the only two places it is referenced in the code. Seems like something is missing with this array.

Any help appreciated.

Dennis
------------begin code this is the complete source ---------

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

int buttons[0] = 2;
// give the first element of the array the value 2 note that it counts from zero

int notes[] = {262, 294, 330, 349};
//corresponds to the notes C, D, E and F

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

Moderator edit: error message rendered legible

int buttons[6] = {2};

Can't see why it is defined either, but I don't have the book.

I think that may be a typo in the book (I see it in mine as well). AWOL's suggestion would fix it, or you could move 'int buttons[0] = 2' into the setup method and drop the int declaration.

e.g.:

void setup() {
  buttons[0] = 2;
  Serial.begin(9600);
}

I was pretty confused about the buttons array as well. Since it's never used again in the code I figured it was some sort of example of how to use arrays? Odd because they don't do anything like that in the rest of the book. I completely removed both lines of code from my sketch and it complied without a problem.

Owl_: I was pretty confused about the buttons array as well. Since it's never used again in the code I figured it was some sort of example of how to use arrays? Odd because they don't do anything like that in the rest of the book. I completely removed both lines of code from my sketch and it complied without a problem.

Now that I looked at it again, I think you are correct. There aren't any line numbers next to the buttons array, but the notes array starts with line 1; so it's probably just there for explaining arrays (except for the fact that 'int buttons[0] = 2' seems incorrect to me).

I got the problem solved by skipping the first two lines:

int buttons[6];
int buttons[0] = 2;

and starting at:

int notes = {262,294,330,394};

It works fine!

Regarding the first problem:

You are initializing an array with a capacity to fit six integer items:

int buttons[6];

Then you are trying to assign a value, however the syntax is incorrect, making your program attempt to re-initialize the buttons variable:

int buttons[0] = 2;

Instead for assignments you no longer need to keep in the type instruction, therefore use:

int buttons[6]; buttons[0] = 2;

This will work.

As per usage... you are right the variable is actually not being used anywhere within the rest of the program routine, therefore it is safe to remove these two lines.

Hope this helps.

I have an unrelated problem with this project unfortunately. Everything appears to be wired up properly yet the button with the 1 Megaohm resistor going into it does not seem to be outputting anything to analogRead(). The other three work so I can assume the ladder wiring is correct and I know the button isn't faulty. The pull down is working as well.

In my Serial Monitor I just continue to get 0 when I press it. Is it possible the resistor is faulty? Any tips for troubleshooting this? Thanks.

EDIT:

Just tried a different valued resistor yet the output remained 0. I rewired the button and it still isn't doing anything. The pins are placed in the breadboard in the correct direction. I'm at a loss but I'll keep fooling around for now.

EDIT:

I rewired the entire circuit on a larger breadboard and it works. I have no clue what I had wrong before. Darn! XD

Same here, mysterious 0 values

Seriously, it's so frustrating that this arduino kit has so many errors in it. I thought this was supposed to encourage and teach, not to confuse and frustrate

nass: Same here, mysterious 0 values

Seriously, it's so frustrating that this arduino kit has so many errors in it. I thought this was supposed to encourage and teach, not to confuse and frustrate

I would suggest rewiring the entire circuit. It worked for me. That is if you're certain the code is correct.

Learning isn't always stress free. Turn that frustration into motivation to overcome your obstacles!

I don’t have an issue with learning, but you have to have the right tools.

I’m actually starting to believe it’s something to do with cheap components. By putting the switches on a different part of the breadboard I’ve managed to get 3 of them to work, still working on a bodge circuit to get the 4th one working. 4 hrs later, on something that’s supposed to take 45mins. Because of crappy components. That’s not a great way to learn.

nass: I don't have an issue with learning, but you have to have the right tools.

I'm actually starting to believe it's something to do with cheap components. By putting the switches on a different part of the breadboard I've managed to get 3 of them to work, still working on a bodge circuit to get the 4th one working. 4 hrs later, on something that's supposed to take 45mins. Because of crappy components. That's not a great way to learn.

Problems like this would be very easily fixed with a multimeter with a continuity check. I think it's time for me to finally buy one.

Hmmmm, I'm damm glad I bought one before starting out.

Is there such a thing as bad batches of resistors? All my resistors read as they're supposed to except the 1M? ones, they read nothing. It's not the meter because it's on the 1000k scale setting, and the 10M? ones are fine!

nass: Hmmmm, I'm damm glad I bought one before starting out.

Is there such a thing as bad batches of resistors? All my resistors read as they're supposed to except the 1M? ones, they read nothing. It's not the meter because it's on the 1000k scale setting, and the 10M? ones are fine!

That's certainly odd if your multimeter is reading 0. You could always do a test with a LED. Hook one up with a 1M? resistor. The light will be very faint but should still be noticeable over no light at all. If you get no light whatsoever then maybe your resistor is to blame.

Ok, I've got it. User error compilation. Incredible. :roll_eyes:

0) I marked up all my resistors and the 10M? and 1M? were the last 2. 1) My 1M? uses a colour to denote 4 that's much more like a green than a yellow. So I had them down as the 10M? and thought the others were the 1M?, and used those values. But on top of that, 2) My multimeter's highest ? range is 2000k, and 10M? is 10000k. So the multimeter can't read them on any scale, hence no reading (I think!). 3) When I tried to work out why the 10M? wern't measuring I re-examined the scale and saw the 2000k max. 4) Lightbulb moments followed once I realised 1000k = 1M?

Now to work out why the circuit doesn't work properly... tomorrow night

I tend to agree. Most of the projects I've done had some sort of mistakes in them. It added to the challenge, don't get me wrong. However this project wasn't as fun simply due to vaguely working breadboard. It took me 4 attempts to get all four buttons to work. I made a mistake with one, but the other two that weren't working simply needed a different spot on the board. I had to move last button over to the other side of the board in order to get it to work. Not ideal. Discouraging at times.

There is no mistake in this code. The first 2 lines:

int buttons[6]; int buttons[0] = 2;

are EXAMPLE lines, to show how it works. You can identify them because they have no number. The code always starts at 1. 2.

etc. anything before that is 'code' to explain things.

Hello, I am a newcomer to arduino and I too have been having a problem with this project. The first key (notes[0]) is the only one not working and this is after I made the change to the code suggested by mikeucfl.

void setup() {
  buttons[0] = 2;
  Serial.begin(9600);
}

Furthermore when I tried changing the ranges for the first two keys to see if I could get a wider range for the first key to play (1000-1023 for first key and 990-1000 for second), the second key instead played the note the first key was supposed to play and the first key was still silent (I got this idea from the fact that since the last key was near the highest value and had a small range of values to read the actual note would have a slight stutter when played, so I tried tweaking the range values). My breadboard is the same as the one in the manual. Does anyone have any suggestions for me? Thanks.

Had same problems w/ '0' being returned; turned out I had (at least) 2 bad buttons in my kit. IMHO, they should have put a little less emphasis on making the book look pretty (got to love the 10 point font for readability too!), and more(?) on better components at this price...

Either way, still enjoying it immensely :)