Pages: [1] 2   Go Down
Author Topic: Project 07 Keyboard Instrument  (Read 12662 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
"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:
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
« Last Edit: January 01, 2013, 04:25:51 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24325
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int buttons[6] = {2};

Can't see why it is defined either, but I don't have the book.
« Last Edit: January 01, 2013, 04:30:12 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {
  buttons[0] = 2;
  Serial.begin(9600);
}
« Last Edit: January 01, 2013, 03:47:44 pm by mikeucfl » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

ON, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-lol
« Last Edit: April 11, 2013, 03:25:40 pm by BranchofLight » Logged

UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Photography nut trying to make the flux capacitor of macro pho

ON, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Photography nut trying to make the flux capacitor of macro pho

ON, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Photography nut trying to make the flux capacitor of macro pho

ON, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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Ω

Logged

Photography nut trying to make the flux capacitor of macro pho

Pages: [1] 2   Go Up
Jump to: