Go Down

Topic: Project 07 Keyboard Instrument (Read 16279 times) previous topic - next topic

imruffsdad

Jan 01, 2013, 01:31 am Last Edit: Jan 01, 2013, 10:25 am by AWOL Reason: 1
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

AWOL

#1
Jan 01, 2013, 10:27 am Last Edit: Jan 01, 2013, 10:30 am by AWOL Reason: 1
Code: [Select]
int buttons[6] = {2};

Can't see why it is defined either, but I don't have the book.
"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.

mikeucfl

#2
Jan 01, 2013, 09:41 pm Last Edit: Jan 01, 2013, 09:47 pm by mikeucfl Reason: 1
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);
}

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.

mikeucfl


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

ines

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!

destriaero

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.

BranchofLight

#7
Apr 11, 2013, 09:27 pm Last Edit: Apr 11, 2013, 10:25 pm by BranchofLight Reason: 1
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

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
Photography nut trying to make the flux capacitor of macro pho

BranchofLight


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!

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.
Photography nut trying to make the flux capacitor of macro pho

BranchofLight


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.

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!
Photography nut trying to make the flux capacitor of macro pho

BranchofLight


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.

nass

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?

Photography nut trying to make the flux capacitor of macro pho

Go Up