Arduino Forum

Products => The Arduino Starter Kit and Basic Kit => Topic started by: imruffsdad on Jan 01, 2013, 01:31 am

Title: Project 07 Keyboard Instrument
Post by: imruffsdad on Jan 01, 2013, 01:31 am
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:
Code: [Select]
"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 ---------
Code: [Select]
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
Title: Re: Project 07 Keyboard Instrument
Post by: AWOL on Jan 01, 2013, 10:27 am
Code: [Select]
int buttons[6] = {2};

Can't see why it is defined either, but I don't have the book.
Title: Re: Project 07 Keyboard Instrument
Post by: mikeucfl on Jan 01, 2013, 09:41 pm
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.:
Code: [Select]

void setup() {
 buttons[0] = 2;
 Serial.begin(9600);
}
Title: Re: Project 07 Keyboard Instrument
Post by: Owl_ on Jan 02, 2013, 03:52 am
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.
Title: Re: Project 07 Keyboard Instrument
Post by: mikeucfl on Jan 02, 2013, 07:13 am

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).
Title: Re: Project 07 Keyboard Instrument
Post by: ines on Mar 09, 2013, 10:22 pm
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!
Title: Re: Project 07 Keyboard Instrument
Post by: destriaero on Mar 21, 2013, 03:37 pm
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.
Title: Re: Project 07 Keyboard Instrument
Post by: BranchofLight on Apr 11, 2013, 09:27 pm
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
Title: Re: Project 07 Keyboard Instrument
Post by: nass on Apr 14, 2013, 10:30 pm
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
Title: Re: Project 07 Keyboard Instrument
Post by: BranchofLight on Apr 14, 2013, 10:44 pm

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! (http://www.battlefront3.net/forums/images/smilies/thumup.gif)
Title: Re: Project 07 Keyboard Instrument
Post by: nass on Apr 15, 2013, 12:29 am
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.
Title: Re: Project 07 Keyboard Instrument
Post by: BranchofLight on Apr 15, 2013, 12:39 am

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.
Title: Re: Project 07 Keyboard Instrument
Post by: nass on Apr 15, 2013, 01:00 am
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!
Title: Re: Project 07 Keyboard Instrument
Post by: BranchofLight on Apr 15, 2013, 01:03 am

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.
Title: Re: Project 07 Keyboard Instrument
Post by: nass on Apr 15, 2013, 01:37 am
Ok, I've got it. User error compilation. Incredible. :smiley-roll:

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?

Title: Re: Project 07 Keyboard Instrument
Post by: nass on Apr 15, 2013, 01:37 am
Now to work out why the circuit doesn't work properly... tomorrow night
Title: Re: Project 07 Keyboard Instrument
Post by: crazzzik on Apr 16, 2013, 12:47 am
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.
Title: Re: Project 07 Keyboard Instrument
Post by: harmhero on May 14, 2013, 09:55 pm
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.
Title: Re: Project 07 Keyboard Instrument
Post by: B10N1C on Jul 15, 2013, 10:29 pm
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.

Code: [Select]
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.
Title: Re: Project 07 Keyboard Instrument
Post by: giro on Aug 21, 2013, 04:19 pm
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 :)
Title: Re: Project 07 Keyboard Instrument
Post by: chrismacp on Nov 27, 2013, 01:33 am
Just wanted to report that I had similar issues on this project. The first issue I've had really since starting with the book.

It was kind of weird though and after getting it to work I can't really explain what was wrong.

Looking at the diagram in the book it was the top push button that was not activating the piezo and reading zero in the serial monitor. Removing the push button and replacing with a normal wire instead made the circuit work as intended, giving me 1023 in the serial monitor, so the breadboard seemed fine.

I then got my multimeter and set it to resistance and measured across the push button noticing it change to zero resistance when I depressed the button. This seemed to suggest the button was working.

Then, not sure what was going on, I read this forum and decided to move the button one row down on the breadboard. It worked. I then swapped the button over for one of the other ones and they both worked.

So not sure what the problem is to be honest. Anyway, thanks for the tip, hope it doesn't catch me out again.
Title: Re: Project 07 Keyboard Instrument
Post by: brianearwood on Jan 06, 2014, 05:01 am
My problem with the buttons not working is that the pins don't reach all the way down into the breadboard.

I was able to fix by bending a set of pins outward and inserting the other deeper into the breadboard. After that it worked.
Title: Re: Project 07 Keyboard Instrument
Post by: Andrwsk23 on May 01, 2016, 01:41 pm
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.:
Code: [Select]

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

THANKS!!
Title: Re: Project 07 Keyboard Instrument
Post by: jrobarts on May 22, 2016, 11:50 am
Those two lines aren't actually for the code. They are only an explanation of how arrays are written.
The actual code starts at "int notes []= ..."

You can see that on the right side line 1 only starts there
Title: Re: Project 07 Keyboard Instrument
Post by: Gorduino283 on Aug 11, 2016, 03:40 am
The buttons part is NOT part of the code.  It was just an example.
Title: Re: Project 07 Keyboard Instrument
Post by: jedi5200 on Jan 21, 2017, 10:52 pm
my last 2 buttons don't do anything. help?
Title: Re: Project 07 Keyboard Instrument
Post by: Pinnock on Feb 26, 2017, 09:06 pm
SOLVED IT

SORRY FOR MY ENGLISH, I AM FROM COSTA RICA, ONLY SPEAK SPANISH
CHANGE THE TWO FIRST LINES FOR THIS.. WORK IT


int buttons[6] = {2,0,0,0,0,0};
/*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);
  }
  }
 
Title: Re: Project 07 Keyboard Instrument
Post by: Gluce on May 05, 2017, 12:46 am
Hello Everybody,
I am also having the same problem on this project. The last switch, the one connected to the 10 Kilohm resistor, only reads 0.

The problem is not the switch. If I switch it with one of those that work, it makes the sound. Instead, the one that was working, when put as 4th switch, doesn't work anymore.

Also, I tried to change the position on the breadbord, and the result is still the same. Any idea? =(

This is what the wiring looks like:

(https://scontent-lhr3-1.xx.fbcdn.net/v/t34.0-0/p280x280/18302455_1491355397572312_786458913_n.jpg?oh=0bb70a0571f1ec45ea2351e7ae2bf5a1&oe=590E0989)
Title: Re: Project 07 Keyboard Instrument
Post by: CrossRoads on May 05, 2017, 03:03 am
Is anything connected to the leg in row 27? It looks empty to me.
Title: Re: Project 07 Keyboard Instrument
Post by: Gluce on May 06, 2017, 04:50 pm
Sorry, I had moved a couple of things around and forgot to put it back in the right place.


(https://scontent-lhr3-1.xx.fbcdn.net/v/t34.0-12/18337312_1494058037302048_1750695580_n.jpg?oh=17c67d0c657afc17281a15178b65a650&oe=590FC241)


(https://scontent-lhr3-1.xx.fbcdn.net/v/t35.0-12/18378658_1494057777302074_437217928_o.jpg?oh=2f0ab0a3b63909d8eb88f4f063ad4124&oe=590FCA1C)

(https://scontent-lhr3-1.xx.fbcdn.net/v/t34.0-12/18361727_1494073630633822_1963199389_n.jpg?oh=a0b1e1192e20e2b57997ec07629358bb&oe=590FF612)


This should be like it is explained in the book.

Plus, I don't understand one thing: the last resistance. In the book, it says "connect tall the switches' output in one junction. Connect this junction to ground with a 10 kilohm resistor". I don't understand the function of this last resistance, considering that every single switch has got his own resistance....

Thank you in advance...
Title: Re: Project 07 Keyboard Instrument
Post by: youngbo on Jul 12, 2017, 06:01 am
Just started doing this and had the same problem.
Then after looking here realised in the book the 2 lines

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

don't have line numbers at the start

Then the following line is:

1 int notes[] = {262,294,330,349};

I guess those first 2 lines aren't in the program, you're right though confusing, although i guess they are there for a reason of tutorial but as of yet i don't understand what it is trying to teach.
Title: Re: Project 07 Keyboard Instrument
Post by: independenter on Feb 18, 2019, 07:48 pm
I had all these same problems. I tried everything that everyone suggested. The solution for me was adjusting the keyVal ranges...

if (keyVal >= 1011 && keyVal <=1023) {
    tone(8, notes[0]);
  } else if (keyVal >= 990 && keyVal <= 1010) {
    tone(8, notes[1]);
  } else if (keyVal >= 400 && keyVal <= 515) {
    tone(8, notes[2]);
  } else if (keyVal >= 5 && keyVal <= 10) {
    tone(8, notes[3]);
  } else {
    noTone(8);
Title: Re: Project 07 Keyboard Instrument
Post by: SWof6 on Mar 03, 2019, 09:53 pm
I had the same problem. Circuitry was fine, but it turned out I had the wrong resistor. Triple-check your colors!