Ridiculously nooby question

Hey, I’m new to Arduino, and I am having some trouble with a project from the
projects book (included in the basic kit).

The project in question is no. 07: Keyboard Instrument.

I have written the code exactly (I think) as written in the book, and it returns this error:

soundsketch.ino: In function ‘void loop()’:
soundsketch:10: error: ‘keyVal’ was not declared in this scope
soundsketch:11: error: ‘AO’ was not declared in this scope

As I am new to this, it might be an incredibly easy question to solve, so don’t laugh too hard :stuck_out_tongue:

Here’s the “correct” code for reference:

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

void setup() {
Serial.begin(9600);
}
void loop(){
Serial.println(keyVal);
int keyVal = analogRead(AO);
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]);
}
}

Hi, before setup you need line
int keyVal;
also change AO to A0,, ie A zero.
also select tools and select auto format.
It will improve the look of your sketch and make it easier to read.
And see what that produces.
Tom... :slight_smile:

try this link to see how to post your sketch code.

Try Azero, not Aoh.

Also, when you post code snippets, highlight the code block and click the "#" button (above the smileys) to wrap it in [ special tags ]. This will ensure your code displays properly.

Thanks a lot!

Now the code doesn't return any errors at all!

There's probably some trouble with the hardware/I've done something wrong, as nothing happens when I press the buttons,
but thanks for the help!

The problem with keyVal in the first listing you posted is that you used it in a Serial.print in the line just before you declared it. If you swap the first two statements inside loop() (and change AO to A0) so it is like this"

void loop(){
int keyVal = analogRead(A0);
Serial.println(keyVal);
if (keyVal == 1023){....

it should work ok for you.